Share your love
Simply Install Nginx Web Server on Debian 12
This tutorial intends to teach you to Simply Install Nginx Web Server on Debian 12 Bookworm and Create Nginx Server Blocks. As you know, Nginx is popular, open-source, free software that is used for web serving, load balancing, reverse proxying, etc. Debian 12 ships with default version Nginx 1.22.
Now follow the steps below to complete your Nginx installation on Debian 12 Bookworm.
Simply Learn To Install Nginx Web Server on Debian 12
To complete this guide, you must have access to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can visit the Initial Server Setup with Debian 12 Bookworm.
Also, you need a domain name that is pointed to your server’s IP address.
Step 1 – Install Nginx from Debian 12 Command Line
First, you must run the system update by using the following command:
sudo apt update
Now you can run the following command in your Linux terminal to install Nginx:
sudo apt install nginx -y
When your installation is completed, proceed to the next step.
Step 2 – Adjust UFW Firewall for Nginx on Debian 12
At this point, we assumed that you have enabled the UFW firewall. Now you can list available applications through the UFW firewall with the command below:
sudo ufw app list
Output
Available applications:
...
Nginx Full
Nginx HTTP
Nginx HTTPS
...
Here you just need to allow port 80. So use the Nginx HTTP through the UFW firewall:
sudo ufw allow 'Nginx HTTP'
Then, reload the firewall to apply the changes:
sudo ufw reload
Verify the change with the following command:
sudo ufw status
Output
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
At this point, you can verify that your Nginx service is active and running on Debian 12 by using the following command:
sudo 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: enable>
Active: active (running) 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
Also, you can check that your Nginx web server is working correctly by accessing the Nginx default page. To do this, you can type your server’s IP address in your web browser:
http://your-server-ip-address
Note: You can get your IP address with the command below:
hostname -I
You should see the following page:
When you see this screen, means that your Nginx web server is working correctly on Debian 12.
Step 4 – How To Manage Nginx Service from CLI?
At this point, you can see the following Nginx service management commands.
You can stop your web server with the following command:
sudo systemctl stop nginx
When it is stopped, you can start your web server again with:
sudo systemctl start nginx
To stop and start your Nginx on Debian 12 you can use the following command:
sudo systemctl restart nginx
If you make configuration changes you need to reload the Nginx on Debian 12. you can use the following command:
sudo systemctl reload nginx
Nginx is configured to start automatically when the server boots, by default. If you don’t want that to happen run the following command:
sudo systemctl disable nginx
To re-enable the service starts at boot, run the following command:
sudo systemctl enable nginx
Step 5 – How To Create Nginx Server Blocks on Debian 12?
Nginx server blocks are the same as Apache virtual host files. To set up Nginx Server blocks, you can follow the steps below:
- First, create a directory for your domain with the following command:
sudo mkdir -p /var/www/example.com/html
- Then, give the ownership of the directory with the $USER environmental variable with the following command:
sudo chown -R $USER:$USER /var/www/example.com/html
- Set the correct permissions by using the command below:
sudo chmod -R 755 /var/www/example.com
- Now you need to create a sample index.html page with your favorite text editor. Here we use the Vi text editor:
sudo vi /var/www/example.com/index.html
Add the following sample HTML inside your file:
<html>
<head>
<title>Welcome to example.com</title>
</head>
<body>
<h1>Success! Your Nginx server is successfully configured for <em>example.com</em>. </h1>
<p>This is a sample page.</p>
</body>
</html>
When you are done, save and close the file.
- You should create an Nginx server block file with the correct directives on Debian 12. To do this, run the following command:
sudo vi /etc/nginx/sites-available/example.com
Add the following configuration block to your file:
server {
listen 80;
listen [::]:80;
root /var/www/example.com/html;
index index.html index.htm index.nginx-debian.html;
server_name example.com www.example.com;
location / {
try_files $uri $uri/ =404;
}
}
When you are done, save and close the file.
- Now you need to enable the Nginx server block on Debian 12 with the following command:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
- To avoid a possible hash bucket memory problem that can arise from adding additional server names to your configuration, it is necessary to adjust a single value in the /etc/nginx/nginx.conf file. Open the file:
sudo vi /etc/nginx/nginx.conf
Then, find the “server_names_hash_bucket_size” line and uncomment it by removing the “#” symbol:
...
http {
...
server_names_hash_bucket_size 64;
...
}
...
When you are done. save and close the file.
- Here, you can test for configuration errors for Nginx on Debian 12. If you see the syntax ok in your output means it’s ok.
sudo nginx -t
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
- Then, restart the Nginx on Debian 12 with the following command:
sudo systemctl restart nginx
Test Nginx Server Blocks
Nginx should now be serving your domain name. You can test this by typing your domain name into your web browser:
http://example.com
You will see something like this:
For more information, you can visit the Nginx Documentation page.
Conclusion
At this point, you have learned to Simply Install the Nginx web server on Debian 12 Bookworm, Adjust the UFW firewall for Nginx, and Manage the Nginx web service. Also, you have learned to Nginx server blocks (virtual host files).
Hope you enjoy it. You may like these articles: