Namaste, I'm Avinash Sharma.

How to install nginx on ubuntu 20.04

published on 1/27/2023

How to Install NGINX on Ubuntu 20.04

What is Nginx?

NGINX is open-source software for web servers, reverse proxy, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability for web hosting. More than 75% of the website that is hosted online are hosted on Nginx and Apache Web Server.

Table of Contents


Step 1 – Update The Ubuntu server

Before Installing the Nginx web server we need to upgrade the operating system, By using the following command:

sudo apt-get update
sudo apt-get upgrade

Step 2 - Installing Nginx

sudo apt install nginx

Step 3 – Adjusting the Firewall

What is ufw?

The Uncomplicated Firewall (ufw), is a frontend for IPTables and is particularly well-suited for host-based firewalls for web servers. ufw provides a framework for managing Netfilter (Basically to filter out what incoming traffic is to be allowed) and a command-line (CLI) interface for manipulating the firewall.

You should get a listing of the application profiles that have the access to the external connections:

sudo ufw app list
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH

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

It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured (To have height secret ). Right now, we will only need to allow traffic on port 80 it's our wish that if we want to enable the HTTP or HTTPS to your website. In the future, we can add an SSL certificate in order to get the full advantage of the HTTPS protocol Heare is the link on how to enable the SSL certificate.

If not Enabled you can enable this by typing:

sudo ufw allow 'Nginx HTTP'

You can is the change by typing:

sudo ufw status
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)

Step 3 – Checking your Web Server is running or not:

systemctl status nginx

This command gives you the status of the Nginx server

Cheat Sheet for Nginx

Now that you have your web server up and running, let’s review some basic management commands (Cheat Sheet for Nginx).

To stop your Nginx web server, type:

sudo systemctl stop nginx

To start the Nginx web server when it is stopped, type:

sudo systemctl start nginx

To stop and then start (restart) the service again, type:

sudo systemctl restart nginx

If you are only making configuration changes in The Nginx config, Nginx can often reload without dropping connections, this is a great feature. To do this, type:

sudo systemctl reload nginx

By default, The Nginx server is configured to start automatically when the server boots. If that’s not what you want(to reduce the load on the server), you can disable this behavior by typing:

sudo systemctl disable nginx

To enable the service to start up at boot time, you can type:

sudo systemctl enable nginx

You have now learned basic management commands and should be ready to configure the site to host more than one domain 🎉.

Step 5 – Setting Up Server Blocks (Recommended)

When using the Nginx web server, server blocks (similar to virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain from a single server. We will set up a domain called your_domain, but you should replace this with your own domain name. To learn more about setting up a domain name with DigitalOcean, please refer to our Introduction to DigitalOcean DNS.

Nginx on Ubuntu 20.04 has one server block enabled by default that is configured to serve documents out of a directory at /var/www/html. While this works well for a single site, it can become unwieldy if you are hosting multiple sites. Instead of modifying /var/www/html, let’s create a directory structure within /var/www for our your_domain site, leaving /var/www/html in place as the default directory to be served if a client request doesn’t match any other sites.

Create the directory for your_domain as follows, using the -p flag to create any necessary parent directories:

sudo mkdir -p /var/www/your_domain/html

Next, assign ownership of the directory with the $USER environment variable:

sudo chown -R $USER:$USER /var/www/your_domain/html

The permissions of your web roots should be correct if you haven’t modified your umask value, which sets default file permissions. To ensure that your permissions are correct and allow the owner to read, write, and execute the files while granting only read and execute permissions to groups and others, you can input the following command:

sudo chmod -R 755 /var/www/your_domain

Next, create a sample index.html page using nano or your favorite editor:

vim /var/www/your_domain/html/index.html

Inside, add the following sample HTML:

/var/www/your_domain/html/index.html
<html>
<head>
<title>Welcome to your_domain!</title>
</head>
<body>
<h1>Success! The your_domain server block is working!</h1>
</body>
</html>

Save and close the file by typing CTRL and X then Y and ENTER when you are finished.

In order for Nginx to serve this content, 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/your_domain:

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

Paste in the following configuration block, which is similar to the default, but updated for our new directory and domain name:

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

Notice that we’ve updated the root configuration to our new directory, and the server_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:

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

Two server blocks are now enabled and configured to respond to requests based on their listen and server_name directives (you can read more about how Nginx processes these directives here):

your_domain: Will respond to requests for your_domain and www.your_domain. default: Will respond to any requests on port 80 that do not match the other two blocks. To avoid a possible hash bucket memory problem that can arise from adding additional server names, it is necessary to adjust a single value in the /etc/nginx/nginx.conf file. 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. If you are using nano, you can quickly search for words in the file by pressing CTRL and w.

/etc/nginx/nginx.conf
...
http {
...
server_names_hash_bucket_size 64;
...
}
...

Save and close the file when you are finished.

Next, test to make sure that there are no syntax errors in any of your Nginx files:

sudo nginx -t

If there aren’t any problems, restart Nginx to enable your changes:

sudo systemctl restart nginx

Nginx should now be serving your domain name. You can test this by navigating to http://your_domain, where you should see something like this.

Created with ❤️ by Avinash Sharma