Linux

How to Manage Nginx Server Blocks on Ubuntu 20.04

How to Manage Nginx Server Blocks on Ubuntu 20.04

Server blocks are Nginx directives that define settings for specific domains, allowing you to run more than one website on one server. For each website, you can set the site document’s root (the directory containing website files), create separate security policies, use various SSL certificates, and more.

This article explains how to set up a Nginx server block on Ubuntu 20.04.

Precondition

Make sure you meet the following requirements before continuing:

  • The domain name points to your public IP server.
  • Nginx is installed on your Ubuntu system.
  • You are logged in as root or as a user with sudo rights.

In some articles, the term “Server Block” is referred to as “Virtual Host”. Virtual host is an Apache term.

Creating a Directory Structure

A document root is a directory where web site files for domain names are stored and presented in response to requests. You can set the document root to whatever location you want. In this example, we will use the following directory structure:

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

Each domain hosted on the server will have the document root set to / var / www / / public_html.

Start by creating a root directory for the domain:

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

We will also create an index.html file in the domain document’s root directory which will be displayed 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>

Because the above command is run as sudo user, the newly created file and directory are owned by root. To avoid permission issues, change ownership of the domain document’s root directory and all files in the directory to the Nginx user (www-data):

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

Creating a Server Block

On Ubuntu systems, the Nginx server block configuration file is in the / etc / nginx / sites-available directory. They can be activated by creating symbolic links to the / etc / nginx / sites-enabled directory, which Nginx reads at startup.

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

/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: Domain must match this server’s configuration block.
  • root: The directory where Nginx will present domain files.
  • access_log, error_log: Specifies the location for the log file.

The configuration file can be named whatever you want, but usually, it is best to use a domain name.

To activate a new server block file, create a symbolic link from the file to the site-active directory, which Nginx reads at 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 will look like this:

Output :

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 verify that the server block works as expected, go to http://example.com in the browser of your choice, and you will see something like this:

1

Conclusion

We have shown you how to create a Nginx server block and host multiple domains on one Ubuntu server. You can repeat the steps outlined above and create additional server blocks for all your domains.

If you encounter a problem, please leave a comment.

Related posts

How to Set Up WireGuard VPN on CentOS 8

Linux

How to Install Android Studio on Ubuntu 20.04

Linux

How to Set an SSH Key on Ubuntu 20.04

Linux

How to Install Puppets on CentOS 8 / RHEL 8

Linux

How to Install the SNMP Cacti Monitoring Tool on Debian 10

Linux

How to install Lightworks on Ubuntu

Linux

How to make a POST request with cURL

Linux

How to Download a Linux ISO File via a Torrent Client

Linux

How to compile the ONLYOFFICE Document Server from source code on Ubuntu

Linux