Share your love
Install Nginx on Debian 11
In this article, we want to teach you How to Install Nginx Web Server on Debian 11. Also, you will learn to create the Nginx server blocks which are the same as the Apache virtual host files. NGINX is open-source software for web serving, reverse proxying, caching, load balancing, media streaming, and more.
Steps To Install Nginx Web Server on Debian 11
To install Nginx on Debian 11 you need some requirements first. Let’s see what we need.
Requirements
You need to log in as a non-root user with Sudo privileges into your Debian 11. Also, you need to set up a basic firewall. For this purpose, you can check our article about the Initial server setup with Debian 11.
You need a valid domain name that is pointed to your server’s IP address.
Step 1 – Install Nginx on Debian 11 from Terminal
Before installing Nginx on Debian 11, you need to update the apt packages because Nginx is available in the Debian default repository.
Run the following commands to update the packages on Debian 11:
# sudo apt update
# sudo apt upgrade
Now you can install Nginx on Debian 11 with the following command:
sudo apt install nginx -y
When your installation is completed, you need to adjust the firewall.
Step 2 – Adjust the UFW firewall for Nginx on Debian 11
The firewall software needs to be adjusted to allow access to the service.
We assume that you have installed the UFW firewall on Debian 11 which we mentioned at the beginning of the article.
List available applications that UFW knows how to work with them by the following command:
sudo ufw app list
In your output, you will see:
Output
Available applications:
...
Nginx Full
Nginx HTTP
Nginx HTTPS
...
In this article, you only need to allow traffic for port 80.
You can enable it with the following command:
sudo ufw allow 'Nginx HTTP'
Then verify the change with the following command:
sudo ufw status
You should see this in your output:
Output
Status: active
To Action From
-- ------ ----
OpenSSH ALLOW Anywhere
Nginx HTTP ALLOW Anywhere
OpenSSH (v6) ALLOW Anywhere (v6)
Nginx HTTP (v6) ALLOW Anywhere (v6)
At this point, you can check your web server which is up and running.
Step 3 – Check the Nginx Status Web Server
At the end of the installation process, Debian 11 starts Nginx.
To make sure that your service is up and running, run the following command:
systemctl status nginx
Output
nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Thu 2021-09-02 06:39:26 EDT; 2s ago
Docs: man:nginx(8)
Process: 5838 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 5839 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 5840 (nginx)
...
Now you can access your Nginx Debian 11 default page. You need your server’s IP address for this.
You can get your IP address with the command below:
hostname -I
Or you can use the curl command to get your IP address. First, install curl on Debian 11 with the following command:
sudo apt install curl
Then, run the command below:
curl -4 icanhazip.com
At this point, enter your server’s IP address into your web browser:
http://your_server_ip
Now you should see the default Nginx page on Debian 11:
Step 4 – Nginx Management Commands on Debian 11
Now you have your Nginx web server up and running. Let’s see some basic management commands for it.
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 11, you can use the following command:
sudo systemctl restart nginx
If you make configuration changes you need to reload the Nginx on Debian 11. 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
Here it is recommended to set up Nginx server blocks on Debian 11.
Step 5 – Set up Nginx server blocks on Debian 11
Nginx server blocks are similar to Apache virtual hosts. Server Blocks use the server_name and listen to directives to bind to TCP sockets.
Here you need your domain name to replace it with ours in the commands. Our domain name is nginx.orcacore.net.
To set up Nginx server blocks on Debian 11, follow the instructions below.
First, create a directory for your domain with the following command:
sudo mkdir -p /var/www/nginx.orcacore.net/html
Then, give the ownership of the directory with the $USER
environmental variable. Run the following command:
sudo chown -R $USER:$USER /var/www/nginx.orcacore.net/html
You need to be sure the permissions of your web roots should be correct. You can use the following command:
sudo chmod -R 755 /var/www/nginx.orcacore.net
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/nginx.orcacore.net/index.html
Add the following sample HTML inside your file:
<html>
<head>
<title>Welcome to nginx.orcacore.net</title>
</head>
<body>
<h1>Success! Your Nginx server is successfully configured for <em>ngin.orcacore.net</em>. </h1>
<p>This is a sample page.</p>
</body>
</html>
Save and close the file, when you are finished.
You should create an Nginx server block file with the correct directives on Debian 11. To do this, run the following command:
sudo vi /etc/nginx/sites-available/nginx.orcacore.net
Add the following configuration block to your file:
server {
listen 80;
listen [::]:80;
root /var/www/nginx.orcacore.net/html;
index index.html index.htm index.nginx-debian.html;
server_name nginx.orcacore.net www.nginx.orcacore.net;
location / {
try_files $uri $uri/ =404;
}
}
Save and close the file, when you are finished.
Now you need to enable the Nginx server block on Debian 11 with the following command:
sudo ln -s /etc/nginx/sites-available/nginx.orcacore.net /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 11. 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 11 with the following command to apply the changes:
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://nginx.orcacore.net
You will see something like this:
Where does Nginx install on Debian 11?
By default, NGINX will be installed in the /usr/local/nginx directory on Debian 11.
Where is the Nginx Configuration File located on Debian?
Nginx configuration files are located in the /etc/nginx directory. The main configuration file is located in the /etc/nginx/nginx.conf directory.
How do I know if Nginx is installed by Debian?
You can verify that the Nginx is installed on your Debian 11 server by checking its version:
nginx -v
Conclusion
At this point, you have learned to Install Nginx on Debian 11, Manage the Nginx from CLI, and create the Nginx Server blocks and test them. Hope you enjoy it.
This article about Installing Apache on Debian 11 may be useful for you.