Nginx, often pronounced as “engine-x,” is a popular open-source web server and reverse proxy server that plays a crucial role in the world of web hosting and internet services. Its importance lies in its ability to efficiently handle incoming web requests, serve web pages and content, and distribute network traffic, making it an integral part of many websites and online applications.
Just like the importance of Nginx for other systems, it is equally effective for Linux, especially its Ubuntu distribution (Ubuntu is one of the leading Linux distributions), i.e., as it serves all the features in Ubuntu as well. In this descriptive guide, we will elaborate on the installation of Nginx on Ubuntu 22.04.
How to Install Nginx on Ubuntu 22.04?
The Nginx installation package is available on the default repository of Ubuntu 22.04. Thus, it can be obtained easily using the apt install command. Let’s learn how it works:
Step 1: Load the Updated Packages
If you haven’t updated the package list, dot it before getting the Nginx package. To do so, use the following command:
$ sudo apt update |
---|
Step 2: Install Nginx
Now, install the updated package of Nginx using the apt install command as follows:
$ sudo apt install nginx |
---|
Nginx is now installed. For more clarity, let’s check the installed version of the Nginx using the -v option of the nginx in the terminal as shown below:
$ nginx -v |
---|
The output shows that the version “nginx/1.18.0” is installed on the system.
How to Configure Nginx on Ubuntu 22.04?
After completing the installation of the Nginx, it is important to configure it on Ubuntu 22.04. The configuration of Nginx includes managing the firewall rules for various Nginx services and configuring the Nginx service to have smooth operations (related to Nginx). Let’s do them one by one.
Configure the Firewall Rules For Nginx
For any web-serving tool or tool where network traffic is required (or any involvement where the firewall is required.), a Firewall is as necessary as the package’s core files. So, here we will learn how you can configure firewall rules for the smooth functioning of Nginx on Ubuntu 22.04.
Note: It is recommended to execute the below-stated commands as a root user.
1: Allow the Traffic for Nginx
The users now need to allow the traffic for Nginx through the firewall. The permission can be granted using the “ufw allow” command in the following manner:
# ufw allow ‘nginx full’ |
---|
2: Reload the Firewall
Use the “ufw reload” to refresh and load the firewall rules:
# ufw reload |
---|
Configure the Nginx Service on Ubuntu 22.04
Nginx operations on Ubuntu 22.04 (or any other OS) depend on its service managed by the system. The services are managed from the backend, which allows you to start, stop, disable, or enable the specific tool’s operations on the system. Let’s see what are the most useful commands and their purpose:
1: Enable the Nginx Service
The enable option will keep the service in the active state even after the system reboot:
$ sudo systemctl enable nginx |
---|
2: Start the Nginx Service
$ sudo systemctl start nginx |
---|
3: Stop the Service
$ sudo systemctl stop nginx |
---|
4: Disable the Nginx Service
The users can disable the Nginx service and stop the system from automatic activation of the Nginx service on reboot. For this, use the command:
$ sudo systemctl disable nginx |
---|
How to Test Nginx on Ubuntu 22.04?
Once you have installed and configured the Nginx service on Ubuntu 22.04, you need to test the working of Nginx. Let’s see how it can be done:
Method 1: Using the Terminal
Let’s use the curl with the -i flag and the IP address of your server. In our case, the server’s IP address is “127.0.0.1”, so the command would be:
$ curl -i 127.0.0.1 |
---|
It is clear from the output that the Nginx server is configured and is displaying the welcome page in HTML.
Method 2: Using the Browser
Open the browser and use the HTTP protocol with your server’s IP in the browser’s address bar. The syntax is shown below:
http://server-ip |
---|
The welcome message for the Nginx ensures that the Nginx is now installed and configured properly as well.
How to Remove Nginx From Ubuntu 22.04?
The removal sometimes is the need of time. For instance, the acquired package is corrupted or is not functioning properly. In such a case, you must remove the installed package and get a new one. To remove Nginx from Ubuntu 22.04, the users need to run the command:
$ sudo apt autoremove nginx |
---|
Bottom Line
Nginx, as discussed, is a multi-functional tool offering web serving, reverse proxying, and tasks like traffic management. Keeping the importance of Nginx, we have prepared a guide to install Nginx on Ubuntu 22.04 (the current LTS of Ubuntu). To install it on Ubuntu 22.04, the users can run the command “sudo apt install nginx”.
Apart from only installation, this post has listed the potential measures to be carried out after the installation of Nginx, i.e., configuring traffic and Nginx service on Ubuntu 22.04. Moreover, users can use the command “sudo apt autoremove” if they want to remove it.
So, this post has covered the installation, configuration, and importance of Nginx on Ubuntu 22.04.