LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

How to Set Up Nginx Server Blocks on Ubuntu 20.04

How to Set Up Nginx Server Blocks on Ubuntu 20.04

This article was contributed by the folks at RackNerd – a provider of shared hosting, reseller hosting, VPS hosting, dedicated servers, DRaaS, colocation, and more. If you are looking for a VPS with full root access to set up Nginx on, be sure to review their latest LowEndBox special offers here.

If you want to run more than one website on a single server, the Nginx server block is an incredible tool. It lets you define separate settings for different domain names, and thus enables you to host multiple domains on one server.

With the Nginx server blocks, you can configure separate security policies such as different SSL certificates as well.

In this article, we will guide you on how you can set up Nginx server blocks on your own Ubuntu 20.04 server.

Prerequisites:

Before you continue with the setup, make sure that you fulfill the following requirements:

1- You should already have Nginx installed on your server.

2- You should have a domain name for your public server IP

3- You should have root privileges to your server.

Step 1. Create the directory

You can create the directory at any location of your choice. In your chosen location, create a directory with the following structure:

/var/www/

-> first-domain.com

         -> public_html

-> second-domain.com

         ->public_html

The public_html folder you see in the structure above would contain the public HTML file for your website. You can create the directory with the following command:

sudo mkdir -p /var/www/first-domain.com/public_html

Inside the public_html folder, place the files for your existing website, or you can make a temporary “index.html” file to check the setup.

Place the following code in the temporary index.html file:

<!DOCTYPE html>

<html lang=”en”>

  <head>

<meta charset=”utf-8″>

<title>Nginx Server block</title>

  </head>

  <body>

<h1>Success</h1>

  </body>

</html>

Step 2- Change permission

The directories and files created in step 1 were done with the sudo commands, and therefore are owned by root. We will change the permission and ownership of these files with the following command:

sudo chown -R www-data: /var/www/first-domain.com

Step 3- Create the server blocks

The Nginx server block config files on Ubuntu servers are placed in the /etc/nginx/sites-available folder. You can enable these configs by placing a link to them in the /etc/nginx/sites-enabled folder. This folder is read by Nginx at the boot time.

In the /etc/nginx/sites-available folder, create a config file for your website and paste the following code in it:

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;

}

Step 4- Enable the configuration

You can enable the server block created above with the following command:

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

This command creates a symbolic link to the config file. This would be read by Nginx during startup.

Step 5- Test the setup

Now, you are done with the configuration. You can check if it’s working with the following command:

sudo nginx -t

If the setup is successful, then the output would not contain any errors.

Now, you can restart the Nginx service with this command to get your server block started:

sudo systemctl restart nginx

Congratulations! The Nginx server block should now be working as expected.

Have you set up Nginx Server Blocks on your Ubuntu 20.04 VPS? Leave your comments and feedback in the comments section below!

Jon Biloh

No Comments

    Leave a Reply

    Some notes on commenting on LowEndBox:

    • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
    • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
    • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

    Your email address will not be published. Required fields are marked *