Virtual Hosts - all redirect to the WAMP localhost 'home' page

22,037

Solution 1

I followed this guide and it worked fine

I had to change the path...

<VirtualHost *:80>
 ServerAdmin [email protected]
 DocumentRoot "c:\wamp\www\client1"
 ServerName client1.localhost
 ErrorLog "logs/client1.log"
 CustomLog "logs/client1-access.log" common
</VirtualHost>

In addition to this.. I find this is a good quick guide for virtual hosts with WAMP http://www.techrepublic.com/blog/smb-technologist/create-virtual-hosts-in-a-wamp-server/#

Solution 2

Your hosts file looks good, but your virtual hosts definitions are not so good.

If you change your hosts file you can reload the windows cache by doing this from a command window that has been launched using Runs as Administrator or a simple reboot :-

net stop "DNS Client"

then when that completes do

net start "DNS Client"

Quotes required as there is a space in the service name!!

The DNS Client service caches domain names accessed and is pre loaded with domain names that exists in the HOSTS file at boot time or if you restart the service as above.

When debugging a new vhost definition, remember, that if something is wrong with a definition you are attempting to access, Apache will always default to the first vhost defined in the vhost definition file. So if thats where you end up i.e. the WAMP homepage, you can assume you made a mistake defining that vhost.

This also means that if you define that first vhost definition with something like Require local a random hack on your system should also be sent there, if that has its security set to Require local the hack should receive a 404 error which might discourage further hack attempts.

// My virtual hosts file
<VirtualHost *:80>
  ServerName localhost
  DocumentRoot "C:\wamp\www"
  <Directory "C:\wamp\www">
     AllowOverride All
     # never want to allow access to your wamp home page area to anyone other than This PC
     # plus us the Apache 2.4.x syntax and not the 2.2 syntax
     Require local
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName client1.localhost
  DocumentRoot "C:\wamp\www\client1"
  <Directory "C:\wamp\www\client1">
     AllowOverride all
     # use Apache 2.4 syntax to all access to your internal network only
     Require ip 192.168.0
     # Or if you really want to give access to the whole internet uncomment this and comment the previous line
     #Require all granted
 </Directory>
 DirectoryIndex index.html index.php
</VirtualHost>

<VirtualHost *:80>
  ServerName client2.localhost
  DocumentRoot "C:\wamp\www\client2"
  <Directory "C:\wamp\www\client2">
     AllowOverride all
     # use Apache 2.4 syntax to all access to your internal network only
     Require ip 192.168.0
     # Or if you really want to give access to the whole internet uncomment this and comment the previous line
     #Require all granted
  </Directory>
  DirectoryIndex index.html index.php
 </VirtualHost>

If you don't actually want the world to be allowed access to these client sites, but you do want to be able to access the site from other PC's on your internal network then a better access mechanism would be to use Require ip 192.168.0. Note the use of just the first 3 quartiles of your subnet ( yours may not be 192.168.0, but a lot of routers default to that. Check your subnet first )

Also if you do want the world to see these client sites then you have to Port Forward your router as well.

Also if you didn't intend to give access to these site to the whole world, but were just following bad advice, a much more secure definition of all these sites would be to use Require local so you can only access them from the PC running WAMP.

WAMPServer 2.4, which I assume you mean when you say you are running the latest release of WAMPServer actually changed the the way that you can include vhost definitions. Well actually it included a new way and kept the old way as well.

So to include the vhost definition you can do one of these 2 things :-

