How To Create Virtual Host In Wamp Server

Create Virtual Host In Wamp Server In Windows

When I was building my first website in 2008, I had to face a very common  problem when I uploaded the site on remote server. Actually it was a static website (that didn’t contain any configuration file for base urls or something else) and had a lot of pages. Every page of the site used absolute path in all it’s url’s such as http://localhost/css/ or http://localhost/images etc. When it was time to upload the website on web hosting server, I had to replace the server name “localhost” in all pages with my website domain name (www.example.com) which I purchased from a domain registrant website. Although, later I replaced all “localhost” with the help of Dreamweaver but it didn’t seem to be a perfect and standard solution.

I always use Wamp Server to create a local web server environment in my computer since I started learning website development. One day I read about Virtual Host In Apache on a blog post. It helped me to host all my local websites with their own virtual domain names on a single server that is localhost. So I would like to share everything that I did to make Virtual Hosts for my websites on Wamp.

By creating Virtual Host , you can host your local web projects with their own virtual domain names e.g. http://project1.local or http://www.project1.com. By doing this, you don’t need to put the server name as localhost in your project files. Just use the real website domain name (that you have purchased or will purchase) in your whole project and host it virtually on your Apache Server.

There are two kinds of Virtual Hosting is provided by Apache Web Server. The first one is name based virtual hosting and second is IP based virtual hosting. To work on your local Wamp Server, you should use name based virtual hosting instead of IP based virtual hosting because IP based virtual hosting is used in the projects where SSL certification is needed. In name based virtual hosting, multiple domain names can be hosted on a single IP address. Let me make it very clear with an example.

Suppose you work with Wamp server and develop websites for both for yourself and your clients. You keep all your files in the “www” root folder of Wamp Server. Am I right or not? You have to access all your websites with the url’s like http://localhost/site1_folder, http://localhost/site2_folder. You can make some virtual hosts for these websites and then you will not need to use localhost in your url. Your websites will be accessed with their own domain names on your local computer. Create your Virtual Hosts in Wamp with the following simple steps. All you need to make some changes in only three files and will have to restart the Wamp Server at the end of this tutorial.

Steps To Make Virtual Host For Your Websites:

1. Install Wamp Server in your computer if you don’t have already installed it. You will get a folder www in the wamp directory. Now suppose you have two websites in your www folder. First one is site1 and second is site2. So to access these websites, you will have to enter the url’s such as http://localhost/site1 and http://localhost/site2. You can access both folders by using the url’s http://www.site1.com and http://www.site2.com by creating two virtual hosts for both websites. Your directory structure will look like below screenshot:

Folder Structure For Your Websites

2. Enable Virtual Hosts in httpd.conf file: Now open the file httpd.conf file located  in the directory (D:\wamp\bin\apache\Apache2.2.17\conf). D: is the drive where I installed my wamp server. It may be something else according to you. Press CTRL+F and find the line “# virtual hosts” in this file. You will see a line “#Include conf/extra/httpd-vhosts.conf”. Uncomment this line and save it. You have successfully enabled the Virtual Hosting feature of your apache web server.

Enable Virtual Host In Your httpd.conf File

3. Create Virtual Hosts in the file httpd-vhosts.conf located in (D:\wamp\bin\apache\Apache2.2.17\conf\extra). Open it and go to the end of file. Now write the codes in the end of file as shown in below screenshot.

(a) In above screenshot, there are three block with red border have been shown. The first block is written for localhost and pointed to the directory D:/wamp/www. When you type localhost in the browser, your HTTP header will look into this directory.

(b) The second block is written for your first website site1. Now you don’t need to type “localhost/site1″ in the browser. You have to write only “www.site1.com” in the browser and it will automatically point your  HTTP header to (D:/wamp/www/site1) directory.

(c) The Third block is written for your second website site2. The code is same as second block with the path difference of your second website directory which is D:/wamp/www/site2. Access this website by typing http://www.site2.com in the browser. You may have other sites like site3,site4. So for them you will have to write the same code again to make their virtual hosts.

4. Make the entries of all your Virtual Hosts in Windows hosts.conf File: Open your windows hosts.conf file located in the directory C:\WINDOWS\system32\drivers\etc\hosts.conf. Make the entries of all your virtual hosts that you created in previous steps to start them properly as shown in below screenshot.

Make An Entry Of Your Virtual Host In hosts.conf File

Now restart your wamp server and run all your virtual hosts in the browser with following urls:
http://localhost                                 // will look into D:\wamp\www
http://www.site1.com                      // will look into D:\wamp\www\site1
http://www.site2.com                      // will look into D:\wamp\www\site2

So that was all about creating Virtual Hosts on Wamp Server in windows XP. The same procedure can be applied for windows 7 also.

Important Thing: You will not be able to access the website www.site1.com on the internet if you have registered it in virtual hosts. Suppose you registered google.com in your virtual hosts then google.com will not be accessed through internet until you remove it from virtual host list.

Tags:
No Responses to “How To Create Virtual Host In Wamp Server”
  1. [...] will be no longer able to run your zend project through above url. In this situation you have to make a virtual host on your Apache server. So it would be better to make a virtual host in wamp server to run your project either it is [...]