Nginx is one of the most popular and open-source web servers. It is also used as an HTTP cache, reverse proxy, and a load balancer. As compared to its counterpart Apache, it utilizes fewer resources and is able to scale on minimal hardware. In this post, we will be describing how to install Nginx on Ubuntu OS.
Note: The procedures described here have been tested on Ubuntu 20.04 LTS (Focal Fossa) system. You must have sudo privileges to install/uninstall Nginx on your system.
Steps to Install Nginx on Ubuntu
Nginx is available in the repositories of Ubuntu OS. Therefore, we can install it in Ubuntu simply by using the apt command.
Step 1: Update System Repository Index
Update the system repository index as follows:
$ sudo apt update
Enter the password for sudo.
Step 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
The following output shows the installed version of Nginx on our system is 1.18.0.
Step 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. Issue 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:
- Nginx Full (Allows port 80 and 443)
- Nginx HTTP (Allows port 80)
- Nginx HTTPS (Allows port 443)
Here, 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 following output confirms that the rule has been added to the firewall to allow HTTP traffic.
Step 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
The following output verifies that the Nginx service is actively running without any issues.
Another way to verify 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.
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 following default web page of the Nginx server.
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.
In case, you no longer need the Nginx server on your system, you can easily uninstall it from your system.
Issue 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
That is all there is to it! In today’s post, we have described how to install Nginx on the Ubuntu system. We have also described how to configure the firewall and manage Nginx services. In the end, we have described how to uninstall Nginx in case you no longer need it in your system.
Ummara Mushtaq is a Telecommunication engineer with two years of experience in server support and networking. She writes technical articles based on Linux system administration for LinuxWays.