Nginx Virtual Hosting on Ubuntu 17.04 / 17.10

Few days ago we showed students and new users how to easily setup Apache2 virtual hosting. To read this post, please click here.

This post will show you how to create virtual hosting using Nginx webserver on Ubuntu 17.04 / 17.10. In our previous post we said virtual hosting allows multiple independent websites to be run on a single webserver. If the hardware you’re running Nginx on is capable of running multiple websites, you can configure Nginx virtual host to make it happen.

Each site runs independently and doesn’t know the others are running on the same Nginx server. This is relevant for those wanting to run multiple websites and blogs without spending too much on hardware resources.

To get started with running virtual hosts on Nginx, follow the steps below

Step 1: Install Nginx

You won’t be able to run virtual hosts on Nginx without first installing Nginx webserver. To install Nginx on Ubuntu, run the commands below

sudo apt-get update
sudo apt-get install nginx

Now that Nginx is installed, all your virtual host configurations file will live in the directory below. Each website will have its own configuration file.

/etc/nginx/sites-available/

Run each of the line below to create new virtual host configuration file for each website.

sudo nano /etc/nginx/sites-available/VirtualHost1
sudo nano /etc/nginx/sites-available/VirtualHost2
sudo nano /etc/nginx/sites-available/VirtualHost3

The three lines above create three virtual host configuration files named VirtualHost1VirtualHost2 and VirtualHost3. Each of the configuration file will be used to run one domain or website.

Step 2: Configure VirtualHost Files

Now that your created three virtual host files, you can copy and paste the codes below into each file and save. Each configuration file will have information on the website and domain it controls.. so there should be three separate configurations.

Copy the block and paste the block of code below in each file corresponding with each website or domain.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/virtualhost1.com;
    index  index.html index.htm;
    server_name  www.virtualhost1.com;

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

Copy the above code and paste each configuration file, replacing the highlighted lines to match each domain or website DocumentRoot and ServerName.

Repeat the same for VirtualHost 2.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/virtualhost2.com;
    index  index.html index.htm;
    server_name  www.virtualhost2.com;

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

and so forth.

Now you must create three DocumentRoot directories for the three websites location defined on the root line.

sudo mkdir -p /var/www/html/virtualhost1.com
sudo mkdir -p /var/www/html/virtualhost2.com
sudo mkdir -p /var/www/html/virtualhost3.com

Now all you have to do is create a index.html file in each and test the sites.

STEP 3: ENABLE EACH SITE

Now that you’ve created each virtual host configuration file, you must enable each. Run the commands below for each of the virtual host you created.

sudo ln -s /etc/nginx/sites-available/VirtualHost1 /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/VirtualHost2 /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/VirtualHost3 /etc/nginx/sites-enabled/

Remove Nginx default enabled site configuration by running the commands below. Leaving the default configuration file may create trouble for you. So run the commands to remove it

sudo rm /etc/nginx/sites-enabled/default

STEP 4: RESTART Nginx

Finally, run the commands below to restart Apache2 webserver.

sudo systemctl restart nginx

Test each site after creating an index.html file in each root directory.  Then browse the domain name used in the virtual host configuration files.

<html>
<body>
<p>Nginx Virtual Host 1 Test</p>
</div>
</body>
</html>

Enjoy!

ubuntu virtual host apache2

You may also like the post below:

Setup Apache2 Virtual Hosting on Ubuntu 17.04 / 17.10