Configure Nginx Server Blocks on Debian 9 - How to do it ?

With Nginx Server Blocks, you can easily run more than one website on a single machine. You can specify the separate document root for each domain. In addition, you also can create a separate security policy for each site and use different SSL certificates for each site and much more.

Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Nginx queries.

In this context, we shall look into how to configure Nginx Server Blocks on a Debian 9.


How to Create Nginx Server Blocks On Debian ?

Before proceeding with this task, ensure that the following requirements are met:


1. Create the Directory Structure

To begin, we will create a directory structure to store the site data and serve response to visitor's requests.

The top level directory is considered as DocumentRoot directory. You can set any location as document root as per your choice but it is best practice to define in directory structure. 

So we will store all document root at /var/www directory: 

/var/www/
 ├── example1.com
 │   └── public_html
 ├── example2.com
 │   └── public_html

We will create separate directory for each domain inside /var/www directory. 

Within this directory, we'll create a public_html directory as domain document root directory to store website data:

$ sudo mkdir -p /var/www/example.com/public_html

After that, we will create a index.html file within the domain document root directory for testing purpose. By default, this page will display when visitors visit your website.

You can create a new index.html file using your favorite text editor type:

$ sudo nano /var/www/example.com/public_html/index.html

Add the following lines into it:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Welcome!!</title>
  </head>
  <body>
    <h1>Great! example.com set up completed!</h1>
  </body>
</html>

All above commands run as sudo user so root user is owner of all new created files and directories. We need to change ownership of document root directories to avoid permission issue later for regular user. 

So our regular user can modify files in our web directories without any problems:

$ sudo chown -R www-data: /var/www/example.com


2. Create a Server Block

In Debian systems, default location for Nginx server blocks configuration files is at /etc/nginx/sites-available directory. Those can be used by enabling through symbolic links to the /etc/nginx/sites-enabled/ directory.

Create a new file for example.com using your choice text editor by typing :

$ sudo nano /etc/nginx/sites-available/example.com.conf

Now, add the following lines in to this file:

server {
    listen 80;
    listen [::]:80;
    root /var/www/example.com/public_html;
    index index.html;
    server_name example.com www.example.com;
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
    location / {
        try_files $uri $uri/ =404;
    }
}

You can give any names to your configuration file but it's best practice to give file name same as domain name.

Next, We need to enable nginx server block by making symbolic link at /etc/nginx/sites-enabled/ directory. 

Run the below command to create symbolic link:

$ sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

To test the Nginx configuration for correct syntax by typing:

$ sudo nginx -t

It will show following output if there is no error:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

You must restart the Nginx service to take effect. 

Execute below command:

$ sudo systemctl restart nginx

3. Testing Nginx server block

Finally, to verify the server block is working as expected open http://example.com to your web browser and it should show you as following :

Great! example.com set up completed!


[Need urgent assistance in fixing Nginx Configuration errors? We can help you. ]

This article covers how to create an Nginx server blocks to host multiple website on a single Debian machine. Nginx is a very popular high-performance web server that combines the power of reverse proxying, load balancing, caching and so much more. Depending on how it is configured, it can act as a reverse proxy as well as a load balancer for HTTP/HTTPS servers.


To install Nginx on Debian:

1. Update the Debian 10 Package Repository.

$  sudo apt update -y

2. Install Nginx on Debian 10.

$ sudo apt install nginx -y

3. To check the status of Nginx, execute:

$ systemctl status nginx

Related Posts