Setting Up Virtual Hosts on a WampServer

Like many Web Developers who use Windows, I prefer writing and testing my applications locally. But just because I’m a “Windows” guy doesn’t mean I use IIS, ASP, and MS-SQL . On the contrary, I almost exclusively work with Apache, PHP, and MySQL. So naturally, my tool of choice for installing and managing a local server is WampServer.

I’ll be honest. I’m really not a systems guy and I all but hate the linux terminal (I’ve made my peace with it but I find myself a much happier developer when I don’t have to bother with it). With WampServer, you simply download and install it and your local development server is running. Beautiful.

WampServer even lets you turn on and off PHP modules with one click. You can download and switch PHP versions just as easily, which is great for testing. Finally, WampServer gives you easy access to phpMyAdmin, an important tool for database management.

So WampServer is pretty easy to setup, but there is one vital configuration that you can’t access with just a click: virtual hosts. Alas, the Apache config file that I’ve tried so hard to avoid needs to be manually updated every time I want to add or remove a virtual host. But, as it turns out, it’s actually quite easy if you can remember the following steps.

The following configuration is being done on a Windows Vista box running WampServer 2.0. You should always make a backup copy before editing any configuration file.

  1. Click on the WampServer icon in the taskbar (it looks like half of a clock)
  2. Select Apache > httpd.conf
  3. Scroll down to the very bottom of the file and append the following:
    ServerAdmin    your@email.com
    DocumentRoot   c:/path/to/wamp/www/
    ServerName     yourdomain.local
    ErrorLog       c:/path/to/logs/yourdomain.local-error_log
    TransferLog    c:/path/to/logs/yourdomain.local-access_log
    

    Make sure you update the paths and only use forward slashes. Also, if 127.0.0.100 is already taken, increment 100 to the next available number. You cannot have two virtual hosts with the same IP address.

  4. Save the httpd.conf file.
  5. Now, open your hosts file in a text editor (I.E. Notepad). In most cases, the hosts file is located at c:\windows\system32\drivers\etc\hosts.
  6. Append the following line to the hosts file: [apache]127.0.0.100 yourdomain.local[/apache]If you used a different IP address in the httpd.conf file, use the same one in the hosts file.
  7. Save the hosts file
  8. Using the WampServer icon in the taskbar, select “Restart All Services”

Once Apache restarts, you will be able to navigate to http://yourdomain.local/ and see your development website. If you get a server configuration error or your website isn’t appearing, check both configuration files. Make sure that your paths are correct and that you used a unique IP address for both the httpd.conf and hosts files.

Of course, to remove a virtual host you would simply reverse these steps, deleting all of the appropriate blocks from both configuration files.

If you enjoyed this article, please share it with a friend!

18 Responses to Setting Up Virtual Hosts on a WampServer

  1. Dave says:

    Extremely useful, that was exactly what I was looking for. Thanks!

  2. Vivekanand says:

    Yeah what you have written in this article is right, I do the same in my wamp server. Really WAMP server is useful for running any PHP related application locally for testing and then after testing we can upload the same to the respective server.

    This is awesome enough, no doubt about it.

    Thanks,
    Vivek

  3. Shawn says:

    This is another way to accomplish the same thing. Works on my maching might work on yours? ;-)

    In this configuration you can set your hosts file to:

    yourdomain.local 127.0.0.1

    It also includes the rewrite rules needed for zend framework
    assuming your directory structure looks like:

    virtual_dir/application
    virtual_dir/library
    virtual_dir/public_html – this would be the only directly accessible directory


    ServerName yourdomain.localhost
    ServerAlias *.yourdomain.localhost
    DocumentRoot c:/path/to/wamp/www/virtual_dir/public_html
    ErrorLog c:/path/to/wamp/logs/yourdomain.local_error_log
    CustomLog c:/path/to/wamp/logs/yourdomain.local-access_log combined

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule !\.(js|ico|gif|jpg|png|css|swf)$ /index.php

  4. Saqib Ansari says:

    Is there anyway to run IIS and wampserver on windows 2003 server?
    I’m running number of sites on IIS and want to install wampserver too to run PHP site on apache platform.

  5. Cory LaViska says:

    @Saqib: Good question. I’m sure you can get them both running, but I can’t see the point in having two web servers running concurrently on the same box. You’re better off either a) installing PHP for IIS (http://www.php.net/manual/en/install.windows.iis.php) or b) running it on another box altogether (or virtual machine).

    When it comes to servers, I’m more of a Linux guy anyways :)

  6. Jacob Ball says:

    I’ve just posted my steps at another blog, but wanted to post them here too, as this site helped along the path of WAMP enlightenment too :)

    I’ve just installed WAMP Server 2, using the above instructions as a starting point, but I’ve done it slightly differently:
    1. Edit hosts file (as suggested) and put 127.0.0.1 localhost.mysite
    2. Edited httpd.conf file by UNcommenting the lines:
    LoadModule userdir_module modules/mod_userdir.so AND
    LoadModule vhost_alias_module modules/mod_vhost_alias.so
    and further down:
    Include conf/extra/httpd-userdir.conf AND
    Include conf/extra/httpd-vhosts.conf
    3. Edited httpd-userdir.conf by adding:

    Options All
    AllowOverride All
    Order allow,deny
    Allow from all

    4. Edited httpd-vhosts.conf by adding:

    DocumentRoot C:/path/to/files
    ServerName localhost.mysite
    ErrorLog “logs/localhost.mysite-error.log”
    CustomLog “logs/localhost.mysite-access.log” common

    I needed to put the ErrorLog and CustomLog lines in for mine to work.
    5. Restarted all services, and voila!
    Hope that helps someone too.
    Cheers,
    Jacob

  7. Shiki says:

    I must say, this looks like a copied post from this article:

    http://jagztalk.com/multiple-virtual-hosts-on-wampserver.html

  8. Cory LaViska says:

    @Shiki – that’s a hell of an accusation considering that article was written four months *after* mine.

  9. Shiki says:

    Sorry. I actually meant that that website is copying yours :)

  10. Shiki says:

    Now I see that I have wrong sentence construction there. This should be right:

    I must say, this guy looks like he’s copying from your post. There are sentences that look exactly the same as yours:

    http://jagztalk.com/multiple-virtual-hosts-on-wampserver.html

  11. Cory LaViska says:

    @Shiki: no worries :) It is a pretty similar article and I wouldn’t doubt it if the author was, at least, inspired by my post. Oh well :-\

  12. Fathi says:

    true true true ,now let is move to next step how to access to your virtual domain not locally only

  13. Paul says:

    Better than http://sysadminblog.net/?p=133. Difference between ‘inspired’ and ‘copied’

  14. Mei says:

    Great article, although I had to add this to the httpd.conf as I was getting a 403 error:


    Options Indexes FollowSymLinks
    AllowOverride all
    # onlineoffline tag – don’t remove
    Order Allow,Deny
    Allow from all

  15. Abeon says:

    ^ I got errors until I added that into httpd.conf too. Thanks for posting that. I looked everywhere!

  16. mike says:

    Thanks bro!.. realy usefull and worked smoothly!..

    - mike, malaysia

  17. Nicholas says:

    I have ports forwarded, 80 and 443, server is online but i can not get to it with my ip. anyone have an idea?

  18. jay bharat says:

    How to make virtual host in wamp apache

    Close your eyes for 2 minute then relex.
    Step1: Open
    C:\WINDOWS\system32\drivers\etc\hosts file in pspad
    then it looks like
    # 102.54.94.97 rhino.acme.com # source server
    # 38.25.63.10 x.acme.com # x client host

    127.0.0.1 localhost

    now add this line

    127.0.0.1 admin.com

    now it look like this structure

    # 102.54.94.97 rhino.acme.com # source server
    # 38.25.63.10 x.acme.com # x client host

    127.0.0.1 localhost
    127.0.0.1 admin.com

    and save it

    Step2: open
    F:\wamp\bin\apache\apache2.2.6\conf\httpd.conf
    now go to the end of page and add your project like me(in my condition), I have added these lines
    #Include “F:/wamp/www/admin/”

    ServerAdmin your@email.com
    DocumentRoot F:/wamp/www/admin/

    so, it look like this:

    SSLRandomSeed startup builtin
    SSLRandomSeed connect builtin

    Include “F:/wamp/alias/*”
    #Include “F:/wamp/www/admin/”


    ServerAdmin your@email.com
    DocumentRoot F:/wamp/www/admin/

    congrates then restart your wamp server
    and write in IE or any browser
    http://admin.com
    Work done