On June 11th, 2008 milburn wrote…

Virtual Host with XAMPP

I like to develop my own PHP scripts and small applications. Up to now I’ve been uploading them to my online server. Of course that is bad practise because a small bug may open up a large security hole. My test scripts are reducing the security of things like my blog. So I’ve decided to use XAMPP to develop my scripts on my home computer before deploying them to the web.

If you’ve not heard of XAMPP it stands for

  • X (meaning cross-platform)
  • Apache
  • MySQL
  • PHP
  • Perl

It is a portable application which will work on a Mac PC or Linux. It comes bundled with pretty much everything you need to dive straight in. About a year ago I installed my own Apache, PHP and MySQL stack however it took me ages to install correctly, the MySQL stopped working and the installation was a pain to start and stop. On the contrary XAMPP is a breeze to install and comes with a control panel.

There are a few pitfalls to developing code for an online site on a home computer. Firstly the functions available to you online will vary from those on your home computer because the software works slightly differently on different operating systems. The other pitfall is to develop some code and then have to rename all the links when you deploy it.

This is where building a virtual host comes in. Say I want to make a cheese identifying application (I don’t) and I have purchased the domain name whatcheese.com. When developing my code on XAMPP I can set up a virtual host which means that when I go to whatcheese.com I am redirected to my localhost.

In XAMPP with Windows this is a matter of changing two files. They may not be exactly the same files though on other operating systems. Firstly go to your xampp installation and go to Apache > Conf > Extra and open up httpd-vhosts.conf. In this file we can reference all the virtual hosts we want to create. Add the following to the file.

NameVirtualHost *:80
<virtualhost *:80>
ServerAdmin sampe@mysample.com
DocumentRoot "C:/Program Files/xampp/htdocs" #existing document root
ServerName localhost #if you type localhost in your browser you will get this virtual host
</virtualhost>
<virtualhost *:80>
ServerAdmin webmaster@mywordprees.com
DocumentRoot "C:/whatcheese" #your new document root
ServerName www.whatcheese.com #your virtual domain name
ServerAlias whatcheese.com #your virtual domain name alias
<directory "C:/whatcheese"> #your new document root again
AllowOverride FileInfo AuthConfig Limit Indexes #set folder options
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
<limit GET POST OPTIONS>
Order allow,deny
Allow from all
</limit>
<limitexcept GET POST OPTIONS>
Order deny,allow
Deny from all
</limitexcept>
</directory>
</virtualhost>

This basically sets up a new virtual host in apache. If you go to whatcheese.com now you will be directed to the real online site whatcheese.com and if this doesn’t exist you will get an error like “server not found”. We need to tell our operating system to redirect to 127.0.0.1 (our computers local IP address).

On Windows you need to go to C:\WINDOWS\system32\drivers\etc\hosts Open up the file with a text editor and add the following lines to the bottom of the file:
127.0.0.1 www.whatcheese.com
127.0.0.1 whatcheese.com

This tells your browsers to go to 127.0.0.1 when they meet the domain name whatcheese.com as opposed to consulting a DNS server for the IP address.

That’s all there is too it. Restart apache and go to whatcheese.com and you will see whatever files you put in the C:/whatcheese directory. How cool! Now when you have finished developing your code on your home computer you can deploy it to your server without any problems. Just remember to remove the lines you wrote in the hosts file so you get sent to the online site.

You can leave a comment, or trackback from your own site.

One Response to “Virtual Host with XAMPP”

  1. On October 2nd, 2008 tagmaveageads said…

    How i may contact admin this site? I have a question.
    iijiivei

Leave a Comment

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

Proudly powered by WordPress with plane theme by me! | © 2007 - 2019 Thomas Milburn | Valid CSS and HTML