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:

  

Nginx Server block

  

  

Success

  

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!

The post How to Set Up Nginx Server Blocks on Ubuntu 20.04 appeared first on LowEndBox.

Ubuntu Server Admin

Recent Posts

Welcome to the Ubuntu Weekly Newsletter 883

Welcome to the Ubuntu Weekly Newsletter, Issue 883 for the week of March 9 –…

7 hours ago

How to Install nvidia-smi on Ubuntu or Debian Linux

In this article, we will see how to install nvidia-smi on Ubuntu or Debian Linux.…

19 hours ago

How to Install clang tool on Ubuntu or Debian Linux

In this article, we will see how to install clang tool on Ubuntu or Debian…

2 days ago

How to resolve Ubuntu 20.04 Container Signature Errors on Raspberry Pi ARM Devices

When working with Docker containers on Raspberry Pi devices, you might encounter frustrating signature verification…

2 days ago

How to fix DNS Resolution Issues with OpenVPN on Ubuntu 18.04

You’ve recently upgraded to Ubuntu 18.04 and found that your OpenVPN connection no longer resolves…

2 days ago

How to Fix Ubuntu 18.04 System Monitor Launch Issues

Have you ever tried to open System Monitor on your Ubuntu 18.04 system only to…

3 days ago