Creating Virtual hosts on WAMP server

Hello, here is a quick tip on how to create virtual host on WAMP server for a Windows machine. You might be running a web script in a sub folder that points to the root document $_SERVER['DOCUMENT_ROOT']; but all you get is your WAMP server homepage; you can change the DOCUMENT_ROOT variable to point to the folder your website was installed in by creating virtual hosts.

Lets assume we want to create a virtual host for our website “stackarena” located at C:\wamp\www\stackarena\ so that we can access it from our browser using http://stackarena.localhost/. Here is how we can do that.

First we navigate to "C:\windows\system32\drivers\etc". Once there, you should see a file titled “hosts”. Open that file with a text editor like notepad. You may have to change the open dialog to “all files” in order to see it. You should see a line that looks like this:
127.0.0.1 localhost add
127.0.0.1 stackarena.localhost bellow that line.

You can add as many entries as you like and your browser should display your WAMP Server site for all of them. Save your hosts file and try visiting http://stackarena.localhost” in your browser. You should see your WAMP site index page.

Now lets configure our virtual hosts.

WAMP configures Apache to serve a single site that usually lives in “C:\wamp\www”. Apache can handle multiple sites if we tell it where to look. To open your WAMP Server Apache configuration, left-click the WAMP Server icon and select “Apache – httpd.conf”.

Look for a line that has:
#Include conf/extra/httpd-vhosts.conf now remove the # sign from that line and save the file. This will enable Virtual hosts for WAMP.

Next, navigate to C:\wamp\bin\apache\apache2.2.11\conf\extra and open the file “httpd-vhosts.conf”. Scroll all the way to the bottom of this file and remove all dummy virtual host entries and add the code bellow.

<VirtualHost *:80>
  DocumentRoot C:/wamp/www/stackarena/
  ServerName stackarena.localhost
</VirtualHost>

You should now have something like this:

#
# Virtual Hosts
#
# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.2/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

<VirtualHost *:80>
  DocumentRoot C:/wamp/www/
  ServerName localhost
</VirtualHost>

<VirtualHost *:80>
  DocumentRoot C:/wamp/www/stackarena/
  ServerName stackarena.localhost
</VirtualHost>

The first of these directives is needed to preserve the dashboard i.e the WAMP server homepage. The second directive is the virtual host, for which we have the directory “C:/wamp/www/stackarena/”.

In order to create additional virtual hosts, we’ll just repeat the steps again for a new website.

Finally, restart Apache and verify that you can access the website through the virtual host name. After restarting Apache using WAMP Server you should be able to access http://stackarena.localhost/ using your web browser.

I write codes... web, mobile, desktop and hack stuffs

Leave a Reply

Your email address will not be published. Required fields are marked *