How to set up Nginx server blocks on Ubuntu 18.04

s2 0

Nginx Server Blocks allows you to run more than one website on a single computer. With server blocks, you can specify the Site Document Root (the directory that contains the website files), create a separate security policy for each site, use different SSL certificates for each site, and much more.

In this article, we provide a step-by-step guide on how to set up Nginx server blocks (similar to Apache Virtual Hosts) on Ubuntu 18.04.

requirements

Make sure you meet the following requirements before proceeding with the tutorial:

  • They have a domain name that points to your public server IP. We will use example.com.
  • You installed Nginx by following these instructions.
  • You are logged in as a user with sudo rights.
In some documentation you can see Server Blocks as a . are designated 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 made available in response to requests. You can set the document master to any storage location.

We use the following directory structure:

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

Basically we create a separate directory within the /var/www Directory. In each of these directories we create a public_html Directory in which the domain website files are stored.

Let’s create the root directory for our domain example.com:

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

For test purposes we will create one index.html File in the document root of the domain.

Open your editor and create the demo file:

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

In this example we run the commands as sudo user and the newly created files and directories belong to the root user.

To avoid permissions problems, change 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, configuration files for Nginx server blocks on Ubuntu systems are saved in /etc/nginx/sites-available Directory represented by symbolic links to the /etc/nginx/sites-enabled/ Directory.

Open the editor of your choice and create the following server block file:

/etc/nginx/sites-available/example.com
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;
    }
}

You can name the configuration file anything you want, but it’s 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 read by Nginx 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:

Conclusion

You learned how to create an Nginx server block configuration to 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 want to secure your website with a free LetsEncrypt SSL certificate, you can read the guide below:

Secure Nginx with Let’s Encrypt on Ubuntu 18.04

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

This post is part of the how-installed-lemp-stack-on-ubuntu-18-04 Series.
Further articles in this series:


How to install Nginx on Ubuntu 18.04

How to set up Nginx server blocks on Ubuntu 18.04

Secure Nginx with Let’s Encrypt on Ubuntu 18.04

How to install MySQL on Ubuntu 18.04

How to install PHP on Ubuntu 18.04

This post is part of the how-installed-lemp-stack-on-ubuntu-18-04 Series.
Further articles in this series:


How to install Nginx on Ubuntu 18.04

How to set up Nginx server blocks on Ubuntu 18.04

Secure Nginx with Let’s Encrypt on Ubuntu 18.04

How to install MySQL on Ubuntu 18.04

How to install PHP on Ubuntu 18.04

How to install Nginx on Ubuntu 18.04

How to set up Nginx server blocks on Ubuntu 18.04

Secure Nginx with Let’s Encrypt on Ubuntu 18.04

How to install MySQL on Ubuntu 18.04

How to install PHP on Ubuntu 18.04