How to Install Nginx web server on Debian 12 (Complete Guide)

Welcome to geekandnix, in this tutorial, I will show you how to install the Nginx web server on the Debian 12 server.

Nginx is a popular web server famous for its performance and the ability to handle high traffic. Here, you will learn how to install Nginx using different methods, set up server block a.k.a virtual host, manage Nginx service, and step-by-step debug the Nginx web server.

Without further ado, let’s begin!

Prerequisites

Before proceeding with this tutorial, make sure you have a Debian 12 server initialized with non-root as sudo privileges, and the UFW (Uncomplicated Firewall) installed. You also need a domain name, whether public or local domain (via hosts file).

See more: Initial Server Setup with Debian 12 (9 things todo)

Installing Nginx on Debian

Nginx (read as engine-x) is the most popular web server and is used by 34% of websites on the internet. It’s not only a web server but also can be used as a reverse proxy, load balancer, caching, and web acceleration for your applications.

On Debian, you can install Nginx in two different ways: via the Debian repository or the Nginx official repository.

Installing Nginx via Debian repository

By default, the Nginx package is available on the Debian repository and can be installed easily via APT. At this time, the Debian 12 repository provides Nginx 1.22.

To install the Nginx web server via the Debian repository, follow these steps:

1. First, run the command below to refresh your Debian repository and get the latest package list.

sudo apt update
Get:1 http://deb.debian.org/debian bookworm InRelease [151 kB]
Get:2 http://security.debian.org/debian-security bookworm-security InRelease [48.0 kB]
Get:3 http://deb.debian.org/debian bookworm-updates InRelease [55.4 kB]
Get:4 http://security.debian.org/debian-security bookworm-security/main Sources [84.3 kB]
Get:5 http://security.debian.org/debian-security bookworm-security/main amd64 Packages [146 kB]
Get:6 http://security.debian.org/debian-security bookworm-security/main Translation-en [87.7 kB]

2. Now, run the command below to install Nginx on your Debian server. Enter y to confirm with the installation.

sudo apt install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  nginx-common
Suggested packages:
  fcgiwrap nginx-doc
The following NEW packages will be installed:
  nginx nginx-common
0 upgraded, 2 newly installed, 0 to remove and 34 not upgraded.
Need to get 640 kB of archives.
After this operation, 1,696 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://deb.debian.org/debian bookworm/main amd64 nginx-common all 1.22.1-9 [112 kB]

3. Once Nginx is installed, run the following command to check the details of the Nginx version and its compile options.

sudo nginx -V

As you can see below, Nginx 1.22 is installed via the Debian 12 Bookworm repository.

nginx version: nginx/1.22.1
built with OpenSSL 3.0.8 7 Feb 2023 (running with OpenSSL 3.0.11 19 Sep 2023)
TLS SNI support enabled

Installing Nginx via official Nginx repository

An alternative way to install Nginx is by using the official Nginx repository, which offers two different Nginx versions, stable and mainline.

If you need Nginx’s latest version with the newest features and security updates, install the mainline version. Otherwise, you can install the stable version, which also provides the latest bug fixes/security updates, but not the latest features or experimental modules.

To get the latest version of the Nginx web server and install it via the official Nginx repository, complete these steps:

1. To start, run the following command to install dependencies for managing the repository.

sudo apt install -y curl gnupg2 ca-certificates lsb-release debian-archive-keyring
curl is already the newest version (7.88.1-10+deb12u5).
ca-certificates is already the newest version (20230311).
lsb-release is already the newest version (12.0-1).
lsb-release set to manually installed.
debian-archive-keyring is already the newest version (2023.3+deb12u1).
The following NEW packages will be installed:
  gnupg2
0 upgraded, 1 newly installed, 0 to remove and 34 not upgraded.
Need to get 445 kB of archives.
After this operation, 464 kB of additional disk space will be used.
Get:1 http://deb.debian.org/debian bookworm/main amd64 gnupg2 all 2.2.40-1.1 [445 kB]

2. Once dependencies are installed, run the command below to add the Nginx repository and GPG key to your system. Make sure to choose between mainline (latest version) or stable.

# adding GPG key
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

# Mainline Nginx repository
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/mainline/debian `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

# Stable Nginx repository
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/debian `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

3. Now, run the command below to set the top priority of the Nginx package via the official Nginx repository. The higher Pin-Priority number will be installed as the first option.

echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx

4. Next, run the following apt command to refresh the Debian package list and install Nginx on your machine.

sudo apt update && sudo apt install nginx

Input y to proceed with the installation.

Hit:1 http://security.debian.org/debian-security bookworm-security InRelease
Hit:2 http://deb.debian.org/debian bookworm InRelease
Hit:3 http://deb.debian.org/debian bookworm-updates InRelease
Get:4 http://nginx.org/packages/mainline/debian bookworm InRelease [2,866 B]
Get:5 http://nginx.org/packages/mainline/debian bookworm/nginx amd64 Packages [13.6 kB]
...
The following NEW packages will be installed:
  nginx
0 upgraded, 1 newly installed, 0 to remove and 34 not upgraded.
Need to get 1,011 kB of archives.
After this operation, 3,495 kB of additional disk space will be used.
Get:1 http://nginx.org/packages/mainline/debian bookworm/nginx amd64 nginx amd64 1.25.4-1~bookworm [1,011 kB]
Fetched 1,011 kB in 3s (394 kB/s)

5. Lastly, run the command below to verify your Nginx version.

sudo nginx -V

As displayed below, the Nginx 1.25.4 mainline is installed on the Debian server.

nginx version: nginx/1.25.4
built by gcc 12.2.0 (Debian 12.2.0-14)
built with OpenSSL 3.0.9 30 May 2023 (running with OpenSSL 3.0.11 19 Sep 2023)
TLS SNI support enabled

Allowing Nginx Traffic via UFW

Now that you’ve installed Nginx, you must open both HTTP and HTTPS ports on your server via UFW (Uncomplicated Firewall). This is to allow users to access your web application via the Nginx web server.

To allow Nginx traffic (HTTP and HTTPS) via UFW (Uncomplicated Firewall):

1. First, run the command below to check the detailed information of the WWW Full application profile.

sudo ufw app info 'WWW Full'

As you can see below, the WWW Full profile will open both HTTP and HTTPS ports (80 and 443) on your system.

Profile: WWW Full
Title: Web Server (HTTP,HTTPS)
Description: Web Server (HTTP,HTTPS)

Ports:
  80,443/tcp

2. Now, run the ufw command below to enable the WWW Full profile. Once added, you will see an output Rule added.

sudo ufw allow 'WWW Full'
Rule added
Rule added (v6)

3. Next, run the command below to verify the list-enabled rules on UFW.

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
WWW Full                   ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
WWW Full (v6)              ALLOW       Anywhere (v6)

4. Lastly, visit your server IP address http://SERVER-IP/ and you should get the Nginx default index.html page.

Accessing Nginx default index.html page
Accessing Nginx default index.html page

Setting up Nginx server block (a.k.a virtual host)

With the Nginx traffic allowed, let’s move on to create and set up the Nginx server block.

The term server block in Nginx is the same as a virtual host in Apache. It’s the process of hosting multiple websites or domain names such as test.com and test2.com on a single machine.

To create a Nginx server block, ensure you have prepared the root directory where your application will be stored, a domain name (local domain or public), and a path to Nginx log files for access and error logs.

In this example, you will create the Nginx server block below:

Domain NameDocument RootLog Path
testsite.com/var/www/testsite/var/log/nginx/testsite-access.log
/var/log/nginx/testsite-error.log

To create a Nginx server block a.k.a virtual host, carry out the following:

1. First, run the command below to create a new DocumentRoot directory /var/www/testsite and the index.html page.

mkdir -p /var/www/testsite
echo '<h1>Geekandnix - Welcome to testsite.com</h1>' > /var/www/testsite/index.html

2. Then, run the chown command below to change the ownership of the /var/www/testsite directory to the user www-data. This also allows Nginx to access (read and write) the document root directory /var/www/testsite.

sudo chown -R www-data:www-data /var/www/testsite

3. Now, create a new Nginx server block configuration /etc/nginx/sites-available/testsite.conf using vim.

# For Nginx via Debian repository
sudo vim /etc/nginx/sites-available/testsite.conf

# For Nginx via official Nginx repository
sudo vim /etc/nginx/conf.d/testsite.conf

Insert the configuration below. Make sure to change the details of server_name, root path, and log files path with your information.

server {
        listen 80;

        # web root directory and default index
        root /var/www/testsite;
        index index.html index.htm index.nginx-debian.html;

        # domain name
        server_name testsite.com;

        location / {
                # First attempt to serve request as a file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }
        
        access_log /var/log/nginx/testsite-access.log;
        error_log /var/log/nginx/testsite-error.log;
}

When done, save and exit the file.

4. Next, run the command below to activate the server block file testsite.conf. With this, you will create a symlink file testsite.conf to the /etc/nginx/sites-enabled/ directory.

sudo ln -s /etc/nginx/sites-available/testsite.conf /etc/nginx/sites-enabled/

5. After the Nginx server block is activated, run the command below to check your Nginx syntax.

sudo nginx -t

The output syntax is ok ... test is successful indicates that you have correct and proper Nginx syntax.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

6. Now, run the following systemctl command to restart the Nginx service and apply a new server block configuration.

sudo systemctl restart nginx

7. Lastly, back to your web browser and visit your domain name, such as http://testsite.com/.

If your server block configuration is successful, you will see the default index.html page like the following:

Setting up Nginx server block a.k.a virtual host
Setting up Nginx server block a.k.a virtual host

Managing Nginx service on Debian

After configuring the server block, you must ensure that the nginx web server is running. So here you will manage Nginx via the systemctl command line.

Proceed with the following to manage the Nginx service on the Debian server:

1. To start the nginx service, run the systemctl command below.

sudo systemctl start nginx

2. Once started, run the command below to add the Nginx service to the system startup.

sudo systemctl enable nginx

3. Now, run the command below to verify the Nginx web server. If running, you will see an output such as active (running).

sudo systemctl status nginx
● nginx.service - nginx - high-performance web server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enabled)
     Active: active (running) since ; 1min 47s ago
       Docs: https://nginx.org/en/docs/
    Process: 4240 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
   Main PID: 4241 (nginx)
      Tasks: 3 (limit: 2307)
     Memory: 2.9M
        CPU: 23ms
     CGroup: /system.slice/nginx.service

4. Additionally, run the ss command below to verify the Nginx service via port. If Nginx running, you will see port 80 or 443 are used in the nginx process.

ss -tulpn | grep nginx
tcp   LISTEN 0      511          0.0.0.0:80         0.0.0.0:*    users:(("nginx",pid=4243,fd=6),("nginx",pid=4242,fd=6),("nginx",pid=4241,fd=6))

5. If you need to stop the Nginx web server, run the command below.

sudo systemctl stop nginx

6. Lastly, run the following command to restart the Nginx service. Mostly, needed after making changes to Nginx configurations.

sudo systemctl restart nginx

Debugging Nginx installation

As an administrator, debugging skills are a must, because you most likely get errors in your process. In this sequence, you’ll learn how to debug the Nginx web server, from checking the Nginx service and syntax to checking logs using tail or journalctl commands.

Follow these sequences to debug the Nginx web server:

1. Firstly, run the command below to verify your Nginx configuration. With Nginx, sometimes you have an error because a typo or missing ; at the end of the line.

sudo nginx -t

2. Now, run the command below to verify the status of the Nginx service. If Nginx is running, proceed to check your website. If stopped, check the Nginx error log file.

sudo systemctl status nginx

3. Additionally, run the command below to verify the Nginx web server via the open port on your system. If Nginx running, the default port 80 or 443 will be used by the nginx process.

ss -tulpn | grep nginx

4. Now, run the curl command below to verify the HTTP status code of your domain. Run this after ensuring that you have proper Nginx syntax and that Nginx is running.

  • If you have output 200 OK, that means your domain is fine.
  • If you get an output such as 500 Internal Server Error or 403 Forbidden, check the details error in the Nginx error log.
curl -I http://testsite.com/
HTTP/1.1 200 OK
Server: nginx/1.25.4
Date: Sat, 16 Mar 2024 07:01:30 CET
Content-Type: text/html
Content-Length: 46
Last-Modified: Sat, 16 Mar 2024 06:06:31 CET
Connection: keep-alive
ETag: "65f536e7-2e"
Accept-Ranges: bytes

5. If you’re using rsyslog, check Nginx logs via the cat or tail command below.

cat /var/log/nginx/testsite-error.log
tail f- /var/log/nginx/testsite-error.log

6. Additionally, you can also verify the log of the Nginx service via the systemd journalctl command below.

journalctl -u nginx
journalctl -t -u nginx

Conclusion

Now that you have installed the Nginx web server on Debian 12 server. Also, you created a Nginx server block, and managed the Nginx service via the command line, then you also learned how to debug Nginx if a problem occurred.

Following this tutorial, you may also need to install other software, as part of the LEMP Stack (Linux, Nginx, MySQL/MariaDB, PHP/Python/Perl). Check our guide for those:

System administrator and devops enthusiast, leveraging over 10+ years of Linux expertise to optimize operations. Proficient in FreeBSD, VMWare, KVM, Proxmox, PfSense, Ansible, Docker, and Kubernetes.

Read Also: