Nginx is a web server that acts as the intermediary between the internet and the backend of the application infrastructure. The Web server accepts the request from the client on the internet and finds the requested resource as the response for the client. The Nginx server is extremely popular with high-traffic web applications as it can easily handle above 10k connections simultaneously. It also enables the traffic distribution like the load balancers to utilize the maximum capacity of the server.

This guide is going to explain the process of installing the Nginx server on the Debian 12 machine.

How to Install Nginx on Debian

To install the Nginx server on Debian, update the apt packages to install the latest version Nginx server from there. After installing the server, simply configure the firewall to connect to the server and use the IP address of the machine to access the landing page of Nginx. After that, configure the server by adding the HTML file or files of your application to load your website on the server.

To learn this process in detail, simply go through the following listed steps:

Step 1: Update apt Package

Start the process by updating and upgrading the apt packages to get the updated version of the Nginx server from the package manager. To do that, simply open the terminal on the Debian machine and execute the following command:

sudo apt update && sudo apt upgrade

Step 2: Install Nginx Server

After updating and upgrading the apt package, simply install the Nginx server by executing the “apt install nginx” command:

sudo apt install nginx

Running the above command asks the user to type “Y” from the keyboard to continue with the installation of the Nginx server:

After the installation of the Nginx server, start the services of the server using the following code:

sudo systemctl start nginx

After executing the above command, simply check the status of the Nginx server by running the “systemctl status nginx” command:

sudo systemctl status nginx

The following screenshot displays the Nginx services are running successfully and the server is in the active state:

Step 3: Set up the Firewall

After that, simply configure the Uncomplicated Firewall (UFW) settings so the connection can be established securely. Now, use the following command to get the list of all the application profiles available in the repository:

sudo ufw app list

The following screenshot displays the list of all the application profiles and you need to locate the “Nginx” server application as highlighted below. Locating these applications is required as we need to add them to the firewall to make the server secure:

You need to allow the internet traffic from the required ports to access the resources from the server securely. Now, add the “Nginx HTTP and HTTPS” ports(80 and 443) to the firewall using the following command:

sudo ufw allow 'Nginx HTTP'sudo ufw allow 'Nginx HTTPS'

After adding the rules on the UFW, simply enable the services of the UFW by running the following command:

sudo ufw enable

After that, check the status of the UFW to confirm the addition of the ports on the firewall:

sudo ufw status

Step 4: Access the Nginx Server

Now, check the status of the Nginx server to confirm that it is running or not by running the command written below:

systemctl status nginx

Now, you need to check the IP address of the machine to access the Nginx server on the internet by using the following command:

ip addr

Locate the IP address and the broadcast from the connection as displayed in the following snippet:

Now, type the IP address of the Debian machine on the web browser to get the landing page of the Nginx server:

Bonus Tip: Configure the Nginx Server

Now, you have installed the Nginx server on the Debian machine and accessed it using the IP address of the Debian 12. After that, you can configure the server by adding the web application files and resources to deploy them on the server. The server hosts the application files and runs them on the server to be accessed using the hosted website or IP address.

To learn the process in detail, go through the following steps:

Step 1: Check Configuration Files

Head inside the nginx directory from the Debian terminal using the cd command as mentioned below:

cd /etc/nginx

Inside the Nginx directory, simply execute the following command to get the list of all the files stored in it:

ls -1

The configuration files like “nginx.conf” and “sites-available” can be configured to host the application on the server:

Step 2: Create an HTML File in the Root Directory

Use the following command to open the “default” file from the “sites-available” directory to view its contents: 

nano /etc/nginx/sites-available/default

Remove all the comments from the file and it will look like the following screenshot and from there you can get the path of the root directory:

Create and open the file in the root directory using the following command:

nano /var/www/html/mypage.html

Copy the following content or your webpage code to host it on the nginx server and save the file using the “Ctrl + s” and “Ctrl + x” to get back to the terminal:

<!DOCTYPE html>
<html>
<head>
    <title>My Nginx Page</title>
</head>
<body>
    <h1>I'm showing my page using Nginx!</h1>
</body>
</html>

After saving the “mypage.html” page on the root directory, simply open the default page from the sites-available directory:

nano /etc/nginx/sites-available/default

Add the “mypage.html” page in the location section on the default page as displayed below:

Step 3: Access the HTML File on the Nginx Server

Now, head inside the site-available directory by giving the complete path in the cd command:

cd /etc/nginx/sites-available

After that, execute the following command to test the configuration file without running the server:

sudo nginx -t

The ok message means that the configurations are ok and the test is successful as displayed below:

After testing the server configurations, simply start the process by executing the followings command:

sudo nginx -s reload

After that, run the IP address on the web browser to get the contents of the application running on the Nginx server:

The above screenshot displays the content from the “mypage.html” file and it is running on the IP address from Debian. However, you can deploy a complete web application with all its files, code, or infrastructure and host it using the IP address. Now, if you have web hosting, you can host the application on your website and make it live for the audience on the internet.

That’s all about installing the Nginx server on Debian.

Conclusion

To install the Nginx server on Debian, simply install the Nginx server on Debian and configure its firewall to host it on the IP address. To configure the firewall, simply add the HTTP and HTTPS ports to the firewall and start the Nginx server to get its landing page on the IP address. After that, configure the server to add the web application files and host on your server using the IP address or the web hosting.