How to install NGINX on Ubuntu 20.04 Focal Fossa

In the following tutorial you will learn how to install Nginx web server on Ubuntu 20.04 Focal Fossa with simple to follow steps.

In this tutorial you will learn:

  • How to install Nginx
  • How to configure firewall to allow for Nginx incoming traffic
  • How to manage Nginx server

How to install NGINX on Ubuntu 20.04 Focal Fossa

How to install NGINX on Ubuntu 20.04 Focal Fossa

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Installed Ubuntu 20.04 or upgraded Ubuntu 20.04 Focal Fossa
Software Nginx
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

How to install NGINX on Ubuntu 20.04 step by step instructions

  1. The first step is to perform the NGINX server installation. To do it simply use the apt command to install the nginx package from the standard Ubuntu repository:
    $ sudo apt install nginx
    


  2. Next, step is to create a firewall rule to allow an incoming traffic on port 80 or port 443 or both. Ubuntu uses UFW as a default firewall. Please check the state of your UFW firewall as you only need to go through this step if your UFW firewall is enabled.

    The installation of the NGINX server comes also with a pre-configured UFW firewall rules:

    Nginx Full
    Allows for both HTTP port 80 and HTTPS port 443 incoming traffic
    Nginx HTTP
    Allows for HTTP port 80 incoming traffic
    Nginx HTTPS
    Allows for HTTPS port 443 incoming traffic

    Based on your environment whether you intend to include SSL (HTTPS) as part of the NGINX configuration later on, select one of the above and execute the following command to add new UFW rule to allow incoming traffic to your NGINX server.:

    $ sudo ufw allow in "Nginx Full"
    OR
    $ sudo ufw allow in "Nginx HTTP"
    OR
    $ sudo ufw allow in "Nginx HTTPS"
    
  3. At this point the NGINX server should be up and running. Use the NGINX server’s IP address or hostname to connect to it remotely.

    The test can be simple as using the curl command. Given that the IP address of our NGINX server is 192.168.1.28 to test the server we can execute the following command:

    $ curl -I 192.168.1.28
    HTTP/1.1 200 OK
    Server: nginx/1.17.8 (Ubuntu)
    Date: Fri, 28 Feb 2020 00:29:33 GMT
    Content-Type: text/html
    Content-Length: 612
    Last-Modified: Thu, 27 Feb 2020 23:48:55 GMT
    Connection: keep-alive
    ETag: "5e585567-264"
    Accept-Ranges: bytes
    
     IP Address or hostname of your NGINX server to connect to it using web browser

    Alternatively, use the IP Address or hostname of your NGINX server to connect to it by using any web browser.



  4. The last step is to configure the NGINX server to start after the system restart. To do so enter the following command into your terminal window:
    $ sudo systemctl enable nginx
    
    Check the status of the NGINX server and enable the NGINX server to automatically start after system reboot

    Check the status of the NGINX server and enable the NGINX server to automatically start after system reboot

    Additional commands you may need to properly manage the NGINX server include:

    systemctl status nginx
    Check the status of your NGINX server
    systemctl restart nginx
    Fully restart the NGINX server
    systemctl reload nginx
    Reload NGINX configuration without dropping existing NGINX client connections
    systemctl start nginx
    Start NGINX server
    systemctl stop nginx
    Stop NGINX server

Troubleshooting

What follows are few tips on how to troubleshoot your NGINX server.

Firstly, for any issues or errors check the NGINX server log. The NGINX by default keeps an access and error log. Both logs are located within the /var/log/nginx/ directory.

In case you cannot start the NGINX server use the nginx -t command to check for configuration file syntax. Executing the nginx -t command after any NGINX server configuration changes is a good administration practice anyway.

In case you are unable to connect to your NGINX server make sure that the firewall rules are properly set as per the above Step 2 and confirm that the NGINX is listening on a proper port and network ID. Example:

$ ss -lt
State    Recv-Q   Send-Q     Local Address:Port        Peer Address:Port   Process   
LISTEN   0        128        127.0.0.53%lo:domain           0.0.0.0:*                
LISTEN   0        128              0.0.0.0:ssh              0.0.0.0:*                
LISTEN   0        511              0.0.0.0:http             0.0.0.0:*                
LISTEN   0        128                 [::]:ssh                 [::]:*                
LISTEN   0        511                 [::]:http                [::]:*