27th July 2008

Multiple websites on Xampp

Who is this article for?

This article is for anyone who is using XAMPP to develop websites who isn’t using vhosts to host multiple websites on their machine. Without using vhosts you can only have one domain on XAMPP, if you want multiple websites you have to have them in directories within htdocs ie- http://localhost/website-one/ and http://localhost/website-two/ . This tutorial teaches you to set up vhosts and to trick your computer into thinking that http://www.website-one.com and http://www.website-two live on your computer.

Vhosts

Vhosts or ‘Virtual Hosts’ is a module (addon) to apache that allows you to create a list of websites in a file (the vhost file) that you want hosted on your machine. On my machine the vhost file lives at C:xamppapacheconfextrahttpd-vhosts.conf. We are about to start editing the vhost file so it is sencible to create a back up copy of it that works. Open your vhost file with a text editor, The first thing to do it turn on name-based virtual hosting, you do this by un-commenting (removing the #’s from) the “NameVirtualHost *:80” line. With name-based virtual hosting turned on, apache looks at what is being requested (ie what website name has been asked for) and looks to see if it has a vhost that matches it. Before we add website-one.com and website-two lets set up a vhost for what you would normally see when you go to http://localhost/- the normal localhost XAMPP page. Add the following code to your vhost file…

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot "C:\xampp\htdocs"
ServerName localhost
</VirtualHost>

(be sure to change the paths if your set-up is different to mine) Let also create a vhost for phpmyadmin so that we can go to http://phpmyadmin/ instead of http://localhost/phpmyadmin. Add the following code to your vhost file.

<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot "C:\xampp\phpmyadmin"
ServerName phpmyadmin
</VirtualHost>

Now lets add vhosts for our two websites…

<VirtualHost *:80>
ServerName website-one.com
ServerAdmin webmaster@localhost
DocumentRoot "C:\xampp\htdocs\website-one.com"
</VirtualHost>
<VirtualHost *:80>
ServerName website-two.com
ServerAdmin webmaster@localhost
DocumentRoot "C:\xampp\htdocs\website-two.com"
</VirtualHost>

Now you need to create directories that hold the website-one and website-two’s html files. These directories need to be placed where you have said the Document Root is in you vhost. Now you need to restart apache for the changes to take affect. A common problem at this point is that apache wont start up again. This may be because apache can’t understand your vhost file or that the directories that you have declared as document roots don’t exist. Check your syntax and your paths. If that doesn’t work still, restore your back up vhost and try again. If apache has managed to start up again then you are laughing, but we are not finished yet.

Hosts file (tricking your browser)

Now if you were to type website-one.com into your browser it will ask the internet what IP address website-one lives at, the internet with give your browser an IP address and you send a request for website-one.com to the server that is sitting at the end of that IP. You probably don’t want to go the effort of buying a domain and pointing that domain to your website via DNS. Luckily you can overwrite what the internet says about where website-one.com lives and get requests from your computer to website-one.com to go to 127.0.0.1 (your IP). Most operating systems have a file where you can overwrite what the internet tells you about IP addresses, it’s called your hosts file (not to get conffused with a vhost file!). In windows XP it lives at C:WINDOWSsystem32driversetchost. Open it with a text editor, add the following lines for you normal xampp page and phpmyadmin…

127.0.0.1 localhost
127.0.0.1 phpmyadmin

and then create your websites…

127.0.0.1 website-one.com
127.0.0.1 website-two.com

Save the file and then your done. Restart Apache (is you haven’t already) and try going to your website (type website-one.com into your browser). If it is not working your browser may still be doing what the internet told it to do, your browser is remembering what the internet told it. To fix this you need to clear your cache, restart your browser and you should be fine. (on a mac you may need to open terminal sudo killall lookupd && lookupd -flushcache , I think Mac like to read the hosts file when it starts up and you can only tell it to read it again by doing this). Any other troubleshooting, check the comments.

Responses

FahadOnline
19th August 2008

That was a very helpful article, Thank you.

NY Web Design
27th August 2008

Great Tutorial!! This is exactly what I was looking for to text my domains without having to constantly upload. Thank you!!

Port80Designs
26th October 2008

Thanks for the article. Exactly what I needed.

Any reason why I would not be able to use the directory of a networked drive as the DocumentRoot. When I try, I get the error from Apache that “DocumentRoot must be a directory”. The folder I am pointing to is a folder. Any thoughts would be appreciated.

Again, thanks for the article!

Russell
18th November 2008

Thanks for this it was very helpful. I prefer to have my urls on my local machine set up in the format localhost.mydoman.com as this means that conflict with real / live websites is less likely. That format also means that the url looks very similar to the live one I am developing for.

Ron
9th October 2009

perfect simple to the point good job!

Stefan Brannfjell
20th October 2009

thank you, very helpful! :D

google.com/accounts/o8…
8th April 2010

This increases my productivity tremendously .. another one of those things I should have known about but am happy to learn.

Thanks a lot.

Ken
6th May 2010

Thanks for the post. (BTW, you seem to be missing some “/”’s in your post’s path names.)

Brian Temecula
17th May 2010

This is great. I’m a long time wampserver user that has recently changed to xampp for the auto-installed SSL. Having multiple websites on xampp is harder to do than wampserver, but this tutorial is the key to success!

Michael Campbell
17th May 2010

Thanks, just fixed it

Comments RSS
Trackback URL

Leave a comment

Your details...

Or... if you have an openID...

An OpenId is a Web address that you can use to log into different web sites without having to use yet another username and password. More information. If you have a blog with blogger or some other website You might already have an OpenID.

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