How to Install and Configure Nginx on Ubuntu 20.04?

Nginx is a popular open-source web server software that can be used as a reverse proxy, load balancer, HTTP cache, and more. It's known for its speed and scalability, and is widely used to serve web content for high-traffic websites.

If you're running an Ubuntu 20.04 server and want to use Nginx as your web server, this guide will walk you through the installation and basic configuration process.

Step 1: Update System

Before we begin, it's a good idea to update the system to ensure that all packages are up to date. You can do this by running the following command

sudo apt update && sudo apt upgrade -y

This will update the package list and install any available updates.

Step 2: Install Nginx

Now that the system is up to date, you can proceed with installing Nginx. You can do this by running the following command

sudo apt install nginx -y

This will install the latest version of Nginx on your Ubuntu 20.04 server.

Step 3: Start and Enable Nginx

After installation, start the Nginx service and enable it to start automatically on boot

sudo systemctl start nginx
sudo systemctl enable nginx

Verify that Nginx is running

sudo systemctl status nginx

Step 4: Configure Basic Server Block

Once Nginx is installed, you can begin configuring it to serve your web content. By default, Nginx will serve content located in the /var/www/html directory. You can test that Nginx is working correctly by visiting your server's IP address in a web browser.

To configure Nginx to serve your own content, you will need to create a new configuration file in the /etc/nginx/sites-available directory

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

In this file, you can define your server block, which will contain the configuration for your website. Here's an example of a basic server block

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/example.com;
    index index.html index.htm;

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

Create the document root directory and set proper permissions

sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
sudo chmod -R 755 /var/www/example.com

Create a sample index file

echo "<h1>Welcome to example.com!</h1>" > /var/www/example.com/index.html

Enable the site by creating a symbolic link

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

Test the configuration and restart Nginx

sudo nginx -t
sudo systemctl restart nginx

Step 5: Configure SSL/TLS with Let's Encrypt

If you're serving sensitive information or accepting user data, it's important to secure your website with SSL/TLS encryption. You can do this by obtaining an SSL/TLS certificate through Let's Encrypt, a free and open certificate authority.

Install the Let's Encrypt client

sudo apt install certbot python3-certbot-nginx -y

Obtain and configure the SSL certificate

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

This will automatically configure Nginx to use the newly obtained SSL/TLS certificate and redirect HTTP traffic to HTTPS.

Step 6: Firewall Configuration

As with any server, it's important to ensure that your Ubuntu 20.04 server is secured with a firewall. Ubuntu 20.04 comes with UFW (Uncomplicated Firewall) preinstalled.

Allow HTTP and HTTPS traffic

sudo ufw allow 'Nginx Full'
sudo ufw enable

This rule allows both HTTP (port 80) and HTTPS (port 443) traffic to Nginx.

Advanced Configuration Options

Multiple Server Blocks

You can host multiple domains on the same server by creating additional server blocks. For example, to add another domain

sudo nano /etc/nginx/sites-available/example.org
server {
    listen 80;
    server_name example.org www.example.org;
    root /var/www/example.org;
    index index.html index.htm;

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

Enable the new site

sudo mkdir -p /var/www/example.org
sudo ln -s /etc/nginx/sites-available/example.org /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

Basic Caching Configuration

Enable basic caching for static content by adding this to your server block

location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg)$ {
    expires 1M;
    add_header Cache-Control "public, immutable";
}

Monitoring and Logs

Monitor Nginx access and error logs to troubleshoot issues

sudo tail -f /var/log/nginx/access.log
sudo tail -f /var/log/nginx/error.log

Check Nginx status

sudo systemctl status nginx

Conclusion

In this guide, we've walked through the process of installing and configuring Nginx on Ubuntu 20.04, including SSL/TLS setup with Let's Encrypt and firewall configuration. Nginx is a powerful and versatile web server that can handle high-traffic websites efficiently, making it an excellent choice for modern web applications.

Updated on: 2026-03-17T09:01:38+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements