How To Set Up Nginx Server Blocks on Ubuntu 20.04

0 comments 5.7K views 3 mins read
Published on: February 8, 2023 | Last updated on: October 11, 2024
\\ CATEGORIES: LinuxUbuntu

We’ll show you how to set up Nginx server blocks (the equivalent of Apache Virtual Hosts) on Ubuntu 20.04.

Multiple websites can be hosted on a single server using Nginx Server Blocks. With Server Blocks, you can do a lot of useful things, such as designating a unique security policy for each website, using unique SSL certificates, and more.

Prerequisites

Before you continue with the tutorial, make sure that you have done the following:

  • Your public server IP is pointed to by a domain name. We’ll employ example.com.
  • By following these guidelines, Nginx is installed.
  • You have access as a user with sudo rights.

Sometimes you’ll see the term “Virtual host” used to describe Server Blocks. The term “virtual host” was coined by Apache.

Create the Directory Structure

A domain’s document root is the directory from which all files for that domain will be served when the domain is accessed by a user’s browser. The document root can be changed to any directory you like.

The following folder hierarchy will be used:

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

Each domain name that will be hosted on this server will have its own directory under /var/www. We’ll make a public html folder inside each of these directories to store the files for the various sites under the given domain.

Let’s get started by making our domain’s main directory, example.com’s /.

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

We will make an index.html file in the root directory of the domain for testing purposes.

Launch your preferred text editor and draft the sample file:

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

By using the sudo command, we can ensure that all new files and directories created by these commands are set to be owned by the root user.

To prevent any permissions issues, you should change the domain document root directory’s ownership to the Nginx user (www-data):

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

Create a Server Block

The Nginx server blocks configuration files on Ubuntu systems are located in the /etc/nginx/sites-available directory and are activated via symbolic links to the /etc/nginx/sites-enabled/ directory.

Prepare the following server block file in your preferred text editor:

nano /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;
    }
}

The configuration file can have any name you like, but the domain name is the most common and logical choice.

It is necessary to create a symbolic link from the new server block file to the sites-enabled directory, which is read by Nginx at startup, in order to activate the file.

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

Ensure proper syntax when setting up Nginx:

sudo nginx -t

The expected result should look like this:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

To make the modifications take effect, restart the Nginx service:

sudo systemctl restart nginx

Finally, open http://example.com in your preferred browser, and if everything is in order, you should see something like this:

Conclusion

You now know how to configure a server block in Nginx so that a single Ubuntu server can host multiple domains. If you need more server blocks for your domains, just follow the same steps we showed you above.

This tutorial will show you how to install a free LetsEncrypt SSL certificate on your website.

Secure Nginx with Let’s Encrypt on Ubuntu 20.04

Please share your experiences in the comments if you are having difficulties.