How to Install and Configure Nginx on Ubuntu 20.04

Nginx is a free and open-source web server that can deliver both static and dynamic applications or services. It is commonly used as a web server, load balancer, reverse proxy, or HTTP cache, and integrates seamlessly with existing applications or serves content directly via a server’s IP or domain.

This guide explains the steps required to set up Nginx on Ubuntu 20.04 and configure a sample web application for best performance.

Prerequisites

Before you begin, ensure that you have the following in place:

  • An Ubuntu 20.04 server.
  • A new A record for your domain pointing to the server’s IP address.
  • SSH access as a non-root user with sudo privileges.
  • The server updated with the latest packages.

Install Nginx on Ubuntu 20.04

The current version of Nginx is available in the default APT repositories of Ubuntu 20.04. Follow these steps to update your server packages and install Nginx.

Update the package index

Install Nginx

$ sudo apt install nginx -y

Verify the installed version

Your output should look similar to:

nginx version: nginx/1.18.0 (Ubuntu)

Manage the Nginx Service

Nginx runs as a systemd service, allowing you to control its processes. Use the following commands to manage it.

Enable Nginx at boot

$ sudo systemctl enable nginx

Start Nginx

$ sudo systemctl start nginx

Stop Nginx

$ sudo systemctl stop nginx

Restart Nginx

$ sudo systemctl restart nginx

Check Nginx status

$ sudo systemctl status nginx

If the output shows Active: active (running), then Nginx is running correctly. If it shows Active: active (failed), stop any processes using port 80 and restart Nginx.

Create a New Nginx Virtual Host

A virtual host configuration allows Nginx to serve files from a specific directory under a given domain. Follow the steps to set up a sample configuration.

Create a new configuration file

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

Add the configuration

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;
        }
}

This configuration listens on port 80 for app.example.com and serves files from /var/www/app.example.com.

Test configuration for errors

Activate the site

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

Create the web root

$ sudo mkdir -p /var/www/app.example.com

Create an HTML test page

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

Add the following HTML to the file:

Restart Nginx

$ sudo systemctl restart nginx

Verify access

Use curl or a browser to confirm your setup:

$ curl http://app.example.com

Expected output:

Secure the Nginx Web Server

SSL certificates enable encrypted communication between a user’s browser and the Nginx server over HTTPS. By default, Nginx listens on the unsecured HTTP port 80. Follow these steps to obtain trusted Let’s Encrypt SSL certificates and secure Nginx for encrypted HTTPS connections.

Install Certbot

Use Snap to install the Let’s Encrypt Certbot client package.

$ sudo snap install --classic certbot

Check Certbot version

Example output:

certbot 3.3.0

Allow HTTP traffic for ACME verification

Certbot requires access to port 80 to verify domain ownership.

Generate an SSL certificate

Replace app.example.com with your actual domain configured in the Nginx virtual host.

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

Set Up Firewall Rules

Ubuntu 20.04 comes with Uncomplicated Firewall (UFW) enabled by default. Use the following steps to configure firewall rules so that Nginx can accept both HTTP and HTTPS traffic.

Allow HTTPS traffic

Verify firewall rules

Expected 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 now successfully installed Nginx on Ubuntu 20.04 and configured it to serve web applications. With virtual hosts, you can securely deliver applications, and by integrating Nginx with other technologies such as MySQL and PHP, you can also host dynamic web applications.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: