Thursday 5 June 2014

How To Setup Apache Virtual Host in Wamp server

How To Setup Apache Virtual Host Configuration

Using Apache Virtual Host, you can run several websites on the same server. For example, I can run both siteexample.com and myexample.com on a single physical server that has one Apache webserver running on it.
In this configuration, when Apache webserver receives a request, it looks for the hostname in the HTTP header, and depending on the host name, it servers different websites. This is very easy, as you need only one IP-address on that physical server but, you update the DNS with multiple website names pointing to the same IP-address. For all practical purpose, you’ll be using only Name-based virtual host configuration.
In the following example, the server contains only one NIC card, which is configured with 192.168.102.1 IP-address. The DNS entry for both siteexample.com and myexample.com website points to 192.168.102.1 IP-address. When Apache receives a request, it looks for the host name entry in the HTTP header, and serves the corresponding website.

Step I - Uncomment httpd-vhosts.conf in httpd.conf

In Linux machine
# vi /usr/local/apache2/conf/httpd.conf
Include conf/extra/httpd-vhosts.conf


In Windows wamp server
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf


remove # , which is in front of Include

2. Setup virtual hosts

Modify the httpd-vhosts.conf  (C:\wamp\bin\apache\Apache2.4.4\conf\extra\httpd-vhosts.conf)
as shown below to setup named-based virtual host setting for two hosts.

NameVirtualHost *:80 – Indicates that all the name-based virtual hosts will be listening on the default port 80
   
<VirtualHost *:80> </VirtualHost> – Enclose all the apache configuration parameters for each and every virtual host between these VirtualHost tags. Any apache directives can be used within the virtualhost container.

In the following example, we are setting up virtual host for siteexample.com and myexample.com listening on the same port 80. So, there will be two         <VirtualHost *:80> </VirtualHost>, one for each website.
When you go to siteexample.com, the files under C:/wamp/www/projects/siteexample will be served by Apache.
Example

<VirtualHost *:80>
    DocumentRoot "C:/wamp/www/projects/myexample"
    DocumentRoot "C:/wamp/www/projects/myexample"
    ServerName myexample.com
        <Directory C:/wamp/www/projects/>
        Order Deny,Allow 
        Allow from all
        </Directory>
</VirtualHost>


3.Restart the Apache and test

Now, when you go to siteexample.com (or www.siteexample.com), the apache will serve the files from /wamp/www/projects/siteexample directory. When you go to myexample.com (or www.myexample.com), the same apache running on the same server will serve the files from /wamp/www/projects/myexample directory.