Learn How to Install Nginx on Debian 12 Linux

Choose a different version or distribution

Introduction

Before we begin talking about how to install Nginx on Debian 12 Linux, let's briefly understand – What is Nginx?

Nginx is a popular web server and reverse proxy server that powers millions of websites worldwide. It efficiently handles high traffic loads and improves website performance.

Nginx is known for its speed, reliability, and scalability, making it a top choice for many businesses. It also offers advanced features like load balancing, caching, and SSL/TLS encryption. With Nginx, websites can deliver content quickly and securely, providing an optimal user experience.

In this tutorial, we will discuss how to install Nginx on Debian 12 Linux. We will also address a few FAQs on how to install Nginx on Debian 12 Linux.

Advantages of Nginx

  1. Speed: Nginx is renowned for its exceptional speed and performance, handling high traffic loads efficiently.
  2. Reliability: With its robust architecture, Nginx ensures reliable and stable operation, even under heavy workloads.
  3. Scalability: Nginx easily scales to accommodate growing website traffic and handle concurrent connections effectively.
  4. Load balancing: It provides advanced load balancing capabilities, distributing traffic across multiple servers for optimal performance.
  5. Caching: Nginx offers efficient caching mechanisms, reducing server load and improving response times for frequently accessed content.

Install Nginx

The default Debian Buster repositories contain the Nginx package.

To complete the installation, run the following commands as root, or as a user with sudo privileges:

sudo apt update
sudo apt install nginx

The Nginx service will launch automatically after the installation. As demonstrated below, you may use curl to confirm installation:

curl -I 127.0.0.1

You will get an output like below:

Output

HTTP/1.1 200 OK
Server: nginx/1.14.2
Date: Tue, 16 Jul 2019 16:50:46 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 16 Jul 2019 16:50:26 GMT
Connection: keep-alive
ETag: "5d2e0052-264"
Accept-Ranges: bytes

The Nginx service can be managed using the systemctl command, just like any other systemd unit.

Adjust the Firewall

By enabling the 'Nginx Full' profile, UFW users can open HTTP (80) and HTTPS (443) ports:

sudo ufw allow 'Nginx Full'

If your system uses nftables to filter connections, run the command below to open the required ports:

nft add rule inet filter input tcp dport {80, 443} ct state new,established counter accept

Nginx Configuration File’s Structure and Best Practices

  • The /etc/nginx directory contains the Nginx configuration files.
  • / The primary Nginx configuration file is etc/nginx/nginx.conf.
  • The /etc/nginx/sites-available directory stores the server block (vhost) configuration files. To use these configuration files, they must be connected to the /etc/nginx/sites-enabled directory.
  • To enable server blocks, create a symbolic link (a pointer) from the configuration file in the sites-available directory to the sites-enabled directory.
  • It is a good idea to adhere to a standard naming convention when writing code for easier maintenance. For example, the configuration file should be named /etc/nginx/sites-available/mydomain.com.conf if your domain name is mydomain.com.
  • Configuration snippets that can be used in the server block files can be found in the /etc/nginx/snippets directory. You can refactor repeatable configuration segments into snippets and include the snippet file in the server block if they are used repeatedly.
  • The /var/log/nginx/ directory contains the Nginx log files - access.log and error.log. It is suggested that each server block have its own separate access and error log files.
  • You can adjust your domain's document root directory to any location you prefer. Webroot is most commonly found in:
  • /home/<user_name>/<site_name>
  • /var/www/<site_name>
  • /var/www/html/<site_name>
  • /opt/<site_name>

FAQs to Install Nginx on Debian 12 Linux

Where is the Nginx configuration file located in Debian 12?

The main Nginx configuration file is located at /etc/nginx/nginx.conf in Debian 12.

How do I start, stop, or restart Nginx on Debian 12?

To start Nginx, use the command: sudo systemctl start nginx. For stopping or restarting, you can use sudo systemctl stop nginx or sudo systemctl restart nginx.

How can I check if Nginx is running on Debian 12?

To check the status of Nginx, run the command: sudo systemctl status nginx.

Where are the Nginx website files stored in Debian 12?

The default directory for Nginx website files in Debian 12 is /var/www/html/.

How do I configure virtual hosts in Nginx on Debian 12?

You can create separate server blocks in the Nginx configuration file, located in /etc/nginx/nginx.conf, to configure virtual hosts.

How do I test the Nginx configuration for errors in Debian 12?

Use the command sudo nginx -t to test the Nginx configuration for any syntax errors or other issues.

How can I uninstall Nginx from Debian 12?

To uninstall Nginx from Debian 12, run the command: sudo apt remove nginx.

Conclusion

We hope this detailed tutorial helped you to install Nginx on Debian 12.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.