Introduction

This article details how to configure Virtual Hosting for Nginx. If you are looking for a guide for Apache, click here.

There are several reasons you may want to host multiple websites on a single server. If you are using a dedicated server / VPS and want to host multiple applications on a separate domain and a single server then you will need to host multiple websites on a single server. You can achieve this with Apache / Nginx virtual hosting. Virtual hosting allows you to use a single VPS to host all your domains. So hosting multiple websites on a single VPS server using Virtual hosting is the best solution for you to reduce the hosting cost.

There is, in theory, no limit to the number of sites that you can host on your VPS with Apache or Nginx. But, make sure that your server has enough disk space, CPU and RAM.

In this tutorial, we will learn how to set up multiple websites on an Ubuntu VPS with Nginx.

Webdock does not recommend you use our servers for shared hosting as it can cause a range of issues and stops you from using some of our management tools, namely our easy Let's Encrypt / Certbot management for SSL Certificates and Wordpress management. Click here to read why we think you should really use a single VPS for each website / app.

Please note: Doing these actions may bring down your server. Do not do this on a live site without knowing the potential consequences.

Prerequisites

  • A fresh Webdock cloud Ubuntu instance with LEMP installed.
  • Two valid domain names are pointed with your VPS IP address. In this tutorial, we will use web1.webdock.io and web2.webdock.io.
  • You have shell (SSH) access to your VPS.

Note : You can refer the Webdock DNS Guide to manage the DNS records.

Configure Nginx to Host Multiple Websites

In this section, we will show you how to host two websites named web1.webdock.io and web2.webdock.io on a single Ubuntu VPS with Nginx webserver.

Create Directory Structure

Before starting, make sure LEMP stack is installed on your VPS. You can check the Nginx server status with the following command:

systemctl status nginx

Best method to host multiple websites is to create a separate document root directory and configuration file for each website. So, you will need to create a directory structure for both websites inside Nginx web root:

To do so, run the following command for each website:

mkdir /var/www/html/web1.webdock.io
mkdir /var/www/html/web2.webdock.io

Next, you will need to create sample website content for each website:

First, create a index.html file for web1.webdock.io website:

nano /var/www/html/web1.webdock.io/index.html

Add the following html markup which will be served when you connect to the site:

web1.webdock.io

Welcome to the web1.webdock.io with Nginx webserver.

Save and close the file.

Next, create a index.html file for web2.webdock.io website:

nano /var/www/html/web2.webdock.io/index.html

Add the following html markup which will be served when you connect to the site:

web2.webdock.io

Welcome to the web2.webdock.io with Nginx webserver.

Save and close the file. Then, change the ownership of both website directories to www-data:

chown -R www-data:www-data /var/www/html/web1.webdock.io
chown -R www-data:www-data /var/www/html/web2.webdock.io

Create Virtual Configuration

Next, you will need to create a virtual host configuration file for each website that indicate how the Nginx web server will respond to various domain requests.

First, create a virtual host configuration file for the web1.webdock.io website:

nano /etc/nginx/sites-available/web1.webdock.io.conf

Add the following lines:

server {
        listen 80;
        listen [::]:80;
        root /var/www/html/web1.webdock.io;
        index index.html index.htm;
        server_name web1.webdock.io;

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

}

Save and close the file. Then, create a virtual host configuration file for the web2.webdock.io website:

nano /etc/nginx/sites-available/web2.webdock.io.conf

Add the following lines:

server {
        listen 80;
        listen [::]:80;
        root /var/www/html/web2.webdock.io;
        index index.html index.htm;
        server_name web2.webdock.io;

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

}

Save and close the file. Then, enable each virtual host with the following command:

ln -s /etc/nginx/sites-available/web1.webdock.io.conf /etc/nginx/sites-enabled/
ln -s /etc/nginx/sites-available/web2.webdock.io.conf /etc/nginx/sites-enabled/

Next, check Nginx for any syntax error with the following command:

nginx -t

If everything goes fine, you should get the following output:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Finally, restart the Nginx service to apply the configuration changes:

systemctl restart nginx

Test Your Websites

Now, open your web browser and type the URL http://web1.webdock.io and http://web2.webdock.io. You should see both websites with the content we have created earlier:

web1.webdock.io

page1.png

web2.webdock.io

page2.png