How to install and configure Nginx as a web server on Linux

Introduction

Nginx (pronounced "engine-x") is a high-performance web server, reverse proxy, and load balancer. It is known for its low memory usage and ability to handle a large number of concurrent connections, making it one of the most popular web servers in the world.

Prerequisites

  • A Linux VPS with Ubuntu 22.04/24.04 or Debian 12 (instructions are similar for other distributions)
  • Root or sudo access
  • A domain name pointed to your server (optional, for virtual hosts)

Step 1: Install Nginx

sudo apt update
sudo apt install nginx -y

Verify the installation:

nginx -v

Step 2: Start and enable Nginx

sudo systemctl start nginx
sudo systemctl enable nginx

Check the status:

sudo systemctl status nginx

Open your browser and navigate to http://your-server-ip. You should see the default Nginx welcome page.

Step 3: Allow Nginx through the firewall

If UFW is enabled:

sudo ufw allow 'Nginx Full'

This allows both HTTP (80) and HTTPS (443) traffic.

Step 4: Understanding the directory structure

Path Purpose
/etc/nginx/nginx.conf Main configuration file
/etc/nginx/sites-available/ Virtual host configuration files
/etc/nginx/sites-enabled/ Symlinks to active virtual hosts
/var/www/html/ Default web root directory
/var/log/nginx/access.log Access log
/var/log/nginx/error.log Error log

Step 5: Create a virtual host (server block)

Create a new configuration file for your domain:

sudo nano /etc/nginx/sites-available/yourdomain.com

Add the following configuration:

server {
    listen 80;
    listen [::]:80;

    server_name yourdomain.com www.yourdomain.com;
    root /var/www/yourdomain.com;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    access_log /var/log/nginx/yourdomain.com.access.log;
    error_log /var/log/nginx/yourdomain.com.error.log;
}

Step 6: Set up the web root

sudo mkdir -p /var/www/yourdomain.com
sudo chown -R www-data:www-data /var/www/yourdomain.com

Create a test page:

echo "<h1>Hello from yourdomain.com</h1>" | sudo tee /var/www/yourdomain.com/index.html

Step 7: Enable the virtual host

sudo ln -s /etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/

Test the configuration for syntax errors:

sudo nginx -t

If the test passes, reload Nginx:

sudo systemctl reload nginx

Step 8: Enable HTTPS with Let's Encrypt

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Obtain and install a certificate:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Certbot will automatically modify your Nginx configuration to use HTTPS and set up automatic renewal.

Verify automatic renewal is configured:

sudo certbot renew --dry-run

Useful Nginx commands

Command Description
sudo systemctl restart nginx Restart Nginx
sudo systemctl reload nginx Reload configuration without downtime
sudo nginx -t Test configuration for errors
sudo tail -f /var/log/nginx/error.log Monitor error log in real time