Setting up Virtual Hosts with XAMPP on a Mac

Sometimes, putting a website into a subdirectory just doesn’t cut it. You need to have the website act like it’s on it’s own first party domain. But how do you do this with XAMPP? XAMPP uses Apache web server and to do this with Apache, you would create virtual hosts.

To create some XAMPP virtual hosts on our Mac, we need to

* create a virtual hosts entry for each virtual host
* include the virtual hosts file in the httpd.conf
* add an entry in your hosts file for each virtual host

By: Procsilas Moscas

Creating virtual host entries
If you know vi or nano or have TextMate installed, you could use those. My examples will use the Mac built-in editor, TextEdit for simplicity.

Open Terminal, if it’s not already open. Then type this command to edit the httpd-vhosts.conf virtual hosts file.

sudo -s /Applications/ /Applications/XAMPP/etc/extra/httpd-vhosts.conf

Enter your password, if necessary.

Get rid of, or comment out these lines…

<VirtualHost *:80>
DocumentRoot "/Applications/XAMPP/xamppfiles/docs/"
ErrorLog "logs/"
CustomLog "logs/" common

<VirtualHost *:80>
DocumentRoot “/Applications/XAMPP/xamppfiles/docs/”
ErrorLog “logs/”
CustomLog “logs/” common

Add these lines…

# this keeps http://localhost working
<VirtualHost *:80>
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
ServerName localhost

# this adds a virtual host called sub.local which will have files located in
# /Applications/XAMPP/xamppfiles/htdocs/sub.local.dir
<VirtualHost *:80>
DocumentRoot “/Applications/XAMPP/xamppfiles/htdocs/sub.local.dir”
ServerName sub.local

The Apache Software Foundation website has the definitive guide to configuring apache virtual hosts.

You can have several virtual hosts by defining their DocumentRoot and ServerName. You’ll just need to enter each ServerName into the hosts file (see below).

Save it.

Include the virtual hosts file in the httpd.conf

Open Terminal, if it’s not already open. Then type this command to edit the httpd.conf configuration file.

sudo -s /Applications/ /Applications/XAMPP/etc/httpd.conf

Enter your password, if necessary.

Look for the following in the file…

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

Remove the comment marker from the Include line, like this

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

Save it.

Edit the hosts file to include an entry for each new virtual host

Open Terminal, if it’s not already open. Then type this command to edit the hosts file.

sudo -s /Applications/ /private/etc/hosts

At the end of the file, make an entry for each virtual host name. Like this… sub.local

If you have more than one virtual host, just enter each virtual host name on a separate line.

Save it.

Use XAMPP Control to stop and then re-start Apache.

Open your browser and make sure http://localhost and each virtual host works by entering the ServerName into the browser like this…


More information:

Author: Eric Holsinger

Eric Holsinger is a software professional and photography enthusiast in Southern Maine.

3 thoughts on “Setting up Virtual Hosts with XAMPP on a Mac”

  1. Great post! Of course the Lion Server package also has its upsides, is much cheaper than it used to be, and comes with Apple’s excellent Server Admin Tools among other benefits….but this is a great way to go and with this guide, should be fairly easy to undertake.

    Personally I’ve found that in the long run, when it comes to dealing with file/directory permissions, files getting overwritten/replaced/etc during system updates, urgent security patches, etc. going with OS X Server is a wise move (particularly now that Lion Server is so much cheaper than previous versions)….but for those that don’t want/need to go that route, for whatever reason, this is a great solution. Thanks for posting about it!

  2. Thank you! This made it really easy.

    Just wanted to add one thing that caught me up. For anybody wanting to use document root that isn’t XAMPP’s htdocs, you have to edit httpd.conf and change “User nobody” to “User yourMacLoginUsername”. This got rid of the access denied error I was getting. In case it helps anyone else that finds this in the SERPs.

Leave a Reply