Introduction
Nginx (pronounced Engine-X) is a lightweight, high-performance web server that is also commonly used as a reverse proxy, load balancer, and caching server. It is known for handling thousands of concurrent connections while using fewer resources compared to other web servers like Apache. Because of its scalability and speed, Nginx powers some of the busiest websites in the world, including services like Netflix, Dropbox, and WordPress.com.
In this tutorial, you will learn how to install and configure the Nginx webserver on Debian 12 (Bookworm).
Step 1 – Install Nginx
Nginx is available in the default Debian 12 package repositories, so installation is straightforward with the APT package manager.
1. Before installing, update your system’s package list to ensure you have the latest version available.
apt update -y
2. Run the following command to install Nginx.
apt install nginx -y
This will download and install the Nginx web server and its dependencies.
3. After installation, check the installed version of Nginx with:
nginx -v
Output.
nginx version: nginx/1.22.1
4. By default, Nginx starts automatically after installation. To confirm that it is working, open your browser and visit your server’s IP address.
http://YOUR-SERVER-IP
If Nginx is running properly, you will see the Nginx default welcome page, confirming that the installation was successful.
Step 2 – Manage the Nginx System Service
On Debian 12, Nginx runs as a systemd service, which means you can control it using the systemctl command. This allows you to enable, start, stop, restart, or check the status of the Nginx web server.
1. Enable Nginx to start after the system reboot.
systemctl enable nginx
2. Start the Nginx service.
systemctl start nginx
3. Verify that Nginx is running.
systemctl status nginx
Output.
● nginx.service - A high performance web server and a reverse proxy server Loaded: loaded (/lib/systemd/system/nginx.service; enabled; preset: enabled) Active: active (running) since Sat 2025-09-06 08:36:22 UTC; 8min ago Docs: man:nginx(8) Process: 16285 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 16286 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 16287 (nginx) Tasks: 3 (limit: 4642) Memory: 2.4M CPU: 18ms CGroup: /system.slice/nginx.service ├─16287 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;" ├─16288 "nginx: worker process" └─16289 "nginx: worker process"
4. Stop the Nginx service.
systemctl stop nginx
5. If you make configuration changes, restart Nginx to apply them.
systemctl restart nginx
Step 3 – Create a New Nginx Virtual Host
Nginx uses server blocks (similar to Apache virtual hosts) to host multiple websites on a single server. Each server block defines the domain name, root directory, and configuration for a specific website.
1. Create a configuration file for your domain (replace nginx.example.com with your actual domain).
nano /etc/nginx/conf.d/nginx.example.com.conf
Add the following content:
server { listen 80; listen [::]:80; server_name nginx.example.com; root /var/www/html/nginx.example.com; index index.html; location / { try_files $uri $uri/ =404; } }
Explanation:
server_name: The domain name for the site.
root: The directory where your website files will be stored.
index: The default page to serve.
location: Handles requests and returns a 404 if the file doesn’t exist.
2. Create the document root for your new site.
mkdir -p /var/www/html/nginx.example.com
3. Create an index.html file inside your document root.
nano /var/www/html/nginx.example.com/index.html
Insert the following content.
<html> <head></head> <body> <h1>Greetings from Atlantic.Net</h1> </body> </html>
The above HTML web application displays a Greetings from Atlantic.Net message when you access it in a web browser.
4. Change ownership of the web root to the Nginx user (www-data).
chown -R www-data:www-data /var/www/html/nginx.example.com
5. Check for syntax errors before restarting Nginx.
nginx -t
Output.
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
6. Restart the Nginx to apply the changes.
systemctl restart nginx
Now visit your domain on a web browser and verify that your web application displays.
http://nginx.example.com
You should see the message:
Greetings from Atlantic.Net
This confirms your virtual host is working correctly.
Step 4 – Secure the Nginx Webserver
By default, Nginx only serves websites over HTTP (port 80), which is not secure because traffic is unencrypted. To protect your website, you can use a free Let’s Encrypt SSL certificate that enables HTTPS (port 443).
1. Install Certbot and Nginx plugin.
apt install python3-certbot-nginx certbot -y
2. Request an SSL certificate for your domain (replace nginx.example.com with your domain name, and update the email).
certbot --nginx -d nginx.example.com -m [email protected] --agree-tos
Explanation.
–nginx: Automatically edits your Nginx config for SSL.
-d: Specifies the domain name.
-m: Email address for renewal notices.
–agree-tos: Accepts Let’s Encrypt terms of service.
If successful, Certbot will update your Nginx configuration and reload the service.
3. Let’s Encrypt certificates are valid for 90 days, but Certbot can renew them automatically. Test the renewal process with:
certbot renew --dry-run
This simulates renewal without making changes.
4. Restart Nginx to apply all SSL settings.
systemctl restart nginx
5. Open your browser and visit your domain using HTTPS.
https://nginx.example.com
If everything is configured correctly, you’ll see your test web page with a secure padlock icon in the browser address bar.
Conclusion
In this guide, you installed and configured the Nginx webserver on Debian 12. With this setup, your Debian 12 server is ready to host websites and web applications securely. You can now expand this configuration to support multiple domains, optimize performance, or integrate with backend applications.