Skip to content

📥 Installation & Basic Configuration

Installing Nginx

bash
# Ubuntu/Debian
sudo apt update
sudo apt install -y nginx

# CentOS/RHEL
sudo yum install -y epel-release
sudo yum install -y nginx

# Start and enable on boot
sudo systemctl start nginx
sudo systemctl enable nginx

# Verify installation
nginx -v
nginx version: nginx/1.24.0

# Check config 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

# Test access (open http://server-IP in browser)
curl http://localhost
<!DOCTYPE html>
<html>
<head><title>Welcome to nginx!</title></head>
<body><h1>Welcome to nginx!</h1></body>
</html>

Nginx Directory Structure

text
/etc/nginx/
├── nginx.conf              # Main configuration file
├── conf.d/                 # Custom configs (recommended place)
│   └── default.conf
├── sites-available/        # Available site configs
├── sites-enabled/          # Enabled site configs (symlinks)
├── mime.types              # MIME type definitions
└── modules-enabled/        # Dynamic modules

Configuration File Structure

nginx
# Main global block (affects overall operation)
worker_processes auto;        # Worker processes (auto = CPU core count)
error_log /var/log/nginx/error.log warn;

# Events block (connection handling config)
events {
    worker_connections 1024;  # Max connections per process
}

# HTTP block (HTTP-related config)
http {
    include mime.types;
    default_type application/octet-stream;

    # Log format
    log_format main '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent';

    # Server block (virtual host)
    server {
        listen 80;                    # Listening port
        server_name example.com;      # Domain name

        # Location block (URL matching)
        location / {
            root /var/www/html;       # Website root directory
            index index.html;
        }
    }
}

📝 Note: 📝 Structure hierarchy: main → events → http → server → location, from large to small, nested layer by layer.

Common Management Commands

bash
# Test config syntax
sudo nginx -t

# Reload config (no downtime, smooth transition)
sudo nginx -s reload

# Quick stop (forcefully terminates all connections)
sudo nginx -s stop

# Graceful stop (finish current requests before stopping)
sudo nginx -s quit

# View Nginx processes
ps aux | grep nginx
root     12345  0.0  0.1  32768  1024 ?  Ss   10:00   0:00 nginx: master process
www-data 12346  0.0  0.1  33792  2048 ?  S    10:00   0:00 nginx: worker process