How to Install Nginx Web Server on Ubuntu 20.04

Updated on 08 May, 2025
How to Install Nginx Web Server on Ubuntu 20.04 header image

Nginx is an open-source web server that delivers static and dynamic web applications or services. It functions as a web server, load balancer, reverse proxy, or HTTP cache, integrating with existing applications or delivering web services via your server's IP or domain.

This article outlines the steps to install Nginx on Ubuntu 20.04 and configure sample web applications for optimal performance.

Prerequisites

Before you begin:

Install Nginx on Ubuntu 20.04

The latest Nginx package is available in the default APT repositories for Ubuntu 20.04. Follow the steps below to update your server packages and install Nginx.

  1. Update the server package index.

    console
    $ sudo apt update
    
  2. Install Nginx.

    console
    $ sudo apt install nginx -y
    
  3. View the installed Nginx version on your server.

    console
    $ nginx -v
    

    Your output should be similar to the one below:

    nginx version: nginx/1.18.0 (Ubuntu)

Manage the Nginx System Service

Nginx uses the nginx systemd service profile to manage the web server's runtime and processes. Follow the steps below to enable the Nginx service and control the web server processes on your server.

  1. Enable the Nginx web server to start automatically at boot time.

    console
    $ sudo systemctl enable nginx
    

    Output:

    Synchronizing state of nginx.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
    Executing: /usr/lib/systemd/systemd-sysv-install enable nginx
  2. Start the Nginx service.

    console
    $ sudo systemctl start nginx
    
  3. Stop the Nginx service.

    console
    $ sudo systemctl stop nginx
    
  4. Restart the Nginx service.

    console
    $ sudo systemctl restart nginx
    
  5. View the Nginx service status and verify that it's running.

    console
    $ sudo systemctl status nginx
    

    Output:

    ● nginx.service - A high performance web server and a reverse proxy server
         Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
         Active: active (running) since Sun 2025-04-06 07:47:16 UTC; 3s ago
           Docs: man:nginx(8)
        Process: 3705 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
        Process: 3714 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
       Main PID: 3715 (nginx)
          Tasks: 5 (limit: 9415)
         Memory: 5.3M
         CGroup: /system.slice/nginx.service
                 ├─3715 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
                 ├─3716 nginx: worker process
                 ├─3717 nginx: worker process
                 ├─3718 nginx: worker process
                 └─3719 nginx: worker process

    Based on the output value Active: active (running), Nginx is running on your server. If the status shows Active: active (failed), stop any processes using HTTP port 80 and restart the nginx service.

Create a New Nginx Virtual Host

Nginx virtual hosts include configurations that allow the web server to serve web application files from a specific directory using a domain on your server. Follow the steps below to create a new sample Nginx virtual host configuration for secure file delivery.

  1. Create a new Nginx virtual host configuration in the /etc/nginx/sites-available directory. For example, app.example.com.conf.

    console
    $ sudo nano /etc/nginx/sites-available/app.example.com.conf
    
  2. Add the following configurations to the file.

    ini
    server {
            listen 80;
            listen [::]:80;
    
            server_name app.example.com;
    
            root /var/www/app.example.com;
            index index.html;
    
            location / {
                    try_files $uri $uri/ =404;
            }
    }
    

    Save and close the file.

    The above Nginx virtual host configuration listens for incoming connections using your app.example.com domain and serves your web application files from the /var/www/app.example.com web root directory on your server.

  3. Test the Nginx configuration for errors.

    console
    $ sudo nginx -t
    

    Output:

    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
  4. Link the configuration to the /etc/nginx/sites-enabled directory to activate the virtual host on your server.

    console
    $ sudo ln -s /etc/nginx/sites-available/app.example.com.conf /etc/nginx/sites-enabled/
    
  5. Create a new web root directory /var/www/app.example.com to store your web application files.

    console
    $ sudo mkdir -p /var/www/app.example.com
    
  6. Create a new HTML application file within the directory. For example, index.html.

    console
    $ sudo nano /var/www/app.example.com/index.html
    
  7. Add the following HTML contents to the file.

    html
    <html>
        <head></head>
        <body>
            <h1>Greetings from Vultr</h1> 
        </body>
    </html>
    

    Save and close the file.

    The above HTML application displays a Greetings from Vultr heading when accessed in a web browser.

  8. Restart Nginx to apply the new virtual host configurations on your server.

    console
    $ sudo systemctl restart nginx
    
  9. Access your domain to verify that Nginx correctly delivers the virtual host web application files on your server. For example, use the Curl utility to test access to your domain.

    console
    $ curl http://app.example.com
    

    Output:

    <html>
        <head></head>
        <body>
            <h1>Greetings from Vultr</h1> 
        </body>
    </html>

Secure the Nginx Web Server

SSL certificates provide encrypted communication between a user's browser and the Nginx web server via HTTPS. Nginx listens for incoming connections on the insecure HTTP port 80 by default. Follow the steps below to generate trusted Let's Encrypt SSL certificates and secure Nginx to enable encrypted HTTPS connections.

  1. Install the Certbot Let's Encrypt client package using Snap.

    console
    $ sudo snap install --classic certbot
    
  2. View the installed Certbot version on your server.

    console
    $ certbot --version
    

    Output:

    certbot 3.3.0
  3. Allow network connections on the HTTP port 80 for Certbot ACME verification.

    console
    $ sudo ufw allow 80/tcp
    
  4. Generate a new SSL certificate for your domain. Replace app.example.com with the actual domain in your Nginx virtual host configurations.

    console
    $ sudo certbot --nginx -d app.example.com --agree-tos
    

Set Up Firewall Rules

Uncomplicated Firewall (UFW) is enabled by default on Ubuntu 20.04. Follow the steps below to configure the firewall with rules that allow Nginx to listen for incoming HTTP and HTTPS connections on your server.

  1. Allow connections on the HTTPS port 443.

    console
    $ sudo ufw allow 443/tcp
    
  2. View the UFW table and verify that the new connection rules are active.

    console
    $ sudo ufw status
    

    Output:

    Status: active
    
    To                         Action      From
    --                         ------      ----
    22/tcp                     ALLOW       Anywhere                  
    80/tcp                     ALLOW       Anywhere                  
    443/tcp                    ALLOW       Anywhere                  
    22/tcp (v6)                ALLOW       Anywhere (v6)             
    80/tcp (v6)                ALLOW       Anywhere (v6)             
    443/tcp (v6)               ALLOW       Anywhere (v6)

Conclusion

You have successfully installed Nginx on your Ubuntu 20.04 server and set it up to serve web applications. Nginx supports various virtual host configurations for secure delivery of applications. It can also integrate with other technologies like MySQL and PHP to power dynamic web applications.

Comments

No comments yet.