Virtual Host refers to the practice of running more than one website on a single machine. Virtual hosts can be "IP-based", meaning that you have a different IP address for every web site, or "name-based", meaning that you have multiple names running on each IP address. The fact that they are running on the same physical server is not apparent to the end-user. This is relevant to anyone looking to host more than one site off of a single VPS.

In this guide, we are going to create named-based virtual hosts with two websites named cloudwaferlabs.com.ng and debian.cloudwaferlabs.com.ng on a single IP address.

If you do not already have a registered domain name, you can register one with Cloudwafer

Deploying your cloud server
If you have not already registered with Cloudwafer, you should begin by getting signed up. Take a moment to create an account after which you can easily deploy your own cloud servers.

Once you have signed up, log into your Cloudwafer Client Area with the password provided in your mail and deploy your Cloudwafer cloud server.

Updating System Packages
It is always recommended that you update the system to the latest packages before beginning any major installations. This is done with the command below:

sudo apt-get update && sudo apt-get upgrade

Note: The user you are logged in as must have sudo privileges to be able to install packages.

Step 1: Install Apache
Follow the steps outlined in our guide on installing Apache on Debian 9 to complete this step.

Step 2: Create the Directory Structure
The default Debian document root is /var/www/html but we are going to make our own virtual hosts under /var/www. Create a public_html folder for each virtual host inside the /var/www directory that will hold our actual files. Type the command below:

sudo mkdir -p /var/www/cloudwaferlabs.com.ng/public_html 

sudo mkdir -p /var/www/debian.cloudwaferlabs.com.ng/public_html

Remember to replace the domains with your own domains or values while setting up your own virtual hosts.

Step 2: Grant Permissions
In order for our webpage to be accessible by anyone on the web, we have to grant permission to our regular user to be able to modify the files in our newly created directories. Type the command below:

sudo chown -R username:username /var/www/cloudwaferlabs.com.ng/public_html

sudo chown -R username:username /var/www/debian.cloudwaferlabs.com.ng/public_html

username here represents the name of the user you are currently logged in as. In this case, it is ayodele. Our regular user now owns the newly created public_html subdirectories where we will be storing our content.

We can further modify our permissions to ensure that read access is permitted to the general web directory and all of the files and folders it contains by issuing the command:

sudo chmod -R 755 /var/www

The Apache web server now has the permissions it needs to serve content, and our regular user should be able to create content within the necessary folders.

Step 3: Create Demo Pages for Each Virtual Host
Our websites need to display something when visited on the web, so we are going to create a demo page that contains content to be served up when we visit our website. A simple index.html page for each site is enough for this demonstration.

Virtual Host 1: cloudwaferlabs.com.ng
Create a simple index.html document in our newly created folder by issuing the command below:

sudo nano /var/www/cloudwaferlabs.com.ng/public_html/index.html

You can type or copy and edit the code below to insert:

<html>  
<head>  
<title>This is CloudwaferLabs
</title>  
</head>  
<body>  
<h1>This is our first virtual host on Debian with an Apache server</h1>  
</body>  
</html>

To make work easier and reduce time, we can easily copy this file to our second site by using the cp
command as shown below:

sudo cp /var/www/cloudwaferlabs.com.ng/public_html/index.html /var/www/debian.cloudwaferlabs.com.ng/public_html/index.html

We can then open the newly copied file and modify the distinct pieces of information:

sudo nano /var/www/debian.cloudwaferlabs.com.ng/public_html/index.html

Ensure that you saved both files before closing the editor.

Step 4: Set Up Virtual Hosts Files
We are going to store our Apache Virtual Hosts configuration files in the /etc/apache2/sites-available directory then enable the files by creating symbolic links to the /etc/apache2/sites-enabled directory.

sudo nano /etc/apache2/sites-available/cloudwaferlabs.com.ng.conf

Enter the following replacing cloudwaferlabs.com.ng with your domain name.

<VirtualHost *:80>
    ServerName cloudwaferlabs.com.ng
    ServerAlias www.cloudwaferlabs.com.ng
    ServerAdmin webmaster@cloudwaferlabs.com.ng
    DocumentRoot /var/www/cloudwaferlabs.com.ng/public_html

    <Directory /var/www/cloudwaferlabs.com.ng/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/cloudwaferlabs.com.ng-error.log
    CustomLog ${APACHE_LOG_DIR}/cloudwaferlabs.com.ng-access.log combined
</VirtualHost>

Just like we did earlier, we can minimize time by using the cp command to create our second virtual host file.

sudo cp /etc/apache2/sites-available/cloudwaferlabs.com.ng.conf /etc/apache2/sites-available/debian.cloudwaferlabs.com.ng.conf

Open the new file and modify all the information needed in regards to the second domain:

sudo nano /etc/apache2/sites-available/debian.cloudwaferlabs.com.ng.conf


/etc/apache2/sites-available/debian.cloudwaferlabs.com.ng.conf
<VirtualHost *:80>
    ServerName debian.cloudwaferlabs.com.ng
    ServerAlias www.debian.cloudwaferlabs.com.ng
    ServerAdmin webmaster@debian.cloudwaferlabs.com.ng
    DocumentRoot /var/www/debian.cloudwaferlabs.com.ng/public_html

    <Directory /var/www/debian.cloudwaferlabs.com.ng/public_html>
        Options -Indexes +FollowSymLinks
        AllowOverride All
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/debian.cloudwaferlabs.com.ng-error.log
    CustomLog ${APACHE_LOG_DIR}/debian.cloudwaferlabs.com.ng-access.log combined
</VirtualHost>

Step Five — Enable the New Virtual Host Files
After creating our new virtual host files, we must enable them by using the a2ensite tool provided by Apache with the command shown below:

sudo a2ensite cloudwaferlabs.com.ng.conf
sudo a2ensite debian.cloudwaferlabs.com.ng.conf

Once the configuration is enabled, test if the syntax is correct by typing:

sudo apachectl configtest

Step Six — Test your Results
If you used a functioning domain address while carrying out this tutorial, navigate your browser to the URL of the domains used for the virtual hosts. In this case, we are visiting the websites: cloudwaferlabs.com.ng and debian.cloudwaferlabs.com.ng

Below are the results:

cloudwaferlabs.com.ng

debian.cloudwaferlabs.com.ng

If you didn't use a functioning domain address, open the file /etc/hosts and edit it using the command below:

sudo nano /etc/hosts  

Assuming that my VPS IP address is 222.222.222.222, Add the virtual domain names one by one as shown below.

127.0.0.1   localhost  
127.0.1.1   guest-desktop  
222.222.222.222  cloudwaferlabs.com.ng  
222.222.222.222  debian.cloudwaferlabs.com.ng  

Open up your browser and point to the URL of your test domains.

Note:

  • Don’t forget to replace the IP address with your own.
  • If your server does not have a GUI, this webpages would only open on a text-based browser (such as Lynx).
  • If you don't have a functioning domain address, this webpages would only open on your system only.