How to Set Up Nginx Server Blocks on Debian 10

[ad_1]

A server block is an Nginx directive that defines settings for a specific domain, allowing you to run more than one website on a single server. For each website, you can set the site document root (the directory which contains the website files), create a separate security policy, use different SSL certificates, and much more.

This article explains how to set up Nginx server blocks on Debian 10.

Prerequisites #

Ensure that you have met the following prerequisites:

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

Create the Directory Structure #

The document root is the directory where the website files for a domain name are stored and served in response to requests. The document root can be any directory on the server.

The examples in this article use the following directory structure:

/var/www/
├── domain1.com
│   └── public_html
├── domain2.com
│   └── public_html
├── domain3.com
│   └── public_html

Basically, we will create a separate directory for each domain we want to host on our server inside the /var/www directory. Within each of these directories, 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:

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

Next, create an index.html file inside the domain’s document root directory:

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

Open the file and paste the following lines:

/var/www/example.com/public_html/index.html

<!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 issues change the ownership
of the domain document root directory to the Nginx user (www-data):

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

Create a Server Block #

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

Open your text editor and create the following server block file:

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

/etc/nginx/sites-available/example.com.conf

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 usually, it is best to use the domain name.

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

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

Test the Nginx configuration
for correct syntax:

sudo nginx -t

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
for the changes to take effect:

sudo systemctl restart nginx

To verify that the server block is working as expected, open http://example.com in your browser, and you will see something like this:

Conclusion #

We have shown you how to create Nginx server blocks and host multiple domains on a single Debian server. To create a server block 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 Letsencrypt SSL certificate
.

Feel free to leave a comment if you have any questions.

[ad_2]

Source link

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.