Install Nginx on Ubuntu 20.04 - Step by Step process to implement it ?

Nginx is a free, open-source Linux application for web servers. It works as a reverse proxy server by directing web traffic to specific servers. 

Nginx is used for security and load-balancing, but can also function independently as a web server. 

Also, it used as an HTTP cache and a load balancer.

Basically, NGINX serves static content much faster than Apache. If you need to serve a lot of static content at high concurrency levels, NGINX can be a real help.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform Ubuntu related Software Installation tasks.

In this context, we shall look into how to install Nginx on Ubuntu OS.


How to Install Nginx on Ubuntu ?

To begin, You must have sudo privileges to install/uninstall Nginx on your system.

Nginx is available in the repositories of Ubuntu OS. 

Therefore, we can install it on Ubuntu simply by using the apt command. Follow the steps given below to start the installation process.


1. Update System Repository Index

Update the system repository index as follows:

$ sudo apt update

Enter the password for sudo.


2. Install Nginx

Then install Nginx as follows:

$ sudo apt install nginx

Now you might be prompted with the y/n option to carry on or terminate the installation procedure respectively. Hit y to carry on with the installation.

Once Nginx installation is completed, you check the installed version by running the following command in Terminal:

$ nginx -v

This will display the installed version of Nginx on our system.


3. Configure Firewall

If you have a UFW firewall enabled on your system, you will have to allow HTTP traffic through it.

First, check if the UFW firewall is enabled by running the following command in Terminal:

$ sudo ufw status

IF you see the active status, it means the firewall is enabled on your system.

Now we will check the available UFW profiles. Execute the following command to do so:

$ sudo ufw app list

This command will list the available UFW profiles on your system.

From the following output, you can see three Nginx profiles listed there:

i. Nginx Full (Allows port 80 and 443)

ii. Nginx HTTP (Allows port 80)

iii. Nginx HTTPS (Allows port 443)


In our case, we will allow the Nginx HTTP profile that will add a rule to allow traffic on port 80. The command is as follows:

$ sudo ufw allow 'Nginx HTTP'

Now to verify if the rule has been added, issue the following command:

$ sudo ufw status

The output will confirm that the rule has been added to the firewall to allow HTTP traffic.


4. Test Nginx

After the installation of Nginx, its service starts running automatically. If it does not start automatically, you can start it manually as follows:

$ sudo systemctl start nginx

To verify if the Nginx service is running, issue the following command in Terminal:

$ sudo systemctl status nginx

If everything is fine, then the output will verify that the Nginx service is actively running without any issues.


Alternatively, you can verify that the Nginx is working properly is by accessing its default web page. 

To access the default web page of Nginx, access the following address in any web browser:

http://ip-address

Make sure to replace the ip-address with the IP address of your system on which the Nginx server is installed. 

To find the IP address of your system, you can issue the following command:

$ ip a

If your Nginx server is working properly, you should see the default web page of the Nginx server.


How to manage Nginx on Ubuntu?

Some other commands that you might need in order to manage the Nginx service are as follows:

If you want to enable the Nginx service at boot, issue the below command in Terminal:

$ sudo systemctl enable nginx

Now each time you restart your system, the Nginx service will start automatically.

To stop the Nginx service, issue the below command in Terminal:

$ sudo systemctl stop nginx

If you make any configuration changes, you will have to restart the Nginx service. Issue the below command to do so:

$ sudo systemctl restart nginx

For more information about managing services, visit our post on how to manage services in Ubuntu .


Important Nginx File Locations

By default, Nginx stores different configuration and log files in the following locations:

1. /var/www/html – Website content as seen by visitors.

2. /etc/nginx – Location of the main Nginx application files.

3. /etc/nginx/nginx.conf – The main Nginx configuration file.

4. /etc/nginx/sites-available – List of all websites configured through Nginx.

5. /etc/nginx/sites-enabled – List of websites actively being served by Nginx.

6. /var/log/nginx/access.log – Access logs tracking every request to your server.

7. /var/log/ngins/error.log – A log of any errors generated in Nginx.


How to Uninstall Nginx from Ubuntu system ?

In case, you no longer need the Nginx server on your system, you can easily uninstall it from your system.

Execute the following command in Terminal to uninstall Nginx but without removing the configuration files:

$ sudo apt remove nginx

If you want to uninstall Nginx and also want to remove the configuration files, issue the following command in Terminal:

$ sudo apt purge nginx

Then to remove the additional packages that were installed as dependencies, issue the following command:

$ sudo apt autoremove


[Need urgent assistance to install missing packages on Ubuntu System? We are available to help you today. ]

This article covers how to install Nginx on the Ubuntu system. Also, you will learn how to configure the firewall and manage Nginx services. 

Finally, you will see how to uninstall Nginx in case you no longer need it in your system.

Nginx is a free, open-source Linux application for web servers. It works as a reverse proxy server by directing web traffic to specific servers.

Also, Nginx is used for security and load-balancing, but can also function independently as a web server.


To Install Nginx From Ubuntu Repositories:

1. Update Software Repositories

It is important to refresh the repository lists before installing new software. This helps make sure that the latest updates and patches are installed.

Open a terminal window and enter the following:

$ sudo apt-get update

2. Nginx is included in the Ubuntu 20.04 default repositories. Install it by entering the following command:

$ sudo apt-get install nginx


How to manage Nginx on Ubuntu ?

The behavior of Nginx can be adjusted. Use this to start or stop Nginx, or to enable or disable Nginx at boot.

Start by checking the status of the Nginx service:

$ sudo systemctl status nginx

If the status displays active (running), Nginx has already been started. 

Press CTRL+z to exit the status display.

If Nginx is not running, use the following command to launch the Nginx service:

$ sudo systemctl start nginx

To set Nginx to load when the system starts, enter the following:

$ sudo systemctl enable nginx

To stop the Nginx service, enter the following:

$ sudo systemctl stop nginx

To prevent Nginx from loading when the system boots:

$ sudo systemctl disable nginx

To reload the Nginx service (used to apply configuration changes):

$ sudo systemctl reload nginx

For a hard restart of Nginx:

$ sudo systemctl restart nginx

To Test the Configuration

$ sudo nginx –t

The system should report that the configuration file syntax is OK, and that the configuration file test is successful.

Related Posts