How To Install Nginx on Debian 11

In this article, we want to teach you How to Install Nginx on Debian 11.

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, you can check our article about the Initial server setup with Debian 11.

Install Nginx on Debian 11

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

When your installation is completed you need to adjust the firewall.

Adjust the firewall for Nginx

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 that is up and running.

Check 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)
Tasks: 3 (limit: 2340)
Memory: 7.0M
CPU: 59ms
CGroup: /system.slice/nginx.service
├─5840 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─5841 nginx: worker process
└─5842 nginx: worker process

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:

Default Nginx landing page

Manage Nginx process 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.

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 instruction 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:

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. 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:

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:

Nginx server Debian 11

Conclusion

At this point, you learn How to Install Nginx on Debian 11. Also, you know about its management process and Nginx server blocks on Debian 11.

May this article about How to Install Apache on Debian 11 be useful for you, too.

Hope you enjoy it.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular