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.
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:
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