Choose a different version or distribution
Introduction
A server block is a Nginx directive that provides domain-specific settings, allowing you to run several websites 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 10.
Prerequisites
Make sure you have met the following requirements:
- Domain name pointing to the IP address of your public server.
- You have Nginx set up on your Debian system.
- You have sudo privileges or are logged in as root user.
Server Blocks
is sometimes referred to as a 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/
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:
Conclusion
We have demonstrated how to set up Nginx server blocks and run several domains on a single Debian server. Repeat these steps to make a server block for another domain.
You can create and install a free Let's Encrypt SSL certificate if you want to safeguard your website with SSL certificate.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.