Learn how to install and configure Nginx on Ubuntu 22.04 with this comprehensive step-by-step tutorial. From updating your package index to configuring server blocks, this guide covers all the essential steps to get your web server up and running smoothly.

Step 1: Update the Package Index

First, ensure your package index is up-to-date:

sudo apt update

Step 2: Install Nginx

Install Nginx using the package manager:

sudo apt install nginx

Step 3: Adjust Firewall Settings

If you have a firewall running, you’ll need to allow traffic on HTTP (port 80) and HTTPS (port 443). Nginx registers itself as a service with ufw upon installation, making it straightforward to allow traffic.

Enable Nginx:

sudo ufw allow 'Nginx Full'

Check the status of ufw to ensure the rules have been added:

sudo ufw status

Step 4: Start and Enable Nginx

Start Nginx and enable it to start at boot:

sudo systemctl start nginx
sudo systemctl enable nginx

Step 5: Verify Nginx Installation

You can verify that Nginx is running by visiting your server’s IP address in your web browser. If you don’t know your server’s IP address, you can find it by running:

ip a

Enter your server’s IP address in your web browser (e.g., http://your_server_ip). You should see the Nginx welcome page.

Alternatively, you can check the status of Nginx using:

sudo systemctl status nginx

Step 6: Managing Nginx

Here are some useful commands for managing the Nginx service:

  • To stop the Nginx service: sudo systemctl stop nginx
  • To start the Nginx service: sudo systemctl start nginx
  • To restart the Nginx service: sudo systemctl restart nginx
  • To reload the Nginx configuration without dropping connections: sudo systemctl reload nginx
  • To disable Nginx from starting at boot: sudo systemctl disable nginx

Step 7: Configure Nginx

The main configuration file for Nginx is located at /etc/nginx/nginx.conf. Additional configuration files are located in the /etc/nginx/sites-available/ directory, and the symlinks to these files are in the /etc/nginx/sites-enabled/ directory.

You can create new server blocks (virtual hosts) by creating configuration files in /etc/nginx/sites-available/ and then linking them to /etc/nginx/sites-enabled/.

For example, to create a new server block, you can create a file called example.com:

sudo nano /etc/nginx/sites-available/example.com.conf

Add your server block configuration, then save and exit the file.

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

    server_name example.com www.example.com;

    root /var/www/example.com/html;
    index index.html index.htm index.nginx-debian.html;

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

    error_page 404 /404.html;
        location = /404.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

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

Create site content in directory

sudo nano /var/www/example.com/html/index.html

Example index.html

Welcome to example.com!

Enable the server block by creating a symbolic link:

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

Test the Nginx configuration for syntax errors:

sudo nginx -t

Reload Nginx to apply the changes:

sudo systemctl reload nginx

That’s it! You’ve successfully installed and configured Nginx on your Ubuntu 22.04 server.