Creating multiple virtual hosts/websites in Wampserver

Creating multiple virtual hosts/websites in Wampserver

To create multiple websites, it would be helpful to have each website setup on the local computer. With Wampserver (or just Apache) we can easily configure multiple websites. Following are the steps to create multiple websites using Apache’s configuration. This uses Apache’s “Named Virtual Hosts” configuration. I have tested these steps on Wampserver 2.1 running on Windows XP and Windows 7.

Step 1: Set your hostnames or setup Windows to recognize your local websites

After installing WAMP server you can go to the browser and type http://localhost and it opens up a page. Windows by default recognizes the site localhost as referring to itself. We need to setup windows to recognize our other local websites. To do this we need to edit the hosts file. This file is found under the following directory. (Another way to reach this directory is, open the run command or press Windows Key + R, and then type “drivers” and press “OK” then go to “etc”.)

C:/Windows/System32/drivers/etc

After going to this directory open the file named hosts in this directory using your favorite texteditor (like Notepad, Editplus, Notepad++).

Note: You may not be able to edit or save this file in Windows Vista or Windows 7. You need administrator rights to edit this file.

Now, you would see a line at the end of the file follows:

127.0.0.1 	localhost

This code tells your computer to connect to your own computer when we try to go to http://localhost
Now we should add our site to this file. Lets say our site name is site1. So we would add an entry for it in the hosts file. The updated file would look similar to the following:

127.0.0.1 	localhost
127.0.0.1 	site1

Save this file and try going to http://site1
This would take you to the same page as http://localhost
Now our computer knows what http://site1 is.

Note: Make sure you do not add host entries for external sites like google.com or bing.com. If you add these entries, then the computer would no longer be able to go to the actual websites.

Step 2: Create a folder for the website

Wampserver has only one folder or site root to host the website. This folder is typically C:/wamp/www. We should create a different folder for each website. We can create the folder in any directory or drive. (If we create the folder outside of C:/wamp/www we would need some additional settings in Apache described later).

Add some test file in this folder like “index.html” in say C:/wamp/www/site1 directory.

Step 3: Setup Apache to serve multiple sites

We would change the file httpd.conf and httpd-vhosts.conf for the Apache setting.
Open the file httpd.conf by clicking the WAMP server icon and selecting “Apache -> httpd.conf”. This file is typically located at C:/wamp/bin/apache/Apache2.2.17/conf.

Don’t add code to this file, its not a clean and easy way. We just want to un-comment one line in this file that include the vhosts file.

#Include conf/extra/httpd-vhosts.conf

Remove the # sign from the front of the line, the changed line should look as follows:

Include conf/extra/httpd-vhosts.conf

Now, we will open the file “httpd-vhosts.conf”, which would be typically located inside the extra directory (C:/wamp/bin/apache/Apache2.2.17/conf/extra)

In this file we will add virtual host using the following code:

<VirtualHost *:80>
	ServerAdmin [email protected]
	DocumentRoot "C:/wamp/www"
	ServerName localhost
	ServerAlias www.localhost.com
	ErrorLog "logs/localhost-error.log"
	CustomLog "logs/localhost-access.log" common
</VirtualHost>

In the above code the ServerName and DocumentRoot are mandatory, other options are optional.

So if we want to have 3 sites on our local machine as follows
localhost -> C:/wamp/www
site1 -> C:/wamp/www/site1
site2 -> C:/site2

<VirtualHost *:80>
	ServerAdmin [email protected]
	DocumentRoot "C:/wamp/www"
	ServerName localhost
	ServerAlias www.localhost.com
	ErrorLog "logs/localhost-error.log"
	CustomLog "logs/localhost-access.log" common
</VirtualHost>

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

<VirtualHost *:80>
	DocumentRoot "C:/site2"
	ServerName site2
	<Directory "C:/site2">
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		Allow from all
	</Directory>
</VirtualHost>

I have not used all the options for site1 and site2, but it is a good practice to have them. Also notice that the code for site2 has some extra code for Directory. This is necessary to give the server access to that directory. If we don’t add this, the server will not be able to access the files in this directory and hence the site would not work. We don’t need this for site1 as its inside C:\wamp\www directory for which the directory setting are set by defult in wampserver.

Now restart Apache and all your sites should be working. Make sure you add site2 to your hosts file and create the directory for it at C:/site2.

