Nginx Installation Guide
Environment Requirements
Nginx supports multiple operating systems including:
- Linux (Debian, Ubuntu, CentOS, RHEL, Alpine, etc.)
- FreeBSD
- Solaris
- AIX
- HP-UX
- macOS
- Windows
Installation Methods
Method 1: Using Package Manager (Recommended)
Ubuntu/Debian
# Update package index
sudo apt update
# Install Nginx
sudo apt install nginx
# Start Nginx
sudo systemctl start nginx
# Enable on boot
sudo systemctl enable nginx
CentOS/RHEL
# Install EPEL repository
sudo yum install epel-release
# Install Nginx
sudo yum install nginx
# Start Nginx
sudo systemctl start nginx
# Enable on boot
sudo systemctl enable nginx
Alpine
apk add nginx
macOS
# Install using Homebrew
brew install nginx
# Start Nginx
brew services start nginx
Method 2: Installing from Source
Install Build Dependencies
Debian/Ubuntu:
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g-dev libssl-dev
CentOS/RHEL:
sudo yum install gcc pcre-devel zlib-devel openssl-devel
Download Source Code
# Download latest stable version
wget https://nginx.org/download/nginx-1.26.0.tar.gz
# Extract
tar -zxf nginx-1.26.0.tar.gz
# Enter directory
cd nginx-1.26.0
Configure Build
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_v3_module \
--with-http_stub_status_module \
--with-pcre
Build and Install
# Compile
make
# Install
sudo make install
Method 3: Using Docker
# Pull official image
docker pull nginx
# Run Nginx container
docker run -d -p 80:80 --name mynginx nginx
# Mount configuration and logs
docker run -d -p 80:80 \
-v /host/path/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /host/logs:/var/log/nginx \
--name mynginx nginx
Method 4: Windows Installation
- Download Windows version from https://nginx.org/en/download.html
- Extract to desired directory
- Double-click nginx.exe to start
- Or start from command line:
cd C:\nginx
start nginx
Post-Installation Verification
Check Installation
# Check Nginx version
nginx -v
# View detailed version information
nginx -V
Check Running Status
# Check if process is running
ps aux | grep nginx
# Check status using systemctl
systemctl status nginx
Test Access
# Use curl to test
curl http://localhost
# Or open browser to access
http://localhost
Directory Structure
Linux Default Directories
| Path | Description |
|---|---|
| /etc/nginx/ | Configuration directory |
| /etc/nginx/nginx.conf | Main configuration file |
| /etc/nginx/conf.d/ | Sub-configuration directory |
| /var/log/nginx/ | Log directory |
| /var/www/html/ | Default document root |
| /usr/sbin/nginx | Executable file |
| /usr/share/nginx/html/ | Default static files directory |
Compiled Installation Directories
| Path | Description |
|---|---|
| /usr/local/nginx/ | Installation root directory |
| /usr/local/nginx/conf/ | Configuration directory |
| /usr/local/nginx/html/ | Default document root |
| /usr/local/nginx/logs/ | Log directory |
| /usr/local/nginx/sbin/ | Executable directory |
Uninstalling Nginx
Debian/Ubuntu
sudo apt remove nginx nginx-common nginx-full
CentOS/RHEL
sudo yum remove nginx
Compiled Installation
# Stop Nginx
sudo nginx -s stop
# Remove installation directory
sudo rm -rf /usr/local/nginx
Common Issues
Port Conflict
If startup fails due to port conflict:
# Check port usage
netstat -tulpn | grep :80
# Modify Nginx listening port
# Edit /etc/nginx/nginx.conf
# Change listen 80; to listen 8080;
Permission Issues
If you encounter permission errors:
# Check Nginx user
ps aux | grep nginx
# Fix file permissions
sudo chown -R www-data:www-data /var/www/html/
Configuration Syntax Check
# Test configuration syntax
nginx -t
# Test with specific configuration file
nginx -t -c /path/to/nginx.conf
