Apache virtual hosts on local machine

Many of us run a local version of the WAMP stack either on a laptop or development desktop, I run a version of Moodle on mine so I can test (and break) things to my hearts content without disrupting the main production server.

Recently a collegue of mine required some assistance with setting up Apache Virtual Hosts on a production server.  It had been a while since I had done this so I decided to practise on my laptop as not to break the server.  It was then I realised I would have to set up domain names on my laptop so I could check the virtual hosts actually work from sub domains.  This got me thinking, "Why would anyone need other domains on a localhost pc?"  After all,  I simply type http://localhost/moodle or http://localhost/moodle2 etc..  to test my dummy sites.  But then I liked the idea of typing http://moodle or http://wordpress to access my  test sites!  So hence this tutorial was born.

Step 1

Edit the following file

C:\WINDOWS\system32\drivers\etc\hosts

And include the extra hosts you require:

127.0.0.1       localhost
127.0.0.1       moodle
127.0.0.1       wordpress
    

Step 2

Now edit your apache config file and uncomment the line

Include conf/extra/httpd-vhosts.conf

Step 3

Now create a new folder anywhere on your system, this is where your new virtual host web pages will reside

Edit the file

confextrahttpd-vhosts.conf

Paste the following below and edit to match the paths to your created folders and substitute server name with the names of your hosts you created.

NameVirtualHost *:80
VirtualHost *:80
ServerName localhost
DocumentRoot "C\:wamp\www\"

NameVirtualHost *:80
VirtualHost *:80
ServerName wordpress
DocumentRoot "C:\vhosts\wordpress"

Now restart apache and test your host names by typing them in your browser.  In the example above if I type http://wordpress then my browser points to a new folder on my C drive called vhosts.  If I type http://localhost it defaults to my WAMP installation.Have fun!