Posts Tagged ‘multiple sites’

Configuring virtual hosts with XAMPP

Sunday, April 20th, 2008

I use a nifty piece of software on my Mac called XAMPP, which flawlessly installs an entire PHP and MySQL (along with other features) development environment.

Of course, Mac OS comes bundled with Apache and there’s a lot of info on the Internet to help you configure the factory installed Apache with PHP and MySQL. That said, it’s much easier just to get a whole package that does it for you.

Not convinced? Take a look. You can even get it for Windows, although from experience I find WAMP does the job perfectly.

The most alluring feature of Apache is the ability to configure multiple hosts on one single machine. Professional web hosting companies use the same technique I will demonstrate here in order to host several websites on one physical node, whilst maintaining individuality for the hosted sites. It’s a little thing called “virtual hosts” and it’s something I couldn’t live without.

Let me give you a small example. I currently have four virtual hosts configured on my MacBook. This means that when I open my browser and navigate to “http://ge”, I’m forwarded to a local copy of this site for development purposes. I also have another - “http://xampp” which is a site for accessing the XAMPP administration page and a further two. You see the point. Whilst all DNS names point back to localhost, Apache sends each one to a seperate virtual directory containing a different site.

Configuring hosts
Before doing anything, you need to set up your hosts file. On a Mac, it’s easy to find, in /etc/hosts. I won’t cover doing that here, there are plenty of sites around the Internet to help you. If you’re a grizzled networking veteran you’ll know that hosts is a local file used for DNS resolution.

Specifying the virtual hosts
The thing I noticed with XAMPP is that the build of Apache it installs doesn’t like the standard virtual hosts configuration you specify with WAMP and other builds. In fact, it took me a good few days to find the technique in order to actually get it up and running properly. You should know where your virtual hosts are specified. In the standard httpd.conf file, Apache includes a file seperately for easy management of virtual hosts. You’ll need to un-comment this line:

# Virtual hosts
Include /Applications/xampp/etc/extra/httpd-vhosts.conf

On boot-up, XAMPP now knows to look in the extra directory for a file called httpd-vhosts.conf. Sure enough, with the original name of “vhosts”, this is where you specify your virtual hosts configuration.

This is the bit I found tough - the syntax to use. Luckily for you, all you have to do is copy what I write and fill in the blanks with your bits:

Quite obviously, you’ll see you will need to create a site directory within the htdocs directory to hold your site. Next, you should have noticed the ServerName and ServerAlias conditions. This bit - specified as “ge” for the purpose of this guide - should be the name of your local server that you’ve already configured to point back to 127.0.0.1 in your hosts file.

There isn’t much else to write on this topic. Obviously, it goes without mention, before expecting any changes to take effect you’ll need to restart your XAMPP installation.

If you have any problems, leave me a comment and I can help you out. Good luck and happy developing! :-)