Docs / Web Servers / How to Install and Configure Nginx from Scratch

How to Install and Configure Nginx from Scratch

By Admin · Mar 15, 2026 · Updated Apr 24, 2026 · 478 views · 3 min read

Nginx is the most popular web server for modern applications, known for its high performance, low resource usage, and versatility as a web server, reverse proxy, and load balancer. This guide covers a production-ready Nginx installation from scratch.

Installation

# Ubuntu/Debian
sudo apt update
sudo apt install nginx
sudo systemctl enable --now nginx

# Verify
curl http://localhost
nginx -v

Configuration Structure

# Main config
/etc/nginx/nginx.conf

# Site configs
/etc/nginx/sites-available/   # All site configs
/etc/nginx/sites-enabled/     # Symlinks to active sites

# Additional configs
/etc/nginx/conf.d/            # Global configuration snippets

# Logs
/var/log/nginx/access.log
/var/log/nginx/error.log

Main Configuration

# /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
worker_rlimit_nofile 65535;

events {
    worker_connections 4096;
    multi_accept on;
    use epoll;
}

http {
    # Basic settings
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;
    client_max_body_size 64m;

    # MIME types
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Logging
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # Gzip compression
    gzip on;
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml text/javascript image/svg+xml;

    # Include site configs
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Virtual Host Configuration

# /etc/nginx/sites-available/mysite.conf
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/example.com/public;
    index index.html index.php;

    # Logging
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;

    # Static files caching
    location ~* \.(jpg|jpeg|png|gif|ico|css|js|svg|woff2)$ {
        expires 30d;
        add_header Cache-Control "public, immutable";
    }

    # PHP processing
    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # Deny access to hidden files
    location ~ /\. {
        deny all;
    }
}

# Enable the site
sudo ln -s /etc/nginx/sites-available/mysite.conf /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

SSL with Let's Encrypt

# Install Certbot
sudo apt install certbot python3-certbot-nginx

# Obtain and install certificate
sudo certbot --nginx -d example.com -d www.example.com

# Auto-renewal is configured automatically
sudo certbot renew --dry-run

Testing and Troubleshooting

# Validate configuration
sudo nginx -t

# Reload without downtime
sudo systemctl reload nginx

# View error log
tail -f /var/log/nginx/error.log

# Check which config is loaded
nginx -T  # Dump full effective config

Was this article helpful?