Nginx, an open-source web server, is well-known for its high-performance capabilities. It serves a diverse range of functions, such as functioning as a reverse proxy with HTTP server features, web hosting, load balancing, caching, media streaming, and more. Nginx's architecture is built on an event-driven and asynchronous framework, enabling it to process multiple requests concurrently. When compared to Apache, Nginx offers superior scalability and excels in distributing the load of numerous concurrent connections.
In this tutorial, we will demonstrate how to install Nginx on Ubuntu 22.04. After installing Nginx, we will also explore how to configure Nginx on Ubuntu. For this guide, we will run all commands on the Ubuntu 22.04 Jammy Jellyfish distribution.
Prerequisites
To install Nginx on the Ubuntu 22.04 system, you must have the following prerequisites:
- The Ubuntu 22.04 server should be running on your system or inside Virtualbox.
- You should have root or sudo privileges to run administrative commands.
- Make sure that no one process is running at ports 80 and 443.
Steps to Install Nginx on Ubuntu 22.04
To install and configure Nginx on Ubuntu 22.04, follow the below-given steps:
Step 1: Update Ubuntu Apt Repository
First, log in as a root user on your Ubuntu system and launch the terminal quickly on the desktop using the keyboard shortcut “Ctrl + Alt + t.” It is a recommended step to update the packages Index of your Linux system before installing new software on your system. So, update all Ubuntu repositories using the following command:
$ sudo apt update
Step 2: Install Nginx on Ubuntu 22.04
Nginx is available in the default Ubuntu apt repository. Therefore, you can easily install Nginx using the “apt” package manager on Ubuntu 22.04. To install Nginx on Ubuntu, run the following command on the terminal:
$ sudo apt install nginx
Once you start the installation process, a user confirmation prompt will appear on the terminal.
Press “y” and then hit “Enter” to proceed with the installation of all Nginx packages on your Ubuntu system. The installation will take a few seconds to complete on your system.
Step 3: Enable Firewall Settings
Once you complete the Nginx installation on Ubuntu 22.04, you will need to enable the firewall setting on your system. To enable traffic over port 80, it's essential that you configure your system's firewall settings.
Ubuntu Firewall (UFW) serves as the primary tool for managing firewall rules on Ubuntu. To open port 80 using UFW, follow the below-given commands. After implementing these commands, be sure to reload the rules to ensure that your changes become active.
$ sudo ufw allow "Nginx HTTP" or
$ sudo ufw allow 80
To verify that Nginx HTTPport 80 is enabled or allowed on your Ubuntu system, use the following command:
$ sudo ufw enable
$ sudo ufw status
Step 4: Managing Nginx Service on Ubuntu
After enabling the firewall setting on Ubuntu 22.04, you'll need to manage your Nginx service. This may involve tasks such as viewing the status, reloading, disabling, enabling, restarting, starting, or stopping the service to apply changes or perform maintenance. For managing the Nginx service, we'll use the “systemctl” command, which stands for “systemd control,” to manage the Nginx service.
To check the Nginx service status whether it is running on this system or not, use the following command:
$ sudo systemctl status nginx
As you can see in the screenshot above, the Nginx service is active and running on this Ubuntu system.
Similarly, you can stop the Nginx service on your Ubuntu system using the following commands:
$ sudo systemctl stop nginx
Again, check the Nginx service running status:
$ sudo systemctl status
After installing Nginx on your Ubuntu system, the Nginx service is enabled by default. But, if it is not enabled, you can easily enable it using the following command:
$ sudo systemctl enable nginx
To start the Nginx service, use this command:
$ sudo systemctl start nginx
If something goes wrong with your system, you can restart the Nginx service using:
$ sudo systemctl restart nginx
If you make changes to the configuration files, you can reload the Nginx service using the following command:
$ sudo systemctl reload nginx
If you want to disable the Nginx service, you can do it by using:
$ sudo systemctl disable nginx
Step 5: Test the Nginx Installation
Now, it's time to verify the Nginx installation. To do this, open your Firefox web browser on your system and enter "http://your_IP" into the browser's address bar. The result should display the Nginx welcome default page in your browser, as shown below:
Congratulations! The installation of the Nginx server on your Ubuntu 22.04 system is complete. You are now well-prepared to begin deploying your applications and can utilize Nginx as a web proxy server.
Configure Nginx: Host a Test Website on Ubuntu 22.04
Nginx, by default, serves its content from the "/var/www/html" directory. To access this directory, you can use the Linux "cd" command to change the directory and then utilize the "ls" command to list its contents.
$ cd /var/www/html
$ ls
You can open this default index file and see its content in the nano editor using the following command:
$ sudo nano /var/www/html/index.nginx-debain.html
Now, let’s create a custom “index.html” page for your test website. To create a default homepage, you can execute the "touch" command to generate an empty file named "index.html" within the "/var/www/html" directory. Once the file is created, you can proceed to edit it by executing the command "nano /var/www/html/index.html."
$ sudo nano /var/www/html/index.html
Paste the following content inside a <p> paragraph tag:
<p> Hello, welcome to test website </p>
Now, reload the Nginx service using the following command:
$ sudo systemctl reload nginx
Open your browser and enter your server IP address in the address bar. You will see the following message in your browser that indicates that your Nginx setup is configured and working properly on your system.
Configure Nginx Server Blocks
To configure the Nginx server block, follow the steps below:
Step 1: Unlink or Disable the Default Configuration
Disable the default file configuration to configure a server block for Nginx.
$ sudo unlink /etc/nginx/sites-enabled/default
Step 2: Create the Nginx Configuration File
Configure the Nginx file in which you will specify your website. In this instance, make sure to replace both the file name and the file's content with your website's domain. Repeat this process whenever you come across the placeholder "websitename.com."
$ sudo nano /etc/nginx/sites-available/websitename.com
In this example, we are using the domain name example.com. Replace it with your domain name.
Paste the following lines of code in this file:
server {
listen 80;
listen [::]:80;
server_name domain-name.com;
root /var/www/domain-name.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Step 3: Create a Symbolic Link
Link your Nginx file configuration:
$ sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
Step 4: Test the Nginx Configuration
Verify or test the Nginx configuration using the following command:
$ sudo nginx -t
Conclusion
In this article, you have learned how to install Nginx on the Ubuntu 22.04 distribution and how to configure firewall settings via the terminal. Nginx is a well-known and powerful tool, so if you want to get more insights into Nginx setup and commands, visit the official Nginx documentation. We hope that you found this article informative and valuable. If you encounter any issues or have questions about the Nginx installation, share your feedback or comments in the comment section. Your input is greatly appreciated.