This guide will walk you through the process of installing and configuring the Nginx web server on an Ubuntu or Debian system. By the end of this tutorial, you will have a basic understanding of how to set up Nginx, create server blocks (virtual hosts), manage sites, configure firewall rules, and test your setup.

Installing Nginx

To install Nginx on Ubuntu or Debian, follow these steps:

  1. Update the package index on your server:
  2. sudo apt update
  3. Install Nginx using the following command:
  4. sudo apt install nginx
  5. Once the installation is complete, you can start the Nginx service with:
  6. sudo systemctl start nginx
  7. To ensure that Nginx starts automatically at boot, enable it using:
  8. sudo systemctl enable nginx

Default Configuration

Nginx comes with a default configuration file located at /etc/nginx/nginx.conf. It's generally recommended to leave this file untouched and make site-specific configurations in server blocks.

The default server block is found at /etc/nginx/sites-available/default and is enabled by creating a symbolic link to it in the sites-enabled directory. You can view its contents with:

cat /etc/nginx/sites-available/default

Creating Server Blocks (Virtual Hosts)

To host multiple domains on a single server, you need to create separate server blocks for each domain.

  1. Create a new configuration file in the sites-available directory:
  2. sudo nano /etc/nginx/sites-available/example.com
  3. Add the following basic server block configuration, replacing example.com with your domain name and adjusting paths as necessary:
  4. server {
            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;
            }
        }
  5. Create the document root directory:
  6. sudo mkdir -p /var/www/example.com/html
  7. Set the correct permissions for the web root directory:
  8. sudo chown -R $USER:$USER /var/www/example.com/html
    sudo chmod -R 755 /var/www
  9. Create a sample index file to test your configuration:
  10. echo "Welcome to example.com!" | sudo tee /var/www/example.com/html/index.html
  11. Enable the new site by creating a symbolic link in the sites-enabled directory:
  12. sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
  13. Test your Nginx configuration for syntax errors:
  14. sudo nginx -t
  15. If the test is successful, reload Nginx to apply changes:
  16. sudo systemctl reload nginx

Firewall Configuration

Ensure that your firewall allows traffic on port 80 (HTTP) and optionally port 443 (HTTPS).

  1. If you are using UFW, allow HTTP traffic:
  2. sudo ufw allow 'Nginx Full'

Testing Your Setup

To verify that your Nginx server block is working correctly, open a web browser and navigate to your domain name or IP address. You should see the "Welcome to example.com!" message you created earlier.

Troubleshooting

  • 403 Forbidden Error: Check that the permissions for your document root directory are set correctly and that Nginx has read access.
  • 502 Bad Gateway Error: This often indicates an issue with a backend service. Ensure all services required by your site (e.g., PHP-FPM) are running.
  • Nginx Not Starting: Check the Nginx error log for more information using sudo tail -f /var/log/nginx/error.log.