Posted by: RiggsFolly (
Date: October 02, 2014 06:05PM

WAMPServer Homepage - Virtual Hosts

Note: The use of VirtualHost is mandatory with Apache if you want to work properly with web applications, CMS or your local sites .
It is recommended - even imperative - to create a virtual host for each of your projects, even if you put them in the structure \wamp\www\subfolder.
The WampServer home page (\wamp\www\index.php) expects you to have created a Virtual Host for all your projects and will therefore work properly only if you do so.


In order to make life easier for beginners using WampServer to learn PHP Apache and MySQL it was suggested that you create subfolders under the `\wamp\www\` folder.

  |-- www
       |-- Chapter1
       |-- Chapter2
       |-- etc
These subfolders would then show as links in the WampServer Homepage under a menu called 'Your Projects' and these links would contain a link to `localhost/subfoldername`.

Acceptable only for simple tutorials

This made life easy for the complete beginner, and was perfectly acceptable for example for those following tutorials to learn PHP coding.
However it was never intended for use when developing a real web site that you would later want to copy to your live hosted server.
In fact if you did use this mechanism it often caused problems as the live sites configuration would not match your development configuration.

The Problem for real website development.

The reason for this is of course that the default DocumentRoot setting for wamp is

DocumentRoot "c:/wamp/www/"

regardless of what your subfolder was called.
This ment that often used PHP code that queried the structure or your site received different information when running on your development WampServer to what it would receive when running on a live hosted server, where the DocumentRoot configuration points to the folder at the top of the website file hierarchy.
This kind of code exists in many frameworks and CMS's for example WordPress and Joomla etc.

For Example

Lets say we have a project called project1 held in `wamp\www\project1` and run incorrectly as `[localhost]`

This is what would be reported by some of the PHP command in question:

 $_SERVER['HTTP_HOST'] = localhost
 $_SERVER['SERVER_NAME'] = localhost
 $_SERVER['DOCUMENT_ROOT'] = c:/wamp/www

Now if we had correctly defined that site using a Virtual Host definition and ran it as `http: //project1` the results on the WAMPServer devlopment site will match those received when on a live hosted environment.

 $_SERVER['HTTP_HOST'] = project1
 $_SERVER['SERVER_NAME'] = project1
 $_SERVER['DOCUMENT_ROOT'] = c:/wamp/www/project1

Now this difference may seem trivial at first but if you were to use a framework like WordPress or one of the CMS's like Joomla for example, this can and does cause problems when you move your site to or from a live server.

How to create a Virtual Host in WampServer

--- With Wampserver 3.0.0 it is simplier and can be done automaticaliy with few click:
--- see []
--- But this does not prevents you reading the following explanations; it will help you understand what a VirtualHost is.

Actually this should work basically the same for any windows Apache server, with differences only in where you may find the Apache config files.

There are 2 steps to create your first Virtual Host in Apache:

1. Create the Virtual Host definition(s)
2. Add your new domain name to the HOSTS file.

Step 1, Create the Virtual Host definition(s)

Edit the file called `httpd-vhosts.conf` which for WampServer lives in
(Apache version numbers may differ, engage brain before continuing)

-- If this is the first time you edit this file, remove the default example code, it is of no use.

I am assuming we want to create a definition for a site called project1 that lives in


Very important, first we must make sure that localhost still works so that is the first VHOST definition we will put in this file.
In principle, since Wampserver 3.0.4 and Apache 2.4.18 this definition already exists
 <VirtualHost *:80>
  DocumentRoot "c:/wamp/www"
  ServerName localhost
  ServerAlias localhost
  <Directory  "c:/wamp/www">
    AllowOverride All
 		Require local
Now we define our project: and this of course you do for each of your projects as you start a new one.
<VirtualHost *:80>
  DocumentRoot "c:/wamp/www/project1"
  ServerName project1
  <Directory  "c:/wamp/www/project1">
    AllowOverride All
 		Require local

Note: each VHOST definition has its own DocumentRoot definition.

Small aside
The way virtual hosts work in Apache: The first definition in this file will also be the default site, so should the domain name used in the browser not match any actually defined virtually hosted domain, making localhost the first domain in the file will therefore make it the site that is loaded if a hack attempt just uses your IP Address.
So if we ensure that the Apache security for this domain **is ALWAYS SET TO**
 Require local
any casual hack from an external address will receive an error and not get into your PC, but should you misspell a domain you will be shown the WampServer homepage, because you are on the same PC as WampServer and therfore `local`.

Setp 2:

Add your new domain name to the HOSTS file.
Now we need to add the domain name that we have used in the Virtual Host definition to the HOSTS file so that windows knows where to find it. This is similiar to creating a DNS A record, but it is only visible in this case on this specific PC.

Edit `C:\windows\system32\drivers\etc\hosts`

The file has no extension and should remain that way. Watch out for notepad, as it may try and add a `.txt` extension if you have no better editor.
I suggest you download Notepad++, its free and a very good editor.

Also this is a protected file so you must edit it with administrator privileges, so launch you editor using the Run as Administrator menu option

The hosts file should look like this when you have completed these edits localhost project1

 ::1 localhost
 ::1 project1
Note that you should have definitions in here for the IPV4 loopback address `` and also the IPV6 loopback address `::1` as Apache is now IPV6 aware and the browser will use either IPV4 or IPV6 or both. I have no idea how it decides which to use, but it can use either if you have the IPV6 stack turned on, and most window OS's do as of XP SP3.

Now we must tell windows to refresh its domain name cache, so launch a command window again using the Run as Administrator menu option again, and do the following.
ipconfig /flushdns
This forces windows to clear its domain name cache and reload it, in reloading it will re-read the HOSTS file so now it knows about the domain `project1`.

To activate this change in you running Apache we must now stop and restart the Apache service.
Wampmamaner Tray Icon -> Apache -> Service -> Restart Service

Now if the WAMP icon in the system tray does not go GREEN again, it means you have probably done something wrong in the `\wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhosts.conf` file.

If so here is a useful mechanism to find out what is wrong. It uses a feature of the Apache exe (httpd.exe) to check its config files and report errors by filename and line numbers.

Launch a command window.
cd \wamp\bin\apache\apache2.4.41\bin
httpd -t

So fix the errors and retest again until you get the output
 Syntax OK

Now there is one more thing.
Wampmanager Tray Icon Your VirtualHost menu item
The 'Your Virtual Hosts' menu item searches the file that is used to define Virtual Hosts that we have just changed and creates menu links for each ServerName parameter it finds and creates a menu item for each one.

Now if you take this to its logical extension
You can now move your web site code completely outside the \wamp\ folder structure simply by changing the DocumentRoot parameter in the VHOST definition. So for example you could do this:

Create a folder on the wamp disk or any other disk (beware of network drive, they are a bit more complicated)

MD websites
CD websites
MD www

You now copy your site code to, or start creating it in the \websites\\www folder and define your VHOST like this:

<VirtualHost *:80>
  DocumentRoot "d:/websites/"
  ServerName example.local
  ServerAlias www.example.local
  <Directory  "d:/websites/">
    AllowOverride All
    Require all granted
  php_flag display_errors Off
  php_flag log_errors On
  php_value max_upload_size 40M
  php_value max_execution_time 120
  php_value error_log "d:/wamp/logs/example_com_phperror.log"

Then add this new development domain to the HOSTS file: localhost
::1 localhost project1
::1 project1 example.local
::1 example.local

NOTE: It is not a good idea to use a ServerName or ServerAlias that is the same as your live domain name, as if we had used as the ServerName it would mean we could no longer get to the real live site from this PC as it would direct to i.e. this PC and not out onto the internet.

See that I have allowed this site to be accessed from the internet from within the VHOST definitions, this change will apply to only this site and no other. Very useful for allowing a client to view your changes for an hour or so without having to copy them to the live server.
This does mean that we have to edit this file manually to turn this access on and off.

Also I have added some modifications to the PHP config, again that will only apply to this one site.
Very useful when maintaining a site with specific requirement unlike all the other sites you maintain.
I guess we can assume from the parameters used that it has a long running page in it somewhere
php_value max_execution_time 120
and it is very badly written and will not run with errors being displayed on the browser without making a horrible mess of the page.
php_flag display_errors Off
Believe me sites like this exist and people still want them maintained badly.

Another bonus to using Virtual Hosts
Because we are using Virtual Hosts, we can tailor the Apache AND PHP parameters from within the VHOST definition therefore applying these change to only one of the possibly many sites we maintain.
We can therefore leave the httpd.conf and php.ini files which of course are GLOBAL to all the sites, containing general base settings and not have to fiddle with them for each different site we may work on.
We also have a record for each site that needed parameters tailoring and what that tailoring was.

You can not reply to this message.
Open a new thread to describe your problem be sure to give the answers to: READ THIS BEFORE YOU ASK A QUESTION

Edited 8 time(s). Last edit at 12/09/2019 06:37PM by Otomatic.

Options: ReplyQuote

Sorry, you can't reply to this topic. It has been closed.