Easy Steps To Install Nginx Web Server on Debian 12
This tutorial provides a straightforward guide to Install Nginx Web Server on Debian 12 Bookworm and configure Nginx server blocks. Nginx, a widely adopted open-source solution, is a powerful and versatile tool for web serving, load balancing, and reverse proxying. Debian 12 comes with version Nginx 1.22 pre-configured.
The following steps detail the installation process for Install Nginx Web Server on Debian 12 on your Orcacore server.
Before you begin, ensure you have:
- Access to your Debian 12 server as a non-root user with sudo privileges.
- A basic firewall set up. Refer to the Orcacore guide on Initial Server Setup with Debian 12 Bookworm for instructions.
- A domain name pointed to your server’s IP address.
Step 1 – Install Nginx from Debian 12 Command Line
First, update your system’s package list:
sudo apt update
Now, execute the following command in your terminal to Install Nginx Web Server on Debian 12:
sudo apt install nginx -y
Once the installation completes, proceed to the next step.
Step 2 – Adjust UFW Firewall for Nginx on Debian 12
Assuming you have enabled the UFW firewall, list the available application profiles:
sudo ufw app list
**<mark>Output</mark>**
Available applications:
...
Nginx Full
Nginx HTTP
Nginx HTTPS
...
Allow traffic on port 80 by enabling the ‘Nginx HTTP’ profile:
sudo ufw allow 'Nginx HTTP'
Reload the firewall to apply the changes:
sudo ufw reload
Verify the changes:
sudo ufw status
**<mark>Output</mark>**
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
Step 3 – Check Nginx Web Server Status
Verify that the Nginx service is active and running. This confirms your efforts to Install Nginx Web Server on Debian 12 was successful:
sudo systemctl status nginx
**<mark>Output</mark>**
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enable>
Active: **<mark>active</mark>** (**<mark>running</mark>**) since Sun 2023-06-18 02:30:54 EDT; 8min ago
Docs: man:nginx(8)
Process: 12630 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_proc>
Process: 12631 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (>
Main PID: 12657 (nginx)
Tasks: 3 (limit: 4631)
Memory: 2.3M
CPU: 38ms
CGroup: /system.slice/nginx.service
Access the Nginx default page by entering your server’s IP address in your web browser:
http://<mark>your-server-ip-address</mark>
Note: Retrieve your IP address using the command:
hostname -I
You should see the default Nginx welcome page:

This confirms that Nginx is running correctly on your Debian 12 server.
Step 4 – How To Manage Nginx Service from CLI?
Here are some essential Nginx service management commands:
Stop the web server:
sudo systemctl stop nginx
Start the web server:
sudo systemctl start nginx
Restart the web server:
sudo systemctl restart nginx
Reload the Nginx configuration:
sudo systemctl reload nginx
Disable Nginx from starting automatically at boot:
sudo systemctl disable nginx
Re-enable Nginx to start automatically at boot:
sudo systemctl enable nginx
Step 5 – How To Create Nginx Server Blocks on Debian 12?
Nginx server blocks, analogous to Apache virtual hosts, allow you to host multiple websites on a single server. Follow these steps to configure them:
- Create the document root directory:
sudo mkdir -p /var/www/<mark>example.com</mark>/html
- Assign ownership of the directory to your user:
sudo chown -R $USER:$USER /var/www/<mark>example.com</mark>/html
- Set appropriate permissions:
sudo chmod -R 755 /var/www/<mark>example.com</mark>
- Create a sample
index.html
file:
sudo vi /var/www/<mark>example.com</mark>/index.html
- Add the following HTML content to the file:
<html>
<head>
<title>Welcome to <mark>example.com</mark></title>
</head>
<body>
<h1>Success! Your Nginx server is successfully configured for <em><mark>example.com</mark></em>. </h1>
<p>This is a sample page.</p>
</body>
</html>
- Create a server block configuration file:
sudo vi /etc/nginx/sites-available/<mark>example.com</mark>
- Add the following configuration block to the file:
server {
listen 80;
listen [::]:80;
root /var/www/<mark>example.com</mark>/html;
index index.html index.htm index.nginx-debian.html;
server_name <mark>example.com</mark> www.<mark>example.com</mark>;
location / {
try_files $uri $uri/ =404;
}
}
- Create a symbolic link to enable the server block:
sudo ln -s /etc/nginx/sites-available/<mark>example.com</mark> /etc/nginx/sites-enabled/
- Adjust the
server_names_hash_bucket_size
innginx.conf
:
sudo vi /etc/nginx/nginx.conf
Find the "server_names_hash_bucket_size" line and uncomment it:
...
http {
...
server_names_hash_bucket_size 64;
...
}
...
- Test the Nginx configuration:
sudo nginx -t
**<mark>Output</mark>**
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
- Restart Nginx to apply the changes:
sudo systemctl restart nginx
Test Nginx Server Blocks
Access your domain name in a web browser:
http://<mark>example.com</mark>
You should see the content of your index.html
file:

For more in-depth information, refer to the Nginx Documentation.
Conclusion
You have successfully learned how to Install Nginx Web Server on Debian 12 Bookworm, configure the UFW firewall, manage the Nginx service, and set up Nginx server blocks (virtual host files).
You might also find these articles helpful:
Secure Apache Web Server with Let’s Encrypt on Debian 12
Install and Configure XRDP on Debian 12 Bookworm
Alternative Solutions for Installing Nginx on Debian 12
While the apt
package manager provides a reliable method for installing Nginx, alternative solutions exist, offering different levels of customization and control.
1. Installing Nginx from Source
This method provides the greatest flexibility and allows you to compile Nginx with specific modules and optimizations tailored to your server’s needs. It’s a more involved process than using apt
, but it can result in a more performant Nginx installation. This alternative method to Install Nginx Web Server on Debian 12 gives the best control.
Steps:
-
Install Dependencies: First, install the necessary build tools and libraries.
sudo apt update sudo apt install build-essential libpcre3-dev zlib1g-dev openssl libssl-dev libgd-dev libgeoip-dev
-
Download Nginx Source: Download the desired Nginx version from the official Nginx website (http://nginx.org/en/download.html). Use
wget
to download the source archive. Replace1.24.0
with the actual version number.wget http://nginx.org/download/nginx-1.24.0.tar.gz
-
Extract the Archive:
tar -xvzf nginx-1.24.0.tar.gz cd nginx-1.24.0
-
Configure the Build: Use the
./configure
script to customize the build process. You can specify the installation directory, enable/disable modules, and add custom modules. For example:./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_stub_status_module
--prefix=/usr/local/nginx
: Specifies the installation directory.--with-http_ssl_module
: Enables SSL support.--with-http_v2_module
: Enables HTTP/2 support.--with-http_gzip_static_module
: Enables pre-compressed file delivery.--with-http_stub_status_module
: Enables the status module for monitoring Nginx.
Consult the Nginx documentation for a complete list of configuration options.
-
Compile and Install:
make sudo make install
-
Create a Systemd Service File (Optional but Recommended): Create a service file to manage Nginx using
systemctl
. Create the file/etc/systemd/system/nginx.service
with the following content:[Unit] Description=The Nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
-
Enable and Start the Service:
sudo systemctl enable nginx sudo systemctl start nginx
-
Adjust Firewall: As with the
apt
installation, adjust your firewall (UFW) to allow HTTP (port 80) and HTTPS (port 443) traffic.
This method of Install Nginx Web Server on Debian 12 provides maximum control but requires a deeper understanding of Nginx configuration and dependencies.
2. Using Docker
Docker provides a containerized environment for running applications, including Nginx. This approach offers portability and isolation, simplifying deployment and management.
Steps:
-
Install Docker: If Docker is not already installed, install it using the following commands:
sudo apt update sudo apt install docker.io sudo systemctl start docker sudo systemctl enable docker
-
Pull the Nginx Image: Pull the official Nginx image from Docker Hub:
sudo docker pull nginx
-
Run the Nginx Container: Run the container, mapping port 80 on the host to port 80 in the container.
sudo docker run -d -p 80:80 nginx
-d
: Runs the container in detached mode (in the background).-p 80:80
: Maps port 80 on the host to port 80 in the container.
-
Configure Nginx (Optional): To configure Nginx within the container, you can mount a volume containing your configuration files. Create a directory on your host machine to store the configuration files:
mkdir ~/nginx-config cd ~/nginx-config
Create a basic
nginx.conf
file:events { worker_connections 1024; } http { server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.html; } }
Then, run the container with a volume mount:
sudo docker run -d -p 80:80 -v ~/nginx-config:/etc/nginx/conf.d nginx
This mounts the
~/nginx-config
directory on your host to/etc/nginx/conf.d
inside the container, allowing you to modify the configuration files on the host and have them reflected in the container. This approach to Install Nginx Web Server on Debian 12 offers the best portability. -
Adjust Firewall: As with the previous methods, ensure your firewall allows traffic on port 80.
These alternative methods offer different approaches to installing and managing Nginx on Debian 12. Choosing the right method depends on your specific requirements, technical expertise, and desired level of customization.