sobyte

server is an Nginx directive that defines domain-specific settings so that you can run multiple websites on a single server. For each site, you can set up the site document root (the directory containing the site files), create separate security policies, use different SSL certificates, and more.

This tutorial describes how to set up an Nginx server on Debian 10.

Prerequisites

Make sure you meet the following prerequisites.

  • A domain name pointing to your public server IP.
  • Nginx is installed on a Debian system.
  • You are a user with root or sudo privileges.

In some documentation, the term Server Blocks is referred to as Virtual host. Virtual host is an Apache term.

Create Directory Structure

The site root directory is the directory used to store the domain site files and respond to requests. The site root directory can be any directory on the server.

The examples in this article use the following directory structure.

1
2
3
4
5
6
7
/var/www/
├── domain1.com
│   └── public_html
├── domain2.com
│   └── public_html
├── domain3.com
│   └── public_html

Basically, we will create a separate directory in the /var/www directory for each domain that we want to host on the server. In each directory, we will create a public_html directory that will store the domain website files.

Run the following command to create the root directory for the domain example.com.

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

Next, create the index.html file in the site root of the domain.

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

Open the file and paste the following lines:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Welcome to example.com</title>
  </head>
  <body>
    <h1>Success! example.com home page!</h1>
  </body>
</html>

To avoid permission problems, change the ownership of the site root directory to the Nginx user (www-data).

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

Creating a server

By default, on Debian systems, Nginx server block configuration files are stored in the /etc/nginx/sites-available directory. To activate the configuration, you need to symbolically link the file to the /etc/nginx/sites-enabled/ directory.

Open your text editor and create the following server configuration file.

1
sudo nano /etc/nginx/sites-available/example.com.conf
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
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;
    }
}

The configuration file can be named anything you want, but it is usually best to use the domain name.

Enable the new server configuration file by creating a symbolic link from the file to the sites-enabled directory.

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

Test Nginx configuration syntax.

1
sudo nginx -t

If there are no errors, the output will look like this.

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

Restart the Nginx service to make the changes take effect.

1
sudo systemctl restart nginx

To verify that the server is working as expected, open http://example.com in your browser.

Conclusion

We have shown you how to create an Nginx server and host multiple domains on a single Debian server. To create a server for another domain, repeat the same steps.

If you want to secure your website with an SSL certificate, you can generate and install a free Letencrypt SSL certificate.