1. Put your vhost definitions into the file \wamp\bin\apache\apache2.4.4\conf\extra\httpd-vhosts.conf and then in thehttpd.conf file uncomment this line ( its near the bottom of the conf file.

# Virtual hosts
#Include conf/extra/httpd-vhosts.conf

Remove the # infront of the Include line

# Virtual hosts
Include conf/extra/httpd-vhosts.conf

2. Place your vhost definitions into a file 'called anything you like' into the \wamp\vhost folder.

There is a line at the bottom of the httpd.conf file now that say IncludeOptional "d:/wamp/vhosts/*" This will include any file in that folder and if it is a vhost definition it will apply that to the config. This is a new command to Apache 2.4 I believe so will only work on Apache 2.4.x installs.

Solution 3

Firstly, your directory structure should never go above the www folder for security purposes.

- c:\wamp\www (home)
    -c:\wamp\www\client1 (client1)
    -c:\wamp\www\client2 (client2)

Secondly changed in the vhost require a reload, but in your case, you must do a restart as wamp doesn't offer reload.

Solution 4

Try to put your client folders under C:\wamp\www\ like this C:\wamp\www\ client1 and C:\wamp\www\client2

EDIT

If still doesn't work try this change your hosts file with client1.dev instead of client1.localhost and then put a Servername in you Virtualhost settings like this

<VirtualHost *:80>
DocumentRoot "F:\www\client1"
ServerName client1.dev
ServerAlias client1.dev www.client1.dev
Options Indexes FollowSymLinks
<Directory "F:\www\client1">
    AllowOverride All
    Order Deny,Allow
</Directory>
</VirtualHost>

You have to restart again to make the changes in hosts file work

Share:
22,037
Zabs
Author by

Zabs

PHP Developer

Updated on July 09, 2022

Comments

  • Zabs
    Zabs almost 2 years

    I've setup the latest version of WAMP on my Windows 8 PC - I cannot seem to get multiple virtual hosts to work, every local URL I load will show the WAMP homepage.

    Can anyone explain what I am doing wrong?

    // My hosts file
    127.0.0.1   localhost   
    127.0.0.1   client1.localhost
    127.0.0.1   client2.localhost
    

    I have two folders in my WAMP directory, 'client1' and 'client2' obviously each folder will relate the the client1 & client2 in the host file above.

    // My virtual hosts file
    <VirtualHost *:80>
      ServerName localhost
      DocumentRoot "C:\wamp\www"
    </VirtualHost>
    
    <VirtualHost *:80>
      ServerName client1.localhost
      DocumentRoot "C:\wamp\www\client1"
    <Directory "C:\wamp\www\client1">
      allow from all
      order allow,deny
      AllowOverride All
    </Directory>
     DirectoryIndex index.html index.php
    </VirtualHost>
    
    <VirtualHost *:80>
      ServerName client2.localhost
      DocumentRoot "C:\wamp\www\client2"
    <Directory "C:\wamp\www\client2">
      allow from all
      order allow,deny
      AllowOverride All
    </Directory>
      DirectoryIndex index.html index.php
     </VirtualHost>
    
  • Zabs
    Zabs over 10 years
    oops - i fixed it to put the www within the paths, still having the same problem after the apache restart
  • Zabs
    Zabs over 10 years
    yeh - i fixed that now, although same problem persists after WAMP restart
  • Andreas P.
    Andreas P. over 10 years
    Try a reboot to your system not wamp restart
  • Zabs
    Zabs over 10 years
    Still no joy after my PC restart? I'm not at home so cannot try anything until later this evening..
  • Andreas P.
    Andreas P. over 10 years
    Have you checked my edit post with changing in virtualhost settings?
  • Zabs
    Zabs over 10 years
    I'll give that a shot later tonight - thanks touch wood that will work :)
  • Zabs
    Zabs over 10 years
    wow.. lot of info there mate, nice one :) I'm not at home yet so cannot try your tips til later thanks again :)
  • RiggsFolly
    RiggsFolly over 10 years
    There is no reason why a Virtual Host should be below the \wamp\www folder. In fact if done properly it is best to put virtual host sites completely outside that folder structure. Then the security you place on each site is completely seperate from the wamp default security
  • RiggsFolly
    RiggsFolly over 10 years
    WAMP Server DOES provide a simple mechanism to reload service. Either both Apache and MySQL or Apache or MySQL individually. Left click the wampmanager and see the menu options.
  • Smudger
    Smudger almost 10 years
    Thanks for the effort in answering this question, users like you on this site are highly appreciated.
  • clod986
    clod986 about 9 years
    Remember to uncomment the include!! That took me a lot to figure out