Setting up a Virtual Host with Apache

August 28, 2008

This is a follow up to my ColdFusion 8 and Apache on Windows XP post.

Apache allows you to set up virtual hosts which is dead handy if you work on lots of sites and don't want to have to use subfolders off you main webserver root. For example you can have http://localhost.myproject/ instead of http://localhost/myproject/. This is especially useful if you want to extend CFCs (as the paths are from the website root).

To set this up on Windows is quite easy (the paths assume that you have installed Apache with XAMPP, but applies to any Apache install).

1) Create a new folder for your new website inside "C:\xampp\htdocs\". I'm going to call mine "aliaspooryorik". Add a file called "index.cfm" and just type in the obligotory "Hello World!" and save it.
2) Open "C:\xampp\apache\conf\extra\httpd-vhosts.conf" in a text editor (i.e. notepad)
3) Find the line "## NameVirtualHost *:80" and remove the # signs (these comment out the line). So you now have "NameVirtualHost *:80". What this does is to tell Apache to use the host header that you set up and not the IP address. The *:80 means listen to everything on port 80. Leave this as 80 unless you use a different port.
4) Add the following the bottom of the "httpd-vhosts.conf" file.

# localhost.aliaspooryorik.com
<VirtualHost *:80>
  ServerAdmin webmaster@localhost.aliaspooryorik.com
  ServerName localhost.aliaspooryorik.com
  DocumentRoot "C:\xampp\htdocs\aliaspooryorik"
  DirectoryIndex index.cfm
  ErrorLog logs/localhost.aliaspooryorik.com-error_log
  CustomLog logs/localhost.aliaspooryorik.com-access_log common
</VirtualHost>

This is telling Apache that for the web address "localhost.aliaspooryorik.com" the scripts are in the "C:\xampp\htdocs\aliaspooryorik" directory and your default document is index.cfm. Save the "httpd-vhosts.conf" file.

5) Now you need to tell windows that it shouldn't look on the internet for the web address "localhost.aliaspooryorik.com", but at your local Apache server. To do this open up "C:\WINDOWS\system32\drivers\etc\hosts" in a text editor. Add the following lines to the bottom of the file:

# localhost.aliaspooryorik.com
127.0.0.1  localhost.aliaspooryorik.com

Save the "hosts" file.

6) Fire up the XAMPP control panel (you can find it at "C:\xampp\xampp-control.exe" if you don't have a shortcut) and Stop and then Start Apache so that it picks up the new settings.

7) Open up a web browser and type in "http://localhost.aliaspooryorik.com/". You should see your "Hello World!" example.

Trouble Shooting.

If you browser goes off to the internet to find the web address you typed in then open up a command prompt (type "cmd" into your run dialog from the start menu) and type into the command window "ipconfig /flushdns" and hit the return key.

If you want to be able to access the CFIDE directory for a local website (if you use cfform validation or the ColdFusion AJAX functionality for example) then you need to add this to your VirtualHost definition. Here is the previous example updated.

# localhost.aliaspooryorik.com
<VirtualHost *:80>
  ServerAdmin webmaster@localhost.aliaspooryorik.com
  ServerName localhost.aliaspooryorik.com
  DocumentRoot "C:\xampp\htdocs\aliaspooryorik"
  DirectoryIndex index.cfm
  ErrorLog logs/localhost.aliaspooryorik.com-error_log
  CustomLog logs/localhost.aliaspooryorik.com-access_log common
  Alias /CFIDE "C:/xampp/htdocs/CFIDE"
  <Directory "C:/xampp/htdocs/CFIDE">
    AllowOverride All
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

 


3 comments

  1. Thanks for both articles, made life so much easier.

    Now, i would like to access multiple sites from another computer on the network. I can do that on the same computer with localhost. But i would like to access different sites as say 192.168.10.101.testsite2 & 192.168.10.101.testsite3 for example. What actually happens is I get sent out to the web to my actual customers site.

    Do I have to setup multiple ports to differentiate each site? ie: 127.0.0.1:8080 and 127.0.0.1:8081 etc. And then would I need to add the Listen directive and NameVirtualHost tag also? (Not totally sure what I'm talking about)

    If you have any ideas that would be great? Thanks in advance.

    Comment by Robert – September 22, 2008
  2. Cool I got it working for multiple sites and access from multiple computers.
    example - add into httpd-vhosts file
    add Listen 8080
    add NameVirtualHost *:8080

    and add 127.0.0.1:8080 in Win32 file

    only thing is I have to refer to each site with 192.168.10.101:8080 instead of site name

    Comment by Robert – September 23, 2008
  3. @Robert, you either need to use IP address or a host header. If you want to use the host header then you will need to add it to the hosts file of each computer you want to browse from, otherwise it will connect to the internet. Hope that helps :)

    Comment by John Whish – September 23, 2008

Leave a comment

If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)

Please note: If you haven't commented before, then your comments will be moderated before they are displayed.