Advance

How to install Nginx on Debian 10

How to install Nginx on Debian 10

In the following of Nginx tutorials, in this article, we will learn you How to install Nginx on Debian 10. But first, what is the Nginx basically? Nginx is one of the most popular web servers in the world and responsible for hosting some of the largest and highest-traffic sites on the internet. It is more resource-friendly than Apache in most cases and can be used as a web server or reverse proxy.

 

Prerequisites

The tutorial may be more useful if you know:

 

Recommended Article: How to Setup and Configure UrBackup on Ubuntu 20.10

How to install Nginx on Debian 10

Let’s walk through the steps of this tutorial and discuss for installing Nginx on Debian 10.

Do not miss related articles:

Tutorial installation Nginx on CentOS 8

Tutorial WordPress installation with Nginx in Ubuntu 20.04

How to secure Nginx with Let’s Encrypt on Ubuntu 20.04

1- Installing Nginx

Since Debian’s default repositories already the Nginx, it is possible to install it from these repositories using the apt packaging system.

So, because this is your first interaction with the apt packaging system in this session, let’s first update our local package index so that we have access to the most recent package listings:

sudo apt update

Now, you can install Nginx:

sudo apt install nginx

Then Hit ENTER to proceed when you are asked. After that, apt will install Nginx and any required dependencies to your server.

 

2- Adjusting the Firewall

Due to firewall software requirements, you need to adjust it to allow access to the service before testing Nginx.

List the application configurations that ufw knows how to work with by typing:

sudo ufw app list
Output
Available applications:  ...    Nginx Full    Nginx HTTP    Nginx HTTPS  ...

As you would see, there are three profiles available for Nginx:

  • Nginx Full: This profile opens both port 80 (normal, unencrypted web traffic) and port 443 (TLS/SSL encrypted traffic)
  • Nginx HTTP: This profile opens only port 80 (normal, unencrypted web traffic)
  • Nginx HTTPS: This profile opens only port 443 (TLS/SSL encrypted traffic)

 

Note: It is recommended that you enable the most restrictive profile that will still allow the traffic you’ve configured. Since you haven’t configured SSL for our server yet in this guide, you will only need to allow traffic for HTTP on port 80.

You can enable this by typing:

sudo ufw allow 'Nginx HTTP

And to verify the changes:

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)

 

buy a virtual machine with bitcoin

 

3- Checking your Web Server

Once the installation is completed, Debian 10 starts Nginx. The web server should already be up and running.

And you can check with the systemd init system to make sure the service is running by typing:

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 Wed 2019-07-03 12:52:54 UTC; 4min 23s ago       Docs: man:nginx(8)   Main PID: 3942 (nginx)      Tasks: 3 (limit: 4719)     Memory: 6.1M     CGroup: /system.slice/nginx.service             ├─3942 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;             ├─3943 nginx: worker process             └─3944 nginx: worker process

By receiving the above output, it means the service appears to have started successfully. However, the best way to test this is to actually request a page from Nginx.

You can access the default Nginx landing page to confirm that the software is running properly by navigating to your server’s IP address. If you do not know your server’s IP address, try typing this at your server’s command prompt:

ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

You will get back a few lines. You can try each in your web browser to see if they work.

When you have your server’s IP address, enter it into your browser’s address bar:

http://your_server_ip

Then, the default Nginx landing page should display.

default Nginx landing page

By this page, you will see that the server is running correctly.

4- Managing the Nginx Process

Since the webserver is up and running, you can review some basic management commands.

Use the command below to stop your web server.

sudo systemctl stop nginx

Also, you can start the web server when it is stopped:

sudo systemctl start nginx

And to stop and then start the service again:

sudo systemctl restart nginx

If you are simply making configuration changes, Nginx can often reload without dropping connections. To do this, type:

sudo systemctl reload nginx

By default, Nginx is configured to start automatically when the server boots. If this is not what you want, you can disable this behavior by typing:

sudo systemctl disable nginx

To re-enable the service to start up at boot, you can type:

sudo systemctl enable nginx

 

5- Setting Up Server Blocks

To know more about setting up server blocks, let us explain this way when using the Nginx web server, server blocks can be used to encapsulate configuration details and host more than one domain on a single server. You will set up a domain called your_domain. To learn more about setting up a domain name with DigitalOcean, see our introduction to DigitalOcean DNS.

Nginx on Debian 10 has one server block enabled by default that is configured to serve documents out of a directory at /var/www/html. While this works well for a single site, it can become unmanageable if you are hosting multiple sites. Instead of modifying /var/www/html, let’s create a directory structure within /var/www for the your_domain website, leaving /var/www/html in place as the default directory to be served if a client request doesn’t match any other sites.

Create the directory for your_domain as follows, using the -p flag to create any necessary parent directories:

sudo mkdir -p /var/www/your_domain/html

Now, assign ownership of the directory with the $USER environment variable, which should reference your current system user:

sudo chown -R $USER:$USER /var/www/your_domain/html

The permissions of your webroot should be correct if you haven’t modified your umask value, but you can make sure by typing:

sudo chmod -R 755 /var/www/your_domain

Then, create a sample index.html page using nano or your favorite editor:

nano /var/www/your_domain/html/index.html

Add the following sample HTML inside:

/var/www/your_domain/html/index.html
<html>      <head>          <title>Welcome to your_domain</title>      </head>      <body>          <h1>Success! Your Nginx server is successfully configured for <em>your_domain</em>. </h1>  <p>This is a sample page.</p>      </body>  </html>

Now you can save and close the file after finishing.

You need to create a server block with the correct directives that point to our custom webroot to serve this content for Nginx. Instead of modifying the default configuration file directly, let’s make a new one at /etc/nginx/sites-available/your_domain:

sudo nano /etc/nginx/sites-available/your_domain

Paste in the following configuration block, which is similar to the default, but updated for your new directory and domain name:

/etc/nginx/sites-available/your_domain
server {          listen 80;          listen [::]:80;            root /var/www/your_domain/html;          index index.html index.htm index.nginx-debian.html;            server_name your_domain www.your_domain;            location / {                  try_files $uri $uri/ =404;          }  }

Please note that you have updated the root configuration to our new directory, and the server_name to our domain name.

Next, let’s enable this server block by creating a symbolic link to our custom configuration file inside the sites-enabled directory, which Nginx reads from during startup.

sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

Two server blocks are now enabled and configured to respond to requests based on their listen and server_name directives.

  • your_domain: Will respond to requests for your_domain and www.your_domain.
  • default: Will respond to any requests on port 80 that do not match the other two blocks.

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 nano /etc/nginx/nginx.conf

Now, find the server_names_hash_bucket_size directive and remove the # symbol to uncomment the line:

/etc/nginx/nginx.conf
...  http {      ...      server_names_hash_bucket_size 64;      ...  }  ...

Now you can save and close the file after finishing.

You can also test to make sure that there are no syntax errors in any of your Nginx files:

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

Restart Nginx to enable your changes, when you passed the configuration test.

sudo systemctl restart nginx

Please be aware that Nginx should now be serving your domain name. You can test this by navigating to http://your_domain, where you should see something like this:

the serverblock of Nginx

6- Getting Familiar with Important Nginx Files and Directories

Since you know how to manage the Nginx service itself, you should take a few minutes to familiarize yourself with a few important directories and files.

Content

  • /var/www/html: The actual web content, which by default only consists of the default Nginx page you saw earlier, is served out of the /var/www/html directory. You can change it by altering Nginx configuration files.

Server Configuration

  • /etc/nginx: The Nginx configuration directory. All of the Nginx configuration files reside here.
  • /etc/nginx/nginx.conf: The main Nginx configuration file. You can modify it to make changes to the Nginx global configuration.
  • /etc/nginx/sites-available/: The directory where per-site server blocks can be stored. Nginx will not use the configuration files found in this directory unless they are linked to the sites-enabled directory. Typically, all server block configuration is done in this directory and then enabled by linking to the other directory.
  • /etc/nginx/sites-enabled/: The directory where enabled per-site server blocks are stored. Typically, these are created by linking to configuration files found in the sites-available directory.
  • /etc/nginx/snippets: This directory contains configuration fragments that can be included elsewhere in the Nginx configuration. Potentially repeatable configuration segments are good candidates for refactoring into snippets.

Server Logs

  • /var/log/nginx/access.log: Every request to your web server is recorded in this log file unless Nginx is configured to do otherwise.
  • /var/log/nginx/error.log: Any Nginx errors will be recorded in this log.
Recommended Article: How to install Nginx on Debian 10

Good job! By touching this point, you have successfully finished this guide of How to install Nginx on Debian 10. and you have your web server installed now, you have many options for the type of content you can serve and the technologies you can use to create a richer experience for your users.

We Are Waiting for your valuable comments and you can be sure that it will be answered in the shortest possible time.

Leave Your Comment

Your email address will not be published.

We are by your side every step of the way

Think about developing your online business; We will protect it compassionately

We are by your side every step of the way

+18054214518

7 days a week, 24 hours a day