May 16

How to set up VirtualHost in XAMPP for Windows

My environment is Windows Vista but these instructions work for Windows XP as well.

0. Introduction

Sometimes, we have multiple projects and we would like to access the Project 1’s website by typing ‘http://project1/’ in the browser address bar. And Project 2 may be at ‘http://project2/’. This is how we can configure Windows and Apache to do just that. While this guide is written for XAMPP’s Apache, if the instruction applies pretty much to Apache too. You can put your configuration into ‘httpd.conf’.

1. Add a new host in Windows

First go to your hosts file, it is located at C:\Windows\System32\drivers\etc\hosts for most people. If you’re using Windows Vista, you need to run command prompt as administrator. Type ‘cmd’ into the search bar and right click on the ‘cmd’ search result to point to ‘Run as administrator’.

Running command prompt as administrator

Type in the following command into your command prompt:

notepad C:\Windows\System32\drivers\etc\hosts

You will see notepad open and your hosts file looking something like this:

127.0.0.1       localhost
::1             localhost

Add an additional line so that it looks this way:

127.0.0.1       localhost
127.0.0.1       project1
::1             localhost

Order doesn’t matter, I just want it to look nicer. ‘::1′ is for IPv6’s localhost by the way.

2. Add VirtualHost into Apache’s configuration

Now edit our httpd-vhosts.conf. It should be located in C:\xampp\apache\conf\extra\httpd-vhosts.conf.

Uncomment the NameVirtualHost line and add new virtual hosts. I add ‘project1′ and ‘localhost’:

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot "C:/projects/project1"
    ServerName project1
</VirtualHost>

<Directory "C:/projects/project1">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

<VirtualHost *:80>
    DocumentRoot "C:/webroot"
    ServerName localhost
</VirtualHost>

<Directory "C:/webroot">
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

We’ll have to add localhost in our httpd-vhosts.conf file so that the localhost to continue working. You can add as many virtual hosts as you want in any order. You can even add hosts like ‘project2.localhost’.

3. That’s all

Oh yeah, and remember to restart your Apache server so that your new settings can be reflected. Hope it helps.

No comments yet. Leave yours here.


WordPress powered and Django inspired.
Love and elephants come after.
RSS: Posts and comments.