Phone :  +370 (5) 204-1903
Email :  sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. How To Install Nginx on Ubuntu 20.04

How To Install Nginx on Ubuntu 20.04

Nginx (which is pronounced as Engine-X) is an open-source web server that is extremely popular amongst the whole world. It is responsible for hosting some of the largest and highest-traffic web sites on the plane. It is often used as an entire web server, or as a reverse proxy or HTTP cache.

Ubnutu Tutorials Sep 15, 16 by admin 8 min Read
How To Install Nginx on Ubuntu 20.04

Since it’s free to use and comes with loads of powerful features, it’s a favourite amongst most web administrators. It is extremely lightweight software with powerful features packed with it.

In this tutorial, we will go through the process of installing Nginx on the Ubuntu 20.04 server. We will set up a basic site and make some configurations.

What you will need:

  • A user with Sudo privileges
  • A VPS (server) running with Ubuntu 20.04
  • Basic knowledge of Linux commands

Install Nginx

Nginx is directly available in Ubuntu’s default repositories and it is possible to install it directly from the repository. However, before we’re going to install it, we will update the local package index so that we will have the most recent package listings.

sudo apt update

sudo apt install nginx

Once updated, you can install Nginx. Now go to your browser and enter the server IP address, you will see this page. If you will see it, you have successfully installed Nginx on your Ubuntu server.

Configure the firewall

Before we proceed further, we will get some firewall configurations in order so that we can allow access to the service. If you don’t have ufw firewall active, you can skip this section. You can list the application configurations by,

sudo ufw app list

And you will see the listing as follows.

As demonstrated by the output, there are three profiles available for Nginx:

  1. Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  2. Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
  3. Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)

It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Right now, we will only need to allow traffic on port 80.

sudo ufw allow 'Nginx HTTP'

To verify the changes, enter the following command:

sudo ufw status

Now everything is running well, so let’s get familiar with some of the common commands that you can use the manage Nginx.

Managing Nginx

If you want to stop your web server:

sudo systemctl stop nginx

Now to start the server once again you can enter the following command:

sudo systemctl start nginx

If you would like to restart the server, enter the following command:

sudo systemctl restart nginx

If you would like to restart the server when you are making configuration changes without dropping the connection, enter the following command:

sudo systemctl reload nginx

By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behaviour by typing:

sudo systemctl disable nginx

To re-enable during start-up, enter:

sudo systemctl enable nginx

These commands are the most basic commands that you can use to manage the server!

Create your website

One of the top features of using an Nginx web server is the server block. This can be used to encapsulate configuration details and host more than one domain from a single server. By default Nginx in Ubuntu 20.04 has one server block enabled by default to serve documents out of the directory /var/www/html.

This will be perfect if you have a single website, however, it’s not recommended if you will host multiple websites. Therefore instead of using the above-mentioned directory, we can create a directory that matches your domain.

So let’s create a simple HTML page in /var/www/sample/ and let’s create a simple HTML file. Here instead of ‘sample’, you can enter whatever you want (suggestion: use your domain name).

Enter the directory:

cd /var/www

Create the sample directory and enter the directory:

sudo mkdir sample

cd sample

Now we will create a sample index.html file using the Nano text editor:

nano /var/www/sample/index.html

Add the following sample HTML text in the window:

<!doctype html>
   <meta charset="utf-8">
   <title>Hello, Nginx!</title>
   <h1>Hello, Nginx!</h1>
   <p>We have just configured our Nginx web server on Ubuntu Server!</p>

Don’t forget to save the file and exit it. To save the file press CTRL+X and Y and then press ENTER.

Setting up virtual hosts

Now we need to set up the virtual hosts. In order for Nginx to serve the content we created above, it’s necessary to create a server block with the correct directives. Instead of modifying the default configuration file directly, let’s make a new one at /etc/nginx/sites-available/sample:

sudo nano /etc/nginx/sites-available/sample

Now in the opened configuration file, copy and paste the following file. Don’t forget to change the name that you chose earlier (in this case we are using sample). The following configuration block is similar to the default, but updated for our new directory and domain name:

server {
       listen 80;
       listen [::]:80;
       root /var/www/sample;
       index index.html index.htm index.nginx-debian.html;
       server_name sample www.sample;
       location / {
               try_files $uri $uri/ =404;

Note that we’ve updated the root configuration to our new directory, and the servr_name to our domain name.

Next, let’s enable the file by creating a link from it to the sites-enabled directory, which Nginx reads from during startup. This will create two server blocks which are enabled and configured to respond to requests.

sudo ln -s /etc/nginx/sites-available/sample /etc/nginx/sites-enabled/

You might come across some problems when adding additional server names, therefore adjust a small value in the /etc/nginx/nginx.conf file. First open the file:

sudo nano /etc/nginx/nginx.conf

Find the server_names_hash_bucket_size directive and remove the # symbol to uncomment the line.

Save and close the file once done. Next, test to make sure that there are no syntax errors in any of your Nginx files:

sudo nginx -t

Activate virtual host

We’re ready with all the needed configurations, let’s go ahead and restart the Nginx that will enable the changes made:

sudo systemctl restart nginx

Test your Nginx server

Let’s check if everything works as it should. Open our newly created site in a web browser. Remember that we used:80 port.

If you will see an output similar to the one seen below, you have successfully configured and set up the Nginx Web server.


Now that you have your web server installed, you have many options for the type of content to serve and the technologies you want to use to create a richer experience.

If you will have any problem along the way, don’t hesitate to contact us and we will be happy to help you. If you liked this article, don’t forget to share it with your colleagues.


author img


Leave A Comment