How to Install nginx on Debian 12?
Nginx, often pronounced as “Engine X,” is a powerful web server with additional functionalities like reverse proxy, load balancing, and caching. It can also handle HTTP requests and serve web pages efficiently. This is crucial for high-traffic applications as well as websites.
NGINX is famous for its low resource usage and efficiency. It deals with a vast number of concurrent(at the same time) connections without consuming excessive memory or CPU. Whether you’re setting up a personal website or managing a large-scale web application, Debian 12 is the suitable choice to install Nginx.
- How to Install Nginx on Debian 12?
- How to Configure Nginx on Debian 12?
- How to Create Nginx Server Blocks (Virtual Hosts)?
Here’s how you can install it.
How to Install Nginx on Debian 12?
NGINX supports cloud-native architectures, making it suitable for modernizing legacy applications or building new ones. To install Nginx on Debian 12, follow the below steps:
Step 1: Update Your System
Before installing Nginx, ensure your system is up-to-date:
sudo apt update
Step 2: Install Nginx
To install Nginx on Debian 12, utilize the “nginx” package with the “apt” command:
sudo apt install nginx
Step 3: Verify Installation
Users can confirm that Nginx is installed by checking its version with the help of the “version” utility as below:
sudo nginx -v
Optional: Check Nginx Services
Users can also check if Nginx is running or not via the “systemctl” command:
sudo systemctl status nginx
That is all from the installation.
Optional: Remove/Uninstall Nginx
To remove nginx completely from Debian, use the “autoremove” utility as below:
sudo apt autoremove nginx
Let’s head over to the configuration of the nginx server.
How to Configure Nginx on Debian 12?
NGINX is capable of working as a reverse proxy. It distributes/divides all incoming requests to the backend server (including web servers as well as application servers). This helps improve performance, security, and scalability.
To configure the UFW Firewall for Nginx on Debian 12, follow the stated steps:
Step 1: Install UFW
UFW (Uncomplicated Firewall) simplifies firewall management. Install it if not already done via the below command:
sudo apt install ufw
Step 2: Enable UFW
To start and enable UFW services, use the below command with the sudo privileges:
sudo ufw enable
Step 3: List Installed Applications
Now, users need to verify that Nginx is allowed through the firewall. It is possible through the “app list” utility as below:
sudo ufw app list
Step 4: Configure UFW Rules for Nginx
After verifying the “Nginx Full” and “Nginx HTTP” installed applications, users need to allow HTTP traffic (port 80):
sudo ufw allow 'Nginx HTTP'
Step 5: Verify Firewall Rules
Now, check the status of UFW rules via the “ufw” utility as below:
sudo ufw status
Nginx Web Server
Users can also verify the nginx web server by mentioning the server IP address (such as “127.0.0.1”) on the web browser as below:
It displays the home page of the Nginx web server.
Let’s head over to the creation of our own nginx server (for testing purposes).
How to Create Nginx Server Blocks (Virtual Hosts)?
NGINX provides features like SSL termination, DDoS protection, and access control. It helps secure your applications and data. Server blocks allow hosting multiple websites on a single Nginx instance. Here’s how to create one:
Step 1: Create a Directory for Your Domain
For instance, create an “nginx” subdirectory under the “/var/www” directory with sudo privileges:
sudo mkdir -p /var/www/nginx
Step 2: Assign Ownership to the Nginx Directory
Now, set the ownership permissions for the created directory “nginx” as below:
sudo chown -R $USER:$USER /var/www/nginx
Step 3: Create an Nginx Test HTML Page
Create a simple HTML file for testing purpose and named as “index.html”:
echo "<html><body><h1>Hello Linuxgenie Users</h1></body></html>" | sudo tee /var/www/nginx/index.html
Step 4: Create an Nginx Server Block
After that, users need to create a configuration file for your domain under the “/etc/nginx/sites-available” directory as below:
sudo nano /etc/nginx/sites-available/nginx
Add the following content (replace “nginx” with your actual domain):
server { listen 80; server_name nginx www.nginx; location / { root /var/www/nginx; index index.html; } }
Users can create a symbolic link to enable the Nginx server block via the below command:
sudo ln -s /etc/nginx/sites-available/nginx /etc/nginx/sites-enabled/
Optional: Change the default page to your own Website
Users can also change the default page to your own website by removing below comments (server block section) and add already created “index.html” file as below:
sudo nano /etc/nginx/sites-available/default
Step 5: Check Nginx Services (Configured)
In this step, users can check the configuration of nginx virtual host via the “nginx -t” utility:
sudo nginx -t
Note: If users find any error or failure, then need to follow the Step 4 or below Optional section. As well as check the HTTP traffic on port 80 (listening port).
Step 6: Reload Nginx Configuration
Finally, reload the nginx services using the “systemctl” command with the “sudo” privileges:
sudo systemctl reload nginx
Test Your Configuration
In the end, open a web browser and visit “http://your_domain or server_ip_address”. It displays the “Hello Linuxgenie Users” message:
Additional Nginx Commands & Tips
NGINX can serve as an API gateway, managing API requests, authentication, and rate limiting. To explore more commands and tips, use the stated features:
- Enhance file security in your web server.
- Set up Nginx security with Let’s Encrypt for free SSL certificates.
- Configure automatic certificate renewal.
- Manage Nginx server logs.
- Remove Nginx if needed.
- Configure log rotation parameters in Nginx
This is all from the installation and configuration of Nginx on Debian 12.
Conclusion
NGINX can balance traffic across multiple servers and prevent overload on any single server by ensuring even distribution. To Install nginx on Debian 12, update the local package index, and install the nginx package via “sudo apt install nginx”. Then, confirm the installation to proceed. Nginx service automatically starts after the installation process is complete. Users can start, stop, as well as restart Nginx via the “sudo systemctl start/stop/restart nginx” command.
To test Nginx open a web browser and navigate to “http://your_server_ip”. It sees the default Nginx welcome page. In addition, users can configure Nginx by editing the configuration files “/etc/nginx” directory.