Skip to main content

Setting Up Virtual Hosts In Apache HTTP Web Server On Linux-Ubuntu and Debian based distros

What is Apache Virtual Host?

A virtual host allows you to run more than one website or rather multiple websites from a single machine/physical server or virtual private server.

There are 2 types of virtual hosting on Apache
  • IP-based Virtual Hosting
This is where every individual website on the Apache server uses a different unique IP address. It is a method to apply different directives based on the IP address and port a request is received on. It is commonly used to serve different websites on different ports or interfaces. Using a single IP address but multiple TCP ports is also called IP-based virtual hosting.
It uses the IP address of the connection to determine the correct virtual host to serve. Therefore you need to have a separate IP address for each host.
  • Name-based Virtual Hosting
Many different hosts can share the same IP address. It is simpler than IP-based hosting since you only need to configure the DNS server to map each hostname to the correct IP address and then configure the Apache HTTP server to recognize the different hostnames. It also eases the demand for scarce IP addresses.

Requirements

  • A computer running Ubuntu Operating System
  • A user account with sudo or root privileges 
  • Firewall enabled
  • A domain pointing to a public server IP

Apache server by default has one server enabled and configured to serve documents from the main directory located at /var/www/html. But this set up is suitable if you are working with a single site. In the event that you are hosting multiple sites, the server can become too large or disorganized to function efficiently. 

So instead of modifying the default directory at /var/www/html we can create a directory structure within /var/www for our site(s) while leaving /var/www/html in place as the default directory to be served if a client request doesn't match any other sites.

How To setup a name-based virtual host 

Step 1  

  • Open your terminal (Ctrl Alt T) and create a directory for your domain as follows
Replace website1.com and website2.com with your domain name
 sudo mkdir -p /var/www/website1.com/public 

 sudo mkdir -p /var/www/website2.com/public 
The p command stands for parent directory and allows us to create sub-directories of a directory. But it will create the parent directory first if it doesn't exist. If the directory you wish to create already exists, it will present you with an error letting you know that the directory exists.

Within the directories, we also created public directories which are going to store website files for the domains.

  • Next, create a sample index.html page for each domain using your favourite text editor.
(I used Sublime text hence the subl
Visual Studio Code use code
Nano editor use nano)

We'll start with the first domain. Type this in your terminal
 sudo subl /var/www/website1.com/public/index.html  
For Sublime Text use subl
Visual Studio Code use code
Nano editor use nano
Add the following HTML(copy & paste)
 <html>  
 <head>  
 <title>website1.com</title>  
 </head>  
 <body>  
 <h1>Yaaaaaay!!!Website1 setup successfully</h1>  
 </body>  
 </html>  
Save the file and exit
  • Create a sample page for the second domain
Type this in your terminal
 sudo subl /var/www/website2.com/public/index.html  
Add the following HTML (copy and paste)
 <html>  
 <head>  
 <title>website2.com</title>  
 </head>  
 <body>  
 <h1>Yaaaaaay!!!Website2 setup successfully</h1>  
 </body>  
 </html>  
Save and close the file when you are finished. 

Step 2
Create a virtual host configuration file

In order for Apache to serve this content, it is necessary to create a virtual host file with the correct directives. So instead of modifying the default configuration file located at
 /etc/apache2/sites-available/000-default.conf:  
directly, we will make a new one at

 /etc/apache2/sites-available/website1.com.conf:  
 
Write the following command in your terminal

 sudo subl /etc/apache2/sites-available/website1.com.conf  
Now add the following configuration block (copy & paste) to create a basic configuration file which is similar to the default but updated for our new directory and domain name.


 <VirtualHost *:80>  
   
    ServerAdmin webmaster@website1.com  
    ServerName website1.com  
    ServerAlias www.website1.com  
    DocumentRoot /var/www/website1.com/public  
   
    ErrorLog ${APACHE_LOG_DIR}/website1.com-error.log  
    CustomLog ${APACHE_LOG_DIR}/website1.com-access.log combined  
   
 </VirtualHost>  

Save and close the file when done.
Notice we've updated the DocumentRoot to our new directory and ServerAdmin to an email that website1.com site administrator can access. 


We've also added 2 directives:
ServerName which establishes the base domain that should match for this virtual host definition and ServerAlias which defines further names that should match as if they were the base name.
 

There is no established format but naming your configuration files based on your domain name is the recommended practice.
 

Repeat the same process for the rest
For domain 2 run the following in the terminal:

sudo subl /etc/apache2/sites-available/website2.com.conf  
 
Now add the following configuration block (copy & paste) to create a basic configuration file which is similar to the default but updated for our new directory and domain name. 
Use the correct domain name for your website

 <VirtualHost *:80>  
   
    ServerAdmin webmaster@website2.com  
    ServerName website2.com  
    ServerAlias www.website2.com  
    DocumentRoot /var/www/website2.com/public  
   
    ErrorLog ${APACHE_LOG_DIR}/website2.com-error.log  
    CustomLog ${APACHE_LOG_DIR}/website2.com-access.log combined  
   
 </VirtualHost>  

Enable Virtual Host Configuration File

Enable the virtual host file for the first domain using this command
 sudo a2ensite website1.com  
For the second site use
 sudo a2ensite website2.com  
Disable the default site defined in
 000-default.conf:  
using 
 sudo a2dissite 000-default.conf  
Next, we'll test for configuration errors using
 sudo apache2ctl configtest  
You should see the following output
 SyntaxOK
Restart Apache to implement your changes
 sudo systemctl restart apache2  

Apache should now be serving your domain name. So navigate to http://website1.com and http://website2.com where you should see the message we put in the index.html files of each website.

After setting up your virtual host successfully, you can host your domain somewhere if you have a public IP address and anyone can find your site using your domain name via a web browser.

If you encounter any problems while following this guide, please leave a comment down below or send me an email at nuffdailies@gmail.com. 

Let me know if it worked for you too. Enjoy!!

Comments

  1. Replies
    1. Thank you. Please try it if you haven't already and let me know if it works

      Delete
  2. this very informative

    gotta try this

    ReplyDelete
    Replies
    1. Thanks. Would love to know if it worked or you had any problems

      Delete

Post a Comment

Your input is valued. Please type something....