How to Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 20.04

At times, you may need to host more than one domain or website on your Nginx web server. For that to happen, a Server Block (Virtual Hosts) needs to be configured to encapsulate all of your domain’s configuration. Nginx server blocks are synonymous with Apache virtual host files and serve the same purpose.

This topic demonstrates how to set up an Nginx server block on Ubuntu 20.04.

Prerequisites:

  • An A record defined on your domain name hosting provider. An A record is a DNS record that points the domain name to the Public server IP address. For this guide, we will use the domain name crazytechgeek.info for illustration purposes.
  • An Nginx Server or LEMP Stack installed on Ubuntu 20.04 LTS instance.
  • A login user with Sudo privileges.

With all the requirements met, let’s explore how you can set up an Nginx server block in Ubuntu.

Step 1: Create an Nginx Document Root Directory

To get started, we will create a separate directory for our domain that will contain all the settings related to the domain.

$ sudo mkdir -p /var/www/crazytechgeek.info/html

Next, assign the directory’s ownership using the $USER environment variable. This assigns the directory’s ownership to the currently logged in user. Ensure that you are logged in using a regular user’s account and not as root.

$ sudo chown -R $USER:$USER /var/www/crazytechgeek.info/html

Next, assign the appropriate permissions to the directory, granting the logged-in user all the rights (read, write and execute) and the group and other users read and execute permissions only.

$ sudo chmod -R 755 /var/www/crazytechgeek.info

With the directory permissions and ownership correctly configured, we need to create a sample web page for the domain.

Step 2: Create a Sample Page for the Domain

In this step, we are going to create an index.html file for testing purposes. This file will serve content that will be displayed on the web browser when the domain is called on the browser.

$ sudo vim /var/www/crazytechgeek.info/html/index.html

Paste the following HTML content.

<html>
    <head>
        <title>Welcome to your_domain!</title>
    </head>
    <body>
  <h1>Bravo! Your server block is working as expected!</h1>
    </body>
</html>

Save and exit the configuration file.

Step 3: Create an Nginx Server Block in Ubuntu

Nginx server blocks are located in the /etc/nginx/sites-available directory. The default Nginx server block is /etc/nginx/sites-available/default which serves the default HTML file at /var/www/html/index.nginx-debian.html.

For our case, we need to create a server block that will serve the content in the index.html file that we created earlier.

So, create the server block file shown.

$ sudo vim /etc/nginx/sites-available/crazytechgeek.info

Paste the content below:

server {
        listen 80;
        listen [::]:80;

        root /var/www/crazytechgeek.info/html;
        index index.html index.htm index.nginx-debian.html;

        server_name crazytechgeek.info www.crazytechgeek.info;

        location / {
                try_files $uri $uri/ =404;
        }

		
    access_log /var/log/nginx/crazytechgeek.info.access.log;
    error_log /var/log/nginx/crazytechgeek.info.error.log;

}
Create Nginx Server Block in Ubuntu
Create Nginx Server Block in Ubuntu

Save and exit the file.

Step 4: Enable Nginx Server Block in Ubuntu

To enable the Nginx server block, you need to symlink it to the /etc/nginx/sites-enabled/ directory as shown.

$ sudo ln -s /etc/nginx/sites-available/crazytechgeek.info /etc/nginx/sites-enabled/

At this point, we are pretty much done. However, it’s prudent to confirm that all the configurations are in order. To do so, execute the command:

$ sudo nginx -t

If you followed our steps correctly, you should get the output shown:

Check Nginx Configuration File
Check Nginx Configuration File

Finally, restart Nginx for the changes made to the configuration files to take effect.

$ sudo systemctl restart Nginx

Then confirm if Nginx is running by running the command shown:

$ sudo systemctl status Nginx
Check Nginx Service Status
Check Nginx Service Status

Step 5: Testing the Nginx Server Block in Ubuntu

To confirm whether the server block is working as expected and is serving content in the /var/www/crazytechgeek.info directory, open your web browser and browse your server’s domain name:

http://domain-name

You should get content contained in the HTML file in your server block as shown.

Check Nginx Server Block
Check Nginx Server Block
Conclusion

In this guide, we have shown you how to set up an Nginx server block using a single domain on Ubuntu Linux. You can repeat the same steps for different domains and still achieve the same results. We do hope that the guide was insightful.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.