Nginx (pronounced "Engine-X") is an open-source, high-performance HTTP web server and reverse proxy powering over 30% of the world’s top websites. Unlike traditional thread-per-request web servers like Apache, Nginx uses an asynchronous, event-driven architecture capable of serving tens of thousands of concurrent connections with minimal memory usage.

Step 1: Install Nginx via apt on Ubuntu

Terminal Installation Commandsbash
# 1. Update package index and install Nginx
sudo apt update
sudo apt install -y nginx
 
# 2. Verify Nginx service status
sudo systemctl status nginx

Step 2: Configure UFW Firewall Rules

Allow HTTP (Port 80) and HTTPS (Port 443) traffic through Ubuntu’s UFW firewall:

UFW Firewall Configurationbash
# Enable Nginx Full profile (Ports 80 & 443)
sudo ufw allow 'Nginx Full'
 
# Verify UFW status
sudo ufw status

Step 3: Creating a Custom Nginx Server Block (Virtual Host)

Best practice on Ubuntu is creating dedicated server block configuration files under /etc/nginx/sites-available/ and symlinking them to sites-enabled/:

/etc/nginx/sites-available/example.comnginx
server {
    listen 80;
    listen [::]:80;
 
    server_name example.com www.example.com;
    root /var/www/example.com/html;
    index index.html index.htm;
 
    location / {
        try_files $uri $uri/ =404;
    }
 
    # Custom Error Pages
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}
Enable Server Block & Reloadbash
# 1. Create symlink to enable site
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
 
# 2. Test Nginx syntax configuration for errors
sudo nginx -t
 
# 3. Reload Nginx without dropping active connections
sudo systemctl reload nginx