How To Set Up Nginx Virtual Hosts on Ubuntu 16.04

If we have only one server and we need to host many domains on the same server. For that, we need to configure the virtual host. All websites/domains will share the same server resources and configurations. Virtual hosts are very popular in shared hosting. We can run multiple websites on the same server. It’s cost-effective.

In this article, we will see how to set up multiple virtual hosts on the same server.

Steps for multiple virtual hosts setup

First, we need to make sure that Nginx is installed or not on the ubuntu server. If not installed, then we need to install Nginx first. Using the below command you can install Nginx.

apt-get install nginx

Once Nginx is installed, we will start setup virtual hosts.

We are going to set up 2 virtual hosts on the Nginx server. The domain names we will use here are test1.com and test2.com.

Step1: Configure Document Root Directories

By Default, one server block is enabled in the Nginx Ubuntu server. The default root directory is var/www/html. This works well for a single website. If we create multiple websites, then we have to create different root directories for different websites.

We will create our directory structure in the /var/www/ location and we will add our domain name after that.
we need two root directories so we will create two different directories. Please use the below command to create the directories:

sudo mkdir -p /var/www/test1.com/html
sudo mkdir -p /var/www/test2.com/html

The directories are created. Now we need to setup the proper file permissions so that Nginx can access it. Please use below command to provide the permission:

chmod -R 755 /var/www

The directory structure is configured properly. Now we need to create sample pages for both websites.

Create a default file for both domains:

nano /var/www/test1.com/html/index.html
nano /var/www/test2.com/html/index.html

We can add some text in the newly created files:

<html>
    <head>
        <title>Welcome to test1.com</title>
    </head>
    <body>
  <h1>This is a test page for the domain test1.com</h1>
    </body>
</html>

Same we can create for second domain:

<html>
    <head>
        <title>Welcome to test2.com</title>
    </head>
    <body>
  <h1>This is a test page for the domain test2.com</h1>
    </body>
</html>

Step 2 – Configure Virtual Hosts

/etc/nginx/sites-available directory has Nginx server blocks.
Now we need to create server blocks for our domains. first, we will configure the server block for our first domain and then we will copy it for the second domain and we will do the necessary changes.

Create the First Server Block File
Please use the below command to create the new configuration file:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/test1.com

Now open newly created files using sudo privileges

sudo nano /etc/nginx/sites-available/test1.com
server {
        listen 80;
        listen [::]:80;

        root /var/www/test1.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name test1.com www.test1.com;

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

Save the file and close.

Create the Second Server Block File
Like we did for the first domain, we need to create a new file for the second domain:

sudo cp /etc/nginx/sites-available/example.com /etc/nginx/sites-available/test2.com

Now open newly created files using sudo privileges


sudo nano /etc/nginx/sites-available/test2.com

The second domain configuration file will look like this:

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

        root /var/www/test2.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name test2.com www.test2.com;

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

Save and close the file.

Step 3: Enable the Server Blocks

Now we have created our server block files for both domains and now need to enable both server block files. We will enable Nginx virtual hosts by Symbolic linking from the /etc/nginx/sites-available directory to the /etc/nginx/sites-enabled directory.

Please use the following commands to create the symbolic links:

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

Restart the Nginx server to reload the changes:

sudo systemctl restart nginx

Check the Nginx server status:

sudo systemctl status Nginx

Step 4: Testing the virtual hosts setup

If everything works, you can configure the DNS settings. This is optional.

Open the host file using the below command.

sudo nano /etc/hosts
127.0.0.1   localhost

111.2.2.22.2 test1.com www.test1.com
111.2.2.22.3 test2.com www.test2.com

Save and close the file.

Now everything is done. For testing open the browser and browse the domains.

You will get the following page:

http://test1.com/
http://test2.com/

We have successfully configured two domains

Thanks for reading, feel free to reach out to me for any comments and suggestions. I hope you found the article helpful and you were able to setup the virtual hosts.

Leave a comment

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