How to set up Nginx server blocks on Ubuntu 20.04

s2 0

A server block is an Nginx statement that defines settings for a particular domain and allows you to run more than one website on a single server. For each website, you can set the Site Document Root (the directory that contains the website files), create a separate security policy, use different SSL certificates, and much more.

This article describes how to set up Nginx server blocks on Ubuntu 20.04.

requirements

Make sure you meet the following requirements before proceeding:

  • Domain name pointing to your public server IP.
  • Nginx installed on your Ubuntu system.

  • You are logged in as root or as a user with sudo rights.

In some articles, the term “server blocks” is referred to as “virtual host”. A virtual host is an Apache term.

Create the directory structure

The document root is the directory in which the website files for a domain name are stored and made available in response to requests. You can set the document master to any storage location. In this example we use the following directory structure:

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

For each domain hosted on the server, the document root directory is set to /var/www/<domain_name>/public_html.

First, create the root directory for the domain:

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

We also create one index.html File in the root of the domain document that appears when you visit the domain in your browser:

/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>

Since the above commands are run as the sudo user, the newly created files and directories are owned by root. To avoid permissions problems, change ownership of the domain document root directory and all files in the directory to the Nginx user (www-data):

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

Create a server block

On Ubuntu systems, the configuration files of the Nginx server block are located in /etc/nginx/sites-available Directory. They can be activated by adding symbolic links to the /etc/nginx/sites-enabled Directory that Nginx read on startup.

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

/etc/nginx/sites-available/example.com
server {
    listen 80;

    server_name example.com www.example.com;

    root /var/www/example.com/public_html;

    index index.html;

    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
}
  • server_name: The domains that should match for this server block configuration.
  • root: The directory from which Nginx will host the domain files.
  • access_log, error_log: Specifies the location for log files.

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

To activate the new server block file, create a symbolic link from the file to the sites-enabled Directory that Nginx read on startup:

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

Test the Nginx configuration for the correct syntax:

sudo nginx -t

If there are no errors, the output looks like this:

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

Finally, to check that the server block is working as expected, open https://example.com in your browser of your choice and you will see something like this:

diploma

We showed you how to create Nginx server blocks and host multiple domains on a single Ubuntu server. You can repeat the steps above and create additional server blocks for all of your domains.

If you have any problems, please feel free to leave a comment.

Tags: