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.
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
.
|
|
Next, create the index.html
file in the site root of the domain.
|
|
Open the file and paste the following lines:
To avoid permission problems, change the ownership of the site root directory to the Nginx user (www-data
).
|
|
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.
|
|
|
|
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.
|
|
Test Nginx configuration syntax.
|
|
If there are no errors, the output will look like this.
Restart the Nginx service to make the changes take effect.
|
|
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.