Note: I do not take responsibility for proper functioning of the above mentioned steps under all circumstances. If you download any files, programs from my blog then make sure you protect yourself. I am not responsible for any damages to your computer, website, blog, application or any thing else. I am not affiliated with or do not endorse any of the above mentioned sites.

  1. Hi Virendra,

    Wonderful and helpful post, thank you. In your example, can site2 be on a different local disk like D:/site2?

    Eg.

    localhost -> c:wampwww
    site1 -> c:wampwwwsite1
    site2 -> d:site2
    —————————————————————————————-

    ServerAdmin [email protected]
    DocumentRoot “C:/wamp/www”
    ServerName localhost
    ServerAlias http://www.localhost.com
    ErrorLog “logs/localhost-error.log”
    CustomLog “logs/localhost-access.log” common

    DocumentRoot “C:/wamp/www/site1″
    ServerName site1

    DocumentRoot “D:/site2″
    ServerName site2

    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all

  2. Thank you for your reply Virendra, but what do you mean by as long as the disk is accessible by apache server? I assumed configuring C:wampbinapacheApache2.2.17conf and httpd-vhosts.conf as per your instructions would give apache access alread? What else would I have to do then to make Apache accessible to my D drive? Thank you for your help.

  3. Ive read closely vhosts setup on here and on other sites and attempted several times, but keep making mistakes and as a result have had to take sites offline. I am not efficient with tech language and reading guidance notes on Apache site is just making me go blind. I fresh installed wamp with no changes to configs with view to starting from scratch. I have a business web in www folder and a personal web in a sub folder till I can work out what to do. I do use no-ip, and the web urls for each site do reflect the nature of each one (http://businesssite.serveblog.net and Personalsite.hopto.net) but obviously they both come through one router and one port 80 to one machine, and only Business site serves up.

    My problem is that the basic setup of WAMP with my Business website in root folder and Personal website in subfolder means all image folders in the business and personal folders show up in the google search. I assume this is because the sites are served from the same machine. How do I serve a business website and a personal website through 1 router from separate machines or ip addresses? This is the only way I can think of to keep business files and personal files separately from showing up in google images when entering url from one site or the other.

    Any advice would be most appreciated.

  4. This was a great little tutorial, thank you. However I must note that I am unable to see the required VirtualHost and Directory tags … I was fruitlessly attempting to add the example code without the Directory tags at all before I found another resource on the web using the tags:

    http://forum.wampserver.com/read.php?2,85338

  5. Hi Virendra,

    Is it safe to use this as a web server in a production environment?

  6. Great Article…Worked fine for me…

  7. Hey, so I have a question. With these several different websites, I want to use No-Ip so people can connect to them. How would I go about this? I use Wamp.

  8. How can I get WAMP, VIRTUAL HOSTS, Websites in MULTIPLE PARTITIONS & NO-IP working?

    I cannot get this to work!!!:-(

    FRESH INSTALL OF LATEST WAMP VERSION 2.4
    WINDOWS 7 HOME PREMIUM

    I want to know how to serve several websites with each one on a different partition of the harddrive and for them to be accessible to the public using no-ip host direct service! I have looked at so many guides, video tutorials, created Aliases, virtual hosts, configured Options Indexes FollowSymLinks directives and I still can’t get this *** thing to work!

    There is no step by step guide and so many people have different requirements and so many people offer different solutions. And so many people it works for and so many people it doesn’t! I am lost in a pool of so much information with very little to go on. I’m not a techie but I’m not a newbie at reading or trying to understand either. I can do some but cannot seem to find where I’m going wrong. I don’t want to be referred to more long winded websites, guides or manuals. I would like an easy step by step guide from fresh install to full config of Wampserver to host multiple sites that can be viewed by the public or show me where I’m going wrong in this. Can anyone who knows what to do, speak intelligibly but simply and show me where i’m going wrong? and answer these questions?

    DocumentRoot “M:/siteA”
    – Does Document root mean, path AND folder of website pages?

    ServerName siteA
    – Does Servername mean Name of PHYSICAL Computer with websites in it or the folder name with the web pages are in?

    – is this just M:/ ? as in just the partition letter assigned or do I add the folder name as well..

    and this bit

    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all

    does it allow the public to see my websites? … Does it go in http.conf or vhost.conf?

    ———————————–
    THIS IS MY SETUP:-(
    ———————————–

    Status: FRESH WAMP install W:/wamp, and W:/wamp/www
    No Aliases created.

    Wamp light stays on orange

    ————————–
    This is my etc/host file
    ————————–
    127.0.0.1 localhost
    ::1 localhost

    127.0.0.1 siteA.idontknow.com
    127.0.0.1 siteB.whatthehellimdoing.com
    127.0.0.1 sitec.hopeisagoodthing.com

    ————————————————-
    This is my http.conf file
    ————————————————-
    Listen *:80 instead of Listen 80
    Include conf/extra/httpd-vhosts.conf uncommented

    ————————————————
    This is my httpd-vhosts file
    ————————————————
    # VirtualHost example:
    # Almost any Apache directive may go into a VirtualHost container.
    # The first VirtualHost section is used for all requests that do not
    # match a ServerName or ServerAlias in any block.
    #

    ServerAdmin [email protected]
    DocumentRoot “w:/wamp/www”
    ServerName localhost
    ServerAlias http://www.localhost.com
    ErrorLog “logs/localhost-error.log”
    CustomLog “logs/localhost-access.log” common

    DocumentRoot “M:/siteA”
    ServerName siteA.idontknow.com

    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all

    DocumentRoot “I:/siteB”
    ServerName siteB.whatthehellimdoing.com

    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all

    DocumentRoot “S:/siteC”
    ServerName siteC.hopeisagoodthing.com

    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all

    —————————————————–

    RESULT

    No Wamp page, no local host..

    So, Ive uninstalled and started again..

    Currently, re-installed WAMP, back to square one for fresh start if anyone can help guide me through. WAMP Green, Localhost page running!

    • Above Example article is 100% Easy and Very Very Good ,Please check properly once again ,it easy to setup virtual host.If u have not setup ,then feel freely contact me i install via team viewer no hidden-costs.
      ([email protected]).

  9. thats great help. very good and right stuff to config virtual hosts in wamp 2.4

  10. Great example. It was really useful for me. Tnx tnx

  11. Thank you for a great article. The reasons for doing each step was clearly explained.

  12. hello, I am having problems connecting my new project to phpmyadmin.

    New project is located under the www folder, when i preview the php file in my browser it only shows the php code but not the design webpage.

    Also, if i try to access the page view the ‘localhost’ interface, the webpage design is showing but the php scripts are not executing.

    Does anyone know what I am missing? and what I should do to fix this?

    ps: I have another project in my www folder where all the php scripts and previews on browser are working fine.

    Thanks in advance.

    • You probably have php execution disabled in that folder. I would check any .htaccess file and also the apache config.

      • Thanks for the reply Virendra.

        Just to make sure, in other words I should enable php execution via the .htaccess file and apache config?

        I have one more question, does this have to do with setting up multiple virtual hosts, in order for this new project to function with php and php myadmin?

        Thanks again.

        • I meant to say, that you might have it disabled via .htaccess or apache config. By default the PHP execution should be enabled. So check you config files and .htaccess files to make sure you have not disabled it accidentally. I cannot tell you exactly what needs to be changed without seeing your setup.

  13. My site1 is acting like a second localhost, it’s just bringing me to the /www root

  14. i have installed wampserver, and in the root directory i have made a website. now i want to put it online that other users can access it through internet. and i am using Apache Version :
    2.4.9 and windows 8. please any one help me that how can i do it step by step

  15. Thanx 😀 you are a life saver 😀

  16. Thanks!!! I tried many tutos but yours is the best! Well explained (y)

  17. Hi,

    I have a website at c:\wamp\www\site1 in Computer A

    I would like to open this site from Computer B. Both A and B are in LAN (Wifi and Ethernet). How do I do that. I am new in Website development. I have followed your tutorial but was not able to do that.

    I m using windows 7 32 bit and win 8.1 64 bit with WAMP stack

  18. how do i browse the site hosted with WAMP without inputting the ip
    like 123.456.56.120/site1.com
    i want it like http://www.site1.com
    it worked on my laptop but when i tried from another merchine, i got DNS not found error

    • Your local machine is located in local host server ,but you go to access in another machine is not possible to access your local host machine,it searching in server is not found domain name then it through the DNS error.

      One Thing you can do , if it possible u connect another machine with your local network , then access your local host then check your project is accessible.

      Note: if any mistake in English ,extremely sorry .(not good my English)

  19. Thankyou for above shared virtual host create in windows

  20. Thanks for your post. You have done a very good explanation.

  21. If you open the httpd.conf file and there are no spaces (i.e. everything is bunched up onto about 10 lines) try opening the file in a different program like Notepad ++ (free)

    It stumped me for about an hour! I was making the change and because it was all scrunched up it just messed the file up!

  22. This is a lifesaver. Thanks ever so much

  23. This seemed to work correctly, but you really need to add instruction for people new to this kind of thing. Do I need to load the WordPress files into the folder i built for site2 following your instruction? c:/site2. If not how do I get a WP site to use Wamp from site2? Thanks

    Zuke

  24. since doing this i am unable to access phpmyadmin and my squirelmail, says not found /phpmyadmin is not found on this server and they were working before is there a way i can get around this.

  25. Excellent Tutorial. I am now on my third site design using Wampserver.and i am not, an expert. Thank you. If, you plan on doing a tutorial on how to enable Paginattion then let me know as after ten days research i am no still unsure as to which files go where.

  26. For windows users there’s a trick!!! Don’t simply run Notepad – Right click and run as administrator. THEN, file>open and goto your drivers folder. You will need to change the setting from txt to all files to see “hosts”. This is how you crack the seemingly uncrackable windows hosts file. If you try to get admin rights directly over the file itself it’s not likely going to happen. So opening the editor as the admin is the workaround. It took me a month to discover this so don’t feel bad! But then I have been tired….
    All credit goes to this guy: http://www.rackspace.com/knowledge_center/article/modify-your-hosts-file

    Tested and working on Win8.1 on 9/22/15 – Also instructions for vista users as well!

  27. Unfortunately, beyond cracking the host file. I was completely unsuccessful. site1 resolves to where localhost is as well as site2. Folders were created in proper directories. vhost file code copied verbatim. httpd.config was uncommented out. Sample test with your link to site1 worked resolving to the location of localhost but under the new name site1. I do use mod_rewrite but that shouldn’t be an issue.

    So basically I can access localhost now under 3 names. DID turn all wamp services off during, and then back on, due to apache’s tendency to unrecoverable crash. Taking apaches beyond numerous faults as noted and agreed this still doesn’t help me. My objective in doing this was that hopefully I would never have to touch apache again as I’m sick n tired of reinstalling wamp over and over.

    further attempts concluded 3am CST 9/22/15
    http://stackoverflow.com/questions/26113258/wamp-virtual-host-not-working
    This doe not resolve the issue. Yes I stopped and restarted the dnscache as admin.
    This is NOT A WINDOWS ISSUE….as usual it is an apache one. Unfortunately, there are so sooo many “apache issues” I don’t even know where to begin to look. Getting tired…won’t be getting any rest soon…sigh.

    Currently the following setup DOES NOT work
    Reasons: Unknown
    Solution: Pending – Possible reinstall needed
    Win 8.1
    Wamp2.5
    Firefox 40?? top of my head…are we up that high now? WOW
    Still appreciate the tutorial…this is the furthest I’ve gotten in 14 months playing with wamp. Looks like I’m gonna be up all night tho. If I find the tutorial on how to ONCE AGAIN fix apache’s (bleep)ups and piss pour design …I’ll be sure to post the link.

  28. And apache crashed. Thanks tech republic for some awful advice….
    Just lost my databases..yeah…awesome! Orange light of death…god bless the french for at least picking a pleasant color to tell you you’re screwed. hahahaha
    Thank god I’ve come to expect this and backed up the important stuff.

    I wonder if I call Microsoft? I’m sure they get blamed for apache’s malfunctionware all the time!

    Current status…Win8.1 & Wamp2.5….virtual hosts have yet to work to this day! PERIOD!

  29. Really very nice and it is very useful for me…. Thank you…

  30. Hi Virendra,

    I created a site and now I’m trying to installa multisite. I have a Wampserver 2.5 installed on Win Vista. I tried and doesn’t work. Do you have any tips?

    Thanks,

    Heli

  31. Great sharing, do you have a sample of working one?

  32. Note: to make this work for the website on c:\site2 I had to add a line “Require local” inside the entry

  33. It’s working 100% 🙂

Leave a Reply

Trackbacks and Pingbacks: