Sep 27, 2023 4 min read

How to Set Up Nginx Server Blocks on Debian 11

Set up nginx server on Debian 11 with our step-by-step tutorial. It allows you to host multiple websites or applications on a single server.

Set Up Nginx Server Blocks on Debian 11
Table of Contents

Choose a different version or distribution

Introduction

Before we begin talking about how to set up Nginx Server Blocks on Debian 11, let's briefly understand – What is a Server Block?

Nginx server blocks (also known as virtual hosts or server sections) allow you to host multiple websites or applications on a single server.

You can customize a variety of settings for each website, including setting the site document root (the directory that stores the website files), establishing a separate security policy, using various SSL certificates, and more.

In this tutorial, you will set up Nginx Server Blocks on Debian 11. We will also address a few FAQs on how to set up Nginx Server Blocks on Debian 11.

Prerequisites

Make sure you have met the following requirements:

Server Blocks is sometimes referred to as an Virtual host in some documentation. A virtual host is a word used by Apache.

Create the Directory Structure

The directory where the website files for a domain name are kept and served in response to queries is known as the document root. Any directory on the server is acceptable as the document root.

The directory structure used in the examples in this tutorial is as follows:

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

Simply put, we will build a separate directory inside the /var/www directory for each domain we want to host on our server. We will make a public_html directory in each of these directories to store the domain website files.

To create the root directory for the domain example.com, issue the following command:

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

Next, make an index.html file in the document root directory of the domain:

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

Open the file and paste these lines:

<!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 prevent concerns about permission to modify the Nginx user (www-data) ownership of the domain's document root directory:

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

Create a Server Block

Nginx server block configuration files are kept by default in the /etc/nginx/sites-available directory on Debian systems. To enable a configuration, symlink the file to the /etc/nginx/sites-enabled/ directory.

Create the following server block file in your text editor:

sudo nano /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 titled whatever you wish, however, it is ideal to use the domain name.

Make a symbolic link from the file to the sites-enabled directory to activate the new server block file:

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

And now unlink the Nginx's default server block:

sudo unlink /etc/nginx/sites-enabled/default

Verify the syntax in the Nginx configuration:

sudo nginx -t

If there are no errors, the output will seem as follows:

Output

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

For the modifications to take effect, restart the Nginx service:

sudo systemctl restart nginx

Open http://example.com in your browser to check if the server block is functioning as planned. You should see something similar to this:

Output Screen

FAQs: Setting Up Nginx Server Blocks on Debian 11

Why should I use Nginx server blocks on Debian 11? 

Using Nginx server blocks allows you to host multiple websites or applications on a single server, efficiently utilizing server resources and simplifying configuration management.

What are the prerequisites for setting up Nginx server blocks on Debian 11? 

To set up Nginx server blocks on Debian 11, you need a Debian 11 server with Nginx installed. You also need to ensure that your DNS records or hosts file properly point to the server.

What should I include in an Nginx server block configuration file? 

An Nginx server block configuration file should include directives such as the server name, root directory, log files, SSL settings (if applicable), and location blocks to handle requests.

How do I restart Nginx after making changes to server block configurations? 

After making changes to server block configurations, you can restart Nginx to apply the changes using the command sudo systemctl restart nginx.

Can I set up SSL/TLS for my Nginx server blocks on Debian 11? 

Yes, you can set up SSL/TLS for your Nginx server blocks. This involves obtaining SSL certificates and configuring the server block with the appropriate SSL directives.

What is the recommended method to set up Nginx server blocks on Debian 11?

The recommended method to set up Nginx server blocks on Debian 11 is by creating separate configuration files for each website or application and placing them in the /etc/nginx/sites-available/ directory.

How do I check if my Nginx server block configuration is valid? 

You can check the validity of your Nginx server block configuration file using the nginx -t command. This command will check the syntax and report any errors in your configuration.

Conclusion

Setting up Nginx server blocks on Debian 11 is a powerful way to efficiently manage multiple websites or applications on a single server.

By following the steps outlined in this tutorial, you can leverage Nginx's capabilities to host and manage multiple websites or applications on your Debian 11 server effectively.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.