How to create a Virtual Host in WampServer

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 3 steps to create your first Virtual Host in Apache, and only 2 if you already have one defined.

1. Create the Virtual Host definition(s)
2. Add your new domain name to the HOSTS file.
3. Uncomment the line in httpd.conf that includes the Virtual Hosts definition file.

Step 1, Create the Virtual Host definition(s)

Edit the file called `httpd-vhosts.conf` which for WampServer lives in

 \wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhosts.conf

(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

\wamp\www\project1

Very important, first we must make sure that localhost still works so that is the first VHOST definition we will put in this file.

 <VirtualHost *:80>
     DocumentRoot "c:/wamp/www"
     ServerName localhost
     ServerAlias localhost
     <Directory  "c:/wamp/www">
        AllowOverride All
 		Require local
     </Directory>
 </VirtualHost>

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
     </Directory>
 </VirtualHost>


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 Administratormenu option


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

 127.0.0.1 localhost
 127.0.0.1 project1

 ::1 localhost
 ::1 project1

Note that you should have definitions in here for the IPV4 loopback address `127.0.0.1` 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 Administratormenu option again, and do the following.

 net stop dnscache
 net start dnscache

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`.

Step 3: Uncomment the line in httpd.conf that includes the Virtual Hosts definition file.

Edit your httpd.conf, use the wampmanager.exe menus to make sure you edit the correct file.

Find this line in httpd.conf

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

And just remove the `#` to uncomment the Include line.

To activate this change in you running Apache we must now stop and restart the Apache service.

 

- PHP, Apache, MySQL, Windows : WampServer.

max

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>