Skip to main content
Advertisement

Nginx Installation and Basic Environment Setup

To use Nginx in production, you need to know how to install it for your environment and manage the service. This chapter covers installation on Ubuntu, CentOS, macOS, and Docker, along with the directory structure and process management commands.


Ubuntu / Debian Installation

Install via Package Manager (apt)

# Update package list
sudo apt update

# Install Nginx
sudo apt install -y nginx

# Start service
sudo systemctl start nginx

# Enable auto-start on boot
sudo systemctl enable nginx

# Check status
sudo systemctl status nginx

After installation, open http://your-server-ip in a browser to see the Nginx default welcome page.

Install Latest Stable Version (Official Repository)

The Nginx version in the Ubuntu default repository may be outdated. To install the latest stable version, add the official Nginx repository.

# Install required packages
sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring

# Add Nginx official signing key
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

# Add stable version repository
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list

sudo apt update
sudo apt install -y nginx

# Check version
nginx -v

CentOS / RHEL / Rocky Linux Installation

# Add EPEL repository (CentOS 7/8)
sudo yum install -y epel-release

# Install Nginx
sudo yum install -y nginx

# Start service and enable auto-start
sudo systemctl start nginx
sudo systemctl enable nginx

# Allow HTTP/HTTPS through firewall
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

CentOS 8+ / Rocky Linux (dnf)

sudo dnf install -y nginx
sudo systemctl start nginx && sudo systemctl enable nginx

macOS Installation (Homebrew)

# If Homebrew is installed
brew install nginx

# Start service (default port 8080; port 80 requires root)
brew services start nginx

# Or run directly
nginx

# Configuration file location:
# /opt/homebrew/etc/nginx/nginx.conf (Apple Silicon)
# /usr/local/etc/nginx/nginx.conf (Intel)

Running Nginx with Docker

Docker is commonly used in development or container-based production environments.

# Run latest stable version (mapping port 80)
docker run -d --name nginx -p 80:80 nginx

# Mount configuration file and web root from host
docker run -d \
--name nginx \
-p 80:80 \
-v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro \
-v $(pwd)/html:/usr/share/nginx/html:ro \
nginx

# Reload configuration in running container
docker exec nginx nginx -s reload

docker-compose.yml example:

version: '3.8'
services:
nginx:
image: nginx:stable-alpine
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/conf.d:/etc/nginx/conf.d:ro
- ./nginx/ssl:/etc/nginx/ssl:ro
- ./html:/usr/share/nginx/html:ro
restart: unless-stopped

Nginx Directory Structure

Ubuntu/Debian

/etc/nginx/                     ← Configuration file root
nginx.conf ← Main configuration file
conf.d/ ← Additional config files (*.conf auto-loaded)
default.conf ← Default server block
sites-available/ ← Virtual host configs (including disabled)
sites-enabled/ ← Active virtual hosts (symbolic links)
snippets/ ← Reusable configuration snippets
mime.types ← MIME type mappings
fastcgi_params ← FastCGI parameters
proxy_params ← Default proxy parameters

/var/log/nginx/ ← Log directory
access.log ← Access log
error.log ← Error log

/var/www/html/ ← Default web root

/var/run/nginx.pid ← Nginx master process PID file
/usr/sbin/nginx ← Nginx binary

Nginx Process Architecture

Nginx consists of a master process and worker processes.

[Master Process] (root privileges)
│ Reads config, manages workers, receives signals

├─ [Worker Process 1] (nobody/nginx user)
├─ [Worker Process 2] ← Handles actual HTTP requests
└─ [Worker Process N] ← Created per CPU core
# Check processes
ps aux | grep nginx

# Example output:
root 1234 0.0 0.0 nginx: master process /usr/sbin/nginx
www-data 1235 0.0 0.1 nginx: worker process
www-data 1236 0.0 0.1 nginx: worker process

Nginx Service Management Commands

systemctl Commands (Linux)

sudo systemctl start nginx      # Start
sudo systemctl stop nginx # Stop
sudo systemctl restart nginx # Restart (causes brief disconnection)
sudo systemctl reload nginx # Reload config (zero-downtime, recommended)
sudo systemctl status nginx # Check status
sudo systemctl enable nginx # Enable auto-start on boot
sudo systemctl disable nginx # Disable auto-start

nginx Signal Commands

nginx -s stop       # Immediate shutdown (SIGTERM)
nginx -s quit # Graceful shutdown after completing requests (SIGQUIT)
nginx -s reload # Zero-downtime configuration reload (SIGHUP)
nginx -s reopen # Reopen log files (SIGUSR1)
nginx -t # Test configuration syntax (no changes applied)
nginx -T # Test configuration + print full output
nginx -v # Show version
nginx -V # Show version + compile options

Key point: When changing configuration in production, use reload instead of restart. restart briefly drops all connections, while reload replaces workers gracefully — existing workers finish their current requests before the new config takes effect.


Verifying Installation

# Check version and compile options
nginx -V

# Test configuration syntax
sudo nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

# Confirm port 80 is listening
sudo ss -tlnp | grep :80

Summary

  • Ubuntu: apt install nginx, CentOS: yum/dnf install nginx
  • Docker: Separate config files and web root via volume mounts
  • Key directories: /etc/nginx/ (config), /var/log/nginx/ (logs), /var/www/html/ (web root)
  • After config changes: nginx -t to validate → systemctl reload nginx for zero-downtime apply
Advertisement