How to Install Nginx on Ubuntu 20.04

Install Nginx on Ubuntu 20.04

In this article, we will have explained the necessary steps to install and setup Nginx on Ubuntu 20.04. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Nginx pronounced “engine x” is high performance light-weight HTTP and reverse proxy web server capable of handling large websites. It can be used as a standalone web server, load balancer, content cache, and reverse proxy for HTTP and non-HTTP servers.

Install Nginx on Ubuntu

Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.

sudo apt update
sudo apt upgrade

Step 2. Install Nginx.

Nginx is available in the Ubuntu 20.04 repositories by default, so you can install Nginx directly using the apt command:

sudo apt install nginx

Once installed, check to see if the Nginx service is running:

sudo service nginx status

Step 3. Configure Firewall.

Now you can set up Uncomplicated Firewall (UFW) with Nginx to allow public access on default web ports for HTTP and HTTPS:

sudo ufw allow 'Nginx Full'

Now check the firewall status:

Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx Full (v6)            ALLOW       Anywhere (v6)

Step 4. Nginx Configuration File’s Structure.

  • Nginx configuration files are stored in the /etc/nginx directory.
  • The main Nginx configuration file is /etc/nginx/nginx.conf.
  • Server block (vhost) configuration files are stored in /etc/nginx/sites-available directory. The configuration files found in this directory are used by Nginx only when linked to the /etc/nginx/sites-enabled directory.
  • To activate a server blocks create a symlink (a pointer) from the configuration file in a sites-available directory to the sites-enabled directory.
  • To write more maintainable code, it’s a good idea to follow a standard naming convention. For example, if your domain name is mydomain.com then the configuration file should be named /etc/nginx/sites-available/mydomain.com.conf.
  • The /etc/nginx/snippets directory contains configuration snippets that can be included in the server block files. If you use repeatable configuration segments then you can refactor those segments into snippets and include the snippet file to the server blocks.
  • Nginx log files (access.log and error.log) are located in the /var/log/nginx/ directory. It is recommended to have a different access and error log files for each server block.

Step 5. Accessing the Nginx.

Now, you can test your Nginx installation, by opening http://YOUR_IP_ADDRESS or http://YOUR_DOMAIN in your web browser. You should see the default Nginx welcome page.

That’s all you need to do to install Nginx on Ubuntu 20.04 Focal Fossa. I hope you find this quick tip helpful. Don’t forget to share your valuable queries/suggestions in the below comment box & also drop your worthwhile feedback.