Setup multiple sites by Virtual Host on Apache server
Virtual Host is a very powerful feature of any server. By using this feature, you can run multiple sites on a single server that has working apache server and share its resources like memory and processor cycles.
We can setup virtual host on any server by two ways which are alomost similar but quite different in way of implementation. Here are the details :
Domain based host
Most of the cases or scenarios recommend this domain based virtual host because of it's independent structure. When server receives a request, it looks for the hostname in the HTTP header and on the basis of hostname, it calls a assigned directory to that particulat domain name. In this easy way of virtual host, you just need to update the DNS of your hosting with multiple domain names pointing to the single ip address.
For example, Here we have a flow diagram of multiple domains pointing to a ip address of single server.
Here are the steps for implementation of domain based virtual host :
a) Initialize vhosts file
In this step, you just have to uncomment a line of code in httpd.conf file at path i.e. /usr/local/apache2/conf/httpd.conf. This uncommented line will initialize virtual host component inside your apache server. Here is line which you have to uncomment.
b) Virtual hosts configurations
After initializing of virtual host, we have to configure our vhosts file i.e. httpd-vhosts.conf according to our requirement. As mentioned in above flow diagram, we are going to modify existing httpd-vhosts.conf file at path i.e. /usr/local/apache2/conf/extra/httpd-vhosts.conf with our custom entry of virtual hosts. Here is the code:
ServerAdmin firstname.lastname@example.org DocumentRoot "/usr/local/apache2/docs/etutorialz" ServerName etutorialz.com ServerAlias www.etutorialz.com ErrorLog "logs/etutorialz/error_log" CustomLog "logs/etutorialz/access_log" common ServerAdmin email@example.com DocumentRoot "/usr/local/apache2/docs/ezeetools" ServerName ezeetools.com ServerAlias www.ezeetools.com ErrorLog "logs/ezeetools/error_log" CustomLog "logs/ezeetools/access_log" common
After updating httpd-vhosts.conf, just save it and move to next step given below.
c) Restart apache server
sudo service apache2 restart
After performing above three step, you are ready with your multiple domains pointing to different directories at same location.
IP based host
This is also a way for pointing two different ip address to two different domains respectively. In this scenario, server must have multiple ethernet cards and configured with ip address of the website that virtual host serving.
For example, Here we have a flow diagram of multiple domains pointing to a multiple ip address of single server.
At the end of this post, we'll recomment to use domain based virtual host if you are having limited resources to host multiple websites.