Updated for XAMPP version 1.6.5
Some time ago, I decided to start phasing out static xhtml in favor of pages using PHP includes. To test these new pages, I used apachefriends.org’s wonderful XAMPP (which I really can’t recommend highly enough) to install Apache, MySQL, and PHP (among other things). Once I had my local server running, I put each dev site into its own folder in \htdocs\ and navigated to them by http://127.0.0.1/foldername/.
This setup was functional but far from ideal, as the index pages for these local sites weren’t in what could be considered a root directory, which lead to some tip-toeing around when creating links.
Then I discovered the NameVirtualHost feature in Apache. NameVirtualHost allows the server admin to set up multiple domains/hostnames on a single Apache installation by using VirtualHost containers. In other words, you can run more than one web site on a single machine. This means that each dev site (or domain) can then consider itself to have a root directory. You will be able to access each local site as a subdomain of “localhost” by making a change to the HOSTS file. For example, I access the local dev version of this site at http://ardamis.localhost/.
This works great for all sorts of applications that rely on the site having a discernible root directory, such as WordPress.
Unfortunately, setting up NameVirtualHost can be kind of tricky. If you are having problems configuring your Apache installation to use the NameVirtualHost feature, you’re in good company. Here’s how I managed to get it working:
For XAMPP version 1.6.5
- Create a folder in drive:\xampp\htdocs\ for each dev site (adjust for your directory structure). For example, if I’m creating a development site for ardamis.com on my d: drive, I’d create a folder at:
d:\xampp\htdocs\ardamis\ - Edit your HOSTS file (in Windows XP, the HOSTS file is located in C:\WINDOWS\system32\drivers\etc\) to add the following line, where sitename is the name of the folder you created in step 1. Don’t change or delete the existing “127.0.0.1 localhost” line.
127.0.0.1 sitename.localhost
Add a new line for each dev site folder you create.
Continuing with the example, I’ve added the line:
127.0.0.1 ardamis.localhost
- Open your drive:\xampp\apache\conf\extra\httpd-vhosts.conf file and add the following lines to the end of the file, using the appropriate letter in place of drive. Do this step only once. We’ll add code for each dev site’s folder in the next step. (Yes, keep the asterisk.)
NameVirtualHost *:80 <VirtualHost *:80> DocumentRoot "drive:/xampp/htdocs" ServerName localhost </VirtualHost>
My DocumentRoot line would be:
DocumentRoot "d:/xampp/htdocs"
- Immediately after that, add the following lines, changing sitename to the name of the new dev site’s folder, again using the appropriate letter in place of drive. Repeat this step for every folder you’ve created.
<VirtualHost *:80> DocumentRoot "drive:/xampp/htdocs/sitename" ServerName sitename.localhost </VirtualHost>
My DocumentRoot line would be:
DocumentRoot "d:/xampp/htdocs/ardamis"
My ServerName line would be:
ServerName ardamis.localhost
- Reboot your computer to be sure it’s using the new HOSTS file (you’ll have to at least restart Apache). You should now be able to access each dev domain by way of:
http://sitename.localhost/
For XAMPP version 1.4
If you are using an older version of XAMPP (like XAMPP version 1.4) without the httpd-vhosts.conf file, use the instructions below.
- Create a folder in your drive:\apachefriends\xampp\htdocs\ for each local version of your site. For example, if I’m creating a development site for ardamis.com on my f: drive, I’d create a folder at:
f:\apachefriends\xampp\htdocs\ardamis\ - Open your HOSTS file (in Windows XP, the HOSTS file is located in C:\WINDOWS\system32\drivers\etc\) and add the following line, where sitename is the name of the folder you created in step 1. Repeat this step, as necessary, for each folder you create. Don’t change or delete the existing “127.0.0.1 localhost” line.
127.0.0.1 sitename.localhost
Continuing with the example, I’ve added the line:
127.0.0.1 ardamis.localhost
- Open your drive:\apachefriends\xampp\apache\conf\httpd.conf file and add the following lines to the end of the file, using the appropriate letter for drive. Do this step only once. We’ll add code for each dev site’s folder in the next step. (Yes, keep the asterisk.)
NameVirtualHost *:80 <VirtualHost *:80> DocumentRoot "drive:/apachefriends/xampp/htdocs" ServerName localhost </VirtualHost>
My DocumentRoot line would be:
DocumentRoot "f:/apachefriends/xampp/htdocs"
- Immediately after that, add the following lines, changing sitename to the name of the new folder, using the appropriate letter for drive and repeating this step for every folder you’ve created.
<VirtualHost *:80> DocumentRoot "drive:/apachefriends/xampp/htdocs/sitename" ServerName sitename.localhost </VirtualHost>
My DocumentRoot line would be:
DocumentRoot "f:/apachefriends/xampp/htdocs/ardamis"
- Reboot and restart Apache. Open a browser; you should now be able to access each folder by way of:
http://sitename.localhost
I’m assuming that you could change the DocumentRoot line to point to any folder on any drive. I’ll experiment with pointing this at a folder on another drive later.
The official Apache.org documentation for VirtualHost is at http://httpd.apache.org/docs/2.2/vhosts/. You may want to read that for further details before you try to set up virtual hosts.
If you have any questions about the above instructions, the Apache NameVirtualHost function, XAMPP, or anything in between, post a comment, but I can’t promise that I’ll be able to help. I’m learning as I go along, too.
dude, thank you so much. i’ve been so frustrated the last 2 days w/ NameVirtualHost. btw, this page was the 2nd google result for . nice!
This is an old post, but thanks for the XAMPP info. I have have had virtual hosts using Apache2Triad (similar to XAMPP) for years, and I never had to specify the port #. So when I installed XAMPP, I wasn’t specifying the port # in my VirtualHost tags. I was trying to use
instead of the more specific
but I was getting “denied” errors from Apache. But adding the port number (:80) right after the asterisk (*) cleared it all up and virtual hosts are now running fine under XAMPP.This info also applies to the Mac OS X version of XAMPP (still in beta).
You can also define all your Virtual Hosts in a separate file that gets included into your main Apache httpd.conf file. First create a file in the same directory as your httpd.conf file named httpd-vhosts.conf and move all the tags from httpd.conf into that new file. Then in your main httpd.conf file, you can add just a line like this to “include” the external virtual host definitions:
Include
"drive:/apachefriends/xampp/etc/httpd-vhosts.conf"
Depending on what version of XAMPP you have installed, there may already be an Include line commented out near the bottom of the httpd.conf that would include a httpd-vhosts.conf that is in the
XAMP_ROOT/etc/extra/
folder.I hope the formatting in this comment isn’t too ugly. Cheers! 😀
Thanks man. This has saved my bacon. Specifically the bit about the DocumentPath needing the full path. As the apache documentation given within xampp http://httpd.apache.org/docs/2.2/en/vhosts/name-based.html doesnt mention it, but just suggests /www/domain which was throwing a access denied problem.
thanks for the article, its really useful. dose it work with v1.5.5 a
Yes, one of the two methods works with version 1.5.5. I jumped from 1.4 to 1.6, so I’m not sure which method is used in 1.5.5.
Just look for the httpd-vhosts.conf file described in the 1.6.5 instructions. If it doesn’t exist, use the instructions for 1.4 instead.
Thanks so much for this how-to…
Great Tutorial!
In this tutorial all the site are under: drive:/xampp/htdocs
Is it possible to have additional sites located elsewhere, like on an external drive? Reason being, I work off this drive at work and at home.
Oh man! Thanks for that…I’ve been messing with url rewriting and couldn’t quite get it to behave the same on my local server as my production…it was all because of the root directory issue and this solves it. Thanks a lot.
By the way, you’re readers may want to note that I didn’t appear to have a hosts file…I had a hosts1.bak file, which I then edited and renamed as hosts without the extension. Oddly enough it then asked if I wanted to rewrite the hosts file (without the extension) even though hidden files were being shown and there didn’t appear to be any hosts file in that directory at all.
So, then I tried to save it, but it wouldn’t let me…then I realized it was read only, so, when I changed that, all was well. I didn’t have to reboot windows…only apache.
Thanks again for the clear tutorial!
By the way I’m on XAMPP Version 1.6.6a windows xp sp3
Thankyou sooooo much updated to latest xampp and could not get vhost working right. It’s easy when you know how.
chefchops
Thank for this tutorial…… Now I can install and try the vbulletin board forum…
Due to my misery with Dreamweaver, I decided to switch to a local development server. Thanks for blazing the trail on the Virtual Hosts and sharing your experience.
Thanks!
Thanks!!
Hello,
Thanks for this tutorial. I’m new to webservers. Yesterday evening I was trying to do this. I have on my D drive all my applications and on E drive all my development stuff. So in this situation I seperate
If you are working with virtual hosts (httpd-vhosts.conf) then you don’t have to change the documentroot in httpd.conf?
Now I have changed the documentroot in httpd.conf, but then I get php errors of session-request can’t find the line D:XAMPPHTDOCS…. ‘some text’ E:htdocsapplicationname. I have few applications with only html and they are working without any problem.
Another thing my let say standard application as phpmyAdmin are not working anymore, because they are still in the original htdocs dir of XAMPP. But maybe if I’m working with virtual hosts then I can leave the standard applications in the original dir of XAMPP.
I’m gonna try it out tonight.
Nico
You should only need to change the httpd-vhosts.conf file. I haven’t split my apache installation and my dev sites between two hard drives yet, but I wouldn’t mess with the httpd.conf file or any of the other files/folders installed by XAMPP. Just create the new site folders and then use the DocumentRoot line in each VirtualHost entry in httpd-vhosts.conf to describe the path to that site’s directory.
Maybe I’m doing something wrong but I get error HTTP 403 Forbidden. I have switch off the firewall but that doesn’t help. Has it something todo with a different drive?
I’v brought back httpd.conf in the original state. phpmyAdmin, Xampp etc is still working fine with localhost/phpmyadmin for example.
Xampp is installed op de D drive.
httpd-vhosts.conf file.
NameVirtualHost *:80
DocumentRoot D:/xampp/htdocs
ServerName localhost
DocumentRoot e://htdocs/dijkman-wsv
ServerName dijkman-wsv.dev
DocumentRoot e:/htdocs/sedapcatering
ServerName sedapcatering.dev
hosts file
127.0.0.1 localhost
127.0.0.1 dijkman-wsv.dev
127.0.0.1 sedapcatering.dev
I’m typing as url: http://dijkman-wsv.dev or http://sedapcatering.dev.
Does somebody know why I do get error HTTP 403 Forbidden? What do I wrong?
Thanks,
Nico
This the error in my error.log file
[Mon Jun 08 22:57:18 2009] [error] [client 127.0.0.1] client denied by server configuration: E:/htdocs/dijkman-wsv/
This directory exists and in the dir dijkman-wsv does a index.html exist.
I have found a solution about the HTTP 403 error. I have changed the following in httpd.conf:
Options FollowSymLinks
AllowOverride None
Order deny,allow
#Deny from all #This is the original line
Allow from all #Changed to this line
Satisfy all
“Normal” (just HTML) sites are working now but php sites I get HTTP 404 error (Logs):
Warning: session_start() [function.session-start]: open(xampptmpsess_e3a9c80099fad3b0be6d24c38f19087f, O_RDWR) failed: No such file or directory (2) in E:htdocssedapcateringinclude.php on line 34
Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at E:htdocssedapcateringinclude.php:34) in E:htdocssedapcateringinclude.php on line 34
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at E:htdocssedapcateringinclude.php:34) in E:htdocssedapcateringinclude.php on line 34
string(113) "Smarty error: [in tpl_top:20 line 1]: [plugin] unknown tag - 'process_pagedata' (core.load_plugins.php, line 124)"
Fatal error: Call to undefined function smarty_cms_function_process_pagedata() in E:htdocssedapcateringtmptemplates_c%%BC^BCE^BCE9A9A7%%tpl_top%3A20.php on line 5
[Tue Jun 09 22:49:06 2009] [error] [client 127.0.0.1] File does not exist: E:/htdocs/sedapcatering/function.session-start, referer: http://sedapcatering.dev/
Although that this is different error I hope that somebody can help what is wrong
Does somebody what the reason of this error is?
Oke I’m further. First my virtual host:
DocumentRoot "K:/xampp/xampp/htdocs"
ServerName localhost
DocumentRoot "I:/www/cms/"
ServerName cms.tld
ServerAlias cms.tld *.cms.tld
AllowOverride All
Allow from All
DocumentRoot "I:/www/test/"
ServerName test.tld
ServerAlias test.tld *.test.tld
AllowOverride All
Allow from All
Then about the session. In php.ini I have changed the session variable:
session.save_path = "d:xampptmp"
So I have now one error HTTP 401 Access denied for a own made php site. When I have this in the D:xampphtdocs, then it is working fine. I hope I wil find it soon.
Nico
Great tutorial! It was a breeze and I’m really newbi to PHP and Apache. Thanks a lot, now I’m able to do all testing on my computer before going to production.
Chreers!
Thanks a ton.. this helped a lot!
Hi
I am trying to configure a url for my website on my localhost machine. I am running xampp from a memory stick. I cannot find the hosts file to configure the url. I am running the latest version of xampp
Hi All,
I just did the second step and i added the following code to the httpd-vhosts file,this is the code:
DocumentRoot C:xamppphpwwwPCExercises
ServerName PCExercises.localhost
DirectoryIndex index.php
Bigg Salutation from Morocco 😉
Thank you so much. I’ve spended so much time searching for something like this.
Thank you. You maked my day!
WOW i have been having issues for years and still have never had virtual hosts wow is all i can say and many thanks
Dude, you don´t know how great this post was, especially in its simplicity! I´ve gone through a dozen similiar guides today, and not one of them worked, until now! And a lot of the others went into far more detail which still ended up being unclear or useless!
thanks again, simplicity and straightforwardness win the day.
Thank you SO MUCH!
I have been trying for more than a day to do just this,
Followed many other “instructions” to do this = but they all ended up crashing my server or just giving some error messages – endless headache!
Your instuctions were so easy and clear to follow
WELL DONE !
🙂
Hi,
Seems like i lost my previous message:
I am struggling with the virtual server – now that I have moved on to more web sites.
my host file is as follows:
127.0.0.1 localhost
127.0.0.1 web1.dev
127.0.0.1 web2.dev
127.0.0.1 web3.dev
etc
and my httpd-vhosts file is as follows:
NameVirtualHost *:80
DocumentRoot “d:/xampp/htdocs”
ServerName localhost
DocumentRoot “d:/xampp/htdocs/web1”
ServerName web1.dev
DocumentRoot “d:/xampp/htdocs/web2”
ServerName web2.dev
DocumentRoot “d:/xampp/htdocs/web3”
ServerName web3.dev
etc
Now my problem is… is localhost, web1.dev & web2.dev points to web3.dev and i can’t see web1.dev or web2.dev etc.
Another problem, is that when I add more hosts settings to vhosts files -my apache server does not want to restart…
any suggestions please?
thank you!
Marianna,
I believe that your problem is that you need to wrap each new site in its own VirtualHost tag.
In the example below, I’m running three web sites located in different subfolders of E:/xampp/.
The above code would let me access the three local sites at
ardamis.localhost
minifide.localhost
plogger.localhost
Let me know if you still need help.
Ah!
Working like a dream now ! 🙂
Thank you Ardamis.
Hi!
I could not save the host file because this file doesn’t have any extensions. What should I do?
Thanks, I was having an issue with this and your post pointed the way (needed to add the ‘ServerName localhost’ to my almost working configuration).
I am running xampp on Windows 7, and have found that you can leave out the drive letter in the path names, which means it’s still a portable configuration : i.e.
DocumentRoot “/xampp/htdocs/drupal”
ServerName drupal.localhost
Works for me – ymmv!
Hosts file content :127.0.0.1 sumitlocal
httpd-vhosts.conf Content
DocumentRoot “c:/xampp/htdocs/sumitlocal”
ServerName sumitlocal.localhost
After changes its showing me xampp folder content (http://localhost/ )
Please tell me what should i do
its show lthe path //http://sumitlocal/xampp
its not working
Hosts file content :127.0.0.1 sumitlocal
httpd-vhosts.conf Content
DocumentRoot “c:/xampp/htdocs/sumitlocal”
ServerName sumitlocal.localhost
After changes its showing me xampp folder content (http://localhost/ )
Please tell me what should i do
its show lthe path //http://sumitlocal/xampp
After restart of my system and appache server its not working
still the same problem i am not abel to run my virtual site
i am using window 7 can i send u my both setting files
It looks like you are missing part of the HOSTS file line.
The line in HOSTS should be:
It also looks like you are missing some lines in xamppapacheconfextrahttpd-vhosts.conf.
The lines should be:
Good luck.
Thanks DUDE
Thanks for this, I was on another site which had an example that didn’t work and was a lot more complicated.
This was great — clear, concise and it works perfectly. I’m using XAMPP 1.7.7 on Windows 7 – no Windows reboot necessary. Just update the hosts file & apache CONF file and you’re good to go.
Thank you so much for your post. It was a bit of a nightmare finding the right tool (XAMPP) to use on my Windows machine. Then more frustration trying to configure it to use as a web server. I tried a few other instructions and nothing worked until I read yours. I’ll now be able to take over the maintenance of a couple of website applications for a client whose sites I could not “see” and test with IIS (even though it worked for PHP sites) because they use .htaccess. I am sooooo grateful!
Hy! If i connect to my local website through the address http://ip_address:8080/site_name/ ,. how could i make the changes in my hosts to access my website through http://site_name/ ? The port number modifcation where should be done?
Thanks man! I’ve read so many differing ways to accomplish this and nothing was working for me. This was so simple and worked first try!
Thanks for taking the time to explain this. This had been driving me crazy. Thanks to this page, I got it sorted in minutes. XAMPP v.3.1