Configuring VirtualHosts in XAMPP on Mac

A few weeks back I rejoined the “Cult of Mac” when I replaced my old Asus notebook with a MacBook Pro, and since then I’ve been busy settling into my new OS X workflow. I do all my development locally, so one of the first applications I installed was XAMPP, a cross platform Apache/MySQL/PHP stack. While I know that MAMP is very popular on Mac, I have been using XAMPP for many years so I thought I’d stick with what I know.

Installing XAMPP was a snap, but when I came to create my own Apache VirtualHosts things started getting fiddly. Here are the steps I followed to get everything running smoothly.

What are VirtualHosts?

First, some quick background on what we’re trying to achieve.

VirtualHosts allow Apache to map a hostname to a directory on the filesystem. You can set up as many VirtualHosts as you need, so that each website operates under its own hostname. For example, you might want to map mysite.local to /Users/yourusername/mysite. To test your development site all you would need to do is plug “http://mysite.local” into your browser’s address bar.

Enable VirtualHosts

The first thing you’ll need to do is open the file /Applications/XAMPP/xamppfiles/etc/httpd.conf in your favourite text editor. Look for the following lines:

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

Uncomment the second line by removing the hash (#), so that Apache loads your custom VirtualHosts configuration file:

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

Create your VirtualHosts

Open the file /Applications/XAMPP/xamppfiles/etc/extra/httpd-vhosts.conf. Towards the bottom of the file you will see some example VirtualHosts, which you should comment out or delete.

At the bottom of the file, add ‘localhost’ as the default named VirtualHost:

# localhost
<VirtualHost *:80>
    ServerName localhost
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
    <Directory "/Applications/XAMPP/xamppfiles/htdocs">
        Options Indexes FollowSymLinks Includes execCGI
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

This step is necessary to ensure that http://localhost still points at XAMPP’s htdocs directory once we’ve created our custom VirtualHosts. Personally I don’t use the htdocs directory a lot, but occasionally it’s useful to have somewhere to perform quick tests.

Now you are ready to create your own VirtualHosts. After the default localhost that you just created, add:

# My custom host
<VirtualHost *:80>
    ServerName mysite.local
    DocumentRoot "/Users/yourusername/path/to/your/site"
    <Directory "/Users/yourusername/path/to/your/site">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog "logs/mysite.local-error_log"
</VirtualHost>

In the above example you should replace “mysite.local” with your own hostname. This can be anything you wish, but make sure you choose a hostname that won’t conflict with a real domain name. Using a .local extension makes it obvious that the site is hosted locally rather than on a public web server.

The path to your website can point at any folder in your OS X user directory. I store most of my sites inside of Dropbox so that I can access them on both my home and work machines. If your path includes spaces, make sure you enclose it in quotes, like in my example.

Edit your hosts file

Once you’ve saved your httpd.conf and httpd-vhosts.conf files, the next step is to edit your OS X hosts file so it knows how to handle your new ServerName. The hosts file is used by OS X to map hostnames to IP addresses. In this case we want to map your new ServerName to the IP address 127.0.0.1, which is your localhost.

Fire up a Terminal instance, and at the prompt type the following command:

sudo nano /etc/hosts

Enter your OS X password when prompted, and the hosts file will be opened in the nano text editor. You’ll see that the hosts file already contains some default hostname mappings (e.g. “127.0.0.1 localhost”). Use your keyboard’s arrow keys to navigate to the bottom of the file and add your own mapping:

# XAMPP VirtualHost mappings
127.0.0.1 mysite.local

Save the host file using the key combo control+o, and pressing return when prompted to choose the filename. Close the file using control+x.

Restart Apache

So that your changes take effect, restart Apache. This can be done using XAMPP Control, which is found at /Applications/XAMPP/XAMPP Control.app.

Point your browser at http://mysite.local (or whatever ServerName you chose) and you should see your website. However, there’s a chance that instead you’ll be faced with a…

403 error

Because Apache runs as the ‘nobody’ user by default, it may not have adequate permission to browse your OS X user directory or some of its subdirectories, in which case you’ll see a 403 ‘access forbidden’ error when you try and view your development site. Similarly, you may find that although you can view your dev site, PHP throws errors when you attempt to write files or make directories on the filesystem.

To fix this you can configure Apache to run as your OS X user. Open /Applications/XAMPP/xamppfiles/etc/httpd.conf and look for the following lines:

# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
#
User daemon
Group daemon

Change User to your OS X username, and save the file:

User yourusername

Restart Apache and you should now be able to navigate your site without any issues, including manipulating files and folders using PHP.

If you have problems viewing the XAMPP splash pages at http://localhost now that Apache is running as your user (e.g. nothing happens when you try to set the language), then you’ll need to give your user read/write privileges on the file /Applications/XAMPP/xamppfiles/htdocs/xampp/lang.tmp.

Making the change I’ve described above carries certain security risks, and if you choose to run Apache as your OS X user then you’ll need to be quite certain that XAMPP is not accessible from outside your local network. From what I understand, XAMPP’s built in security features ensure that this is the case out-of-the-box, and it is straightforward to beef up security for additional piece of mind.

If you’re not convinced that it’s safe to let Apache run as your OS X user, another option is to change the permissions of your dev directories so that the ‘nobody’ or ‘_www’ user can read/write to them.I suspect that I would quickly tire of setting folder permissions, which is why I have opted to take the path of least resistance!

Conclusion

Hopefully this post helps someone else to get XAMPP up and running on OS X. I imagine that MAMP Pro streamlines this process somewhat (I know it has a wizard for creating VirtualHosts), but as long as you don’t mind getting your hands dirty XAMPP is a fantastic way to learn how Apache actually works.

53 thoughts on “Configuring VirtualHosts in XAMPP on Mac

  1. Stu says:

    Thanks Jonathan. I used to use XAMPP, then MacPorts, but now I’m using MAMP. I’m not sure why, but I imagine it was something to do with the simplicity. I’ll know where to look for instructions about vhosts if I go back to using XAMPP though.

  2. Jonathan says:

    @Stu I will probably spring for a copy of MAMP Pro, but the challenge of getting XAMPP to work was too tempting!

  3. mauro says:

    following your instructions only the front page would work for some reason giving 404 error code on the rest of my drupal installation.

    i went searching (for a way to suicide) for other info in virtual servers and i used this code:

    Options All
    AllowOverride All
    Order allow,deny
    Allow from all

    replaced with yours

    Options Indexes FollowSymLinks Includes execCGI
    AllowOverride None
    Order Allow,Deny
    Allow From All

    and it fixed it

  4. Jonathan says:

    Good catch Mauro. For htaccess rewrites to work AllowOverride needs to be set to All instead of None. I will amend my example.

  5. Erik says:

    Question which app works best for the Mac’s XAMPP or MAMP, I have XAMPP installed; but would like to go with what’s going to give me less aches and pains??? I’m using a Mac Mini :)

    Erik

  6. Hello,

    Could you probably do a tutorial on how we can setup a xampp server and share it locally on a dynamic IP address host? I have been having a hard time figuring it out!

    Thanks,
    Mihir.

  7. Matt says:

    Thanks for this. Most tutorials are for PC which leave out the editing of the hosts file!

  8. John says:

    Thank you very much, this got me up and running in minutes! Outstanding!

  9. Anthony says:

    Thank you so much, this was a life saver. I’m setting up an Intranet Training Site at work through WordPress and we’re trying to have links access files on our network shares, this works perfectly.

  10. Renata says:

    You saved my life! Thank you thank you thank you so freaking much!!! Your post made my day! Thanks for sharing :)

  11. Den says:

    So helpful! Thank you for a very complete (and working) example!

  12. Mauro says:

    There is a an error in the path of the Forbidden 403 fix.

    Quote “To fix this you can configure Apache to run as your OSX user. Open /Applications/XAMPP/xamppfile/etc/httpd.conf and look for the following lines…”

    It should be /Applications/XAMPP/xamppfiles/etc/httpd.conf.

    Good tutorial!

  13. Jonathan says:

    @Mauro Good spotting!

  14. Garth says:

    You’re a legend! That was really easy to follow and thanks for putting in the bit about the 403 error. Thanks!

  15. Mike says:

    Great tutorial — I’m primarily a designer so command-line stuff really worries me but I was quite comfortable going through step by step. EXCEPT where I had a 404 error I now have ‘the dreaded 403 error’. I’ve changed the httpd.conf file so that user ‘nobody’ is replaced by me. But I still get the error… any tips?

  16. Elisa says:

    Works for me. Thanks so much!

  17. ravi says:

    Awesome! Thanks so much!

  18. pjohnkeane says:

    Really handy – thanks!

  19. purusottama says:

    Hello ,
    M try above the line .
    But still a problem ie in start Apache is show me “Apache busy ”
    if any think to do except than this .

    Thank you
    purusottama

  20. joshuak says:

    This saved my life! Thank you!

  21. Lauren says:

    Thank you for this. This is amazing and so helpful!

  22. tknox says:

    This is pretty awesome, but I’m getting a bit of an odd result. Apache appears to think that now when accessing the virtual host through whatever.local it just displays the directory contents. It doesn’t display an index page, php isn’t processed (just displays code plaintext)… any ideas why?

  23. Toni Oriol says:

    Hi,

    Many thanks for the tutorial, it’s the best I’ve found. Only one thing: some routes like this “/Applications/XAMPP/xamppfile/etc/httpd.conf” are misspelled, you need to change “xamppfile” by “xamppfiles”. Sorry for my english, I hope you understand what I’m saying.

  24. Jonathan says:

    @Toni Orial you are quite right! I’ve correct my misspelling.

  25. Wigid says:

    Thank you so much!
    your saving my day

  26. Add the below command with Directory tag of httpd.conf
    Options FollowSymLinks

  27. Jonathan says:

    I have updated these instructions to reflect recent XAMPP versions. Specifically:

    – My example VirtualHost now includes the “Require all granted” directive instead of “Allow From All” and “Order Allow,Deny”.
    – The directory path for the localhost VirtualHost has been updated.
    – XAMPP now runs Apache under the user daemon/daemon instead of nobody/nogroup, and this is reflected in the instructions.

  28. Christian says:

    Thanks a lot for this tutorial, works fine!

  29. Matt Stone says:

    Thanks Jonathan, very handy guide.

  30. Andrew says:

    Thanks for the great tutorial – clear and to the point. I’ve got vhosts set up on my machine now.

    Here’s a tip for people having 500 Server errors: Make sure your project directory is not inside of your main htdocs directory. If it is, apache will end up in a redirect loop. You’ll need to put each of your projects somewhere else, like your home folder.

  31. Robert says:

    Thanks Jonathan, this really helped me out. I’ve found an issue: while changing user daemon/daemon to [username]/daemon localhost/phpmyadmin breaks. The interface loads but trying to use it gives the error “Token mismatch” changing the user back to daemon/daemon fixes the issue.

    Have you come across this before and if so is there a way to get both working?

  32. Bart Falzarano says:

    Followed all the steps but receiving the following error when I try to navigate to mysite.

    Server error!

    The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there was an error in a CGI script.

    If you think this is a server error, please contact the webmaster.

    Error 500

    mysite.local
    Fri Oct 25 16:42:06 2013
    Apache/2.2.14 (Unix) DAV/2 mod_ssl/2.2.14 OpenSSL/0.9.8l PHP/5.3.1 mod_perl/2.0.4 Perl/v5.10.1

    The error in mysite.local-error_log reads:

    Fri Oct 25 16:42:06 2013] [crit] [client 127.0.0.1] configuration error: couldn’t perform authentication. AuthType not set!:

  33. Bart Falzarano says:

    Hi Jonathan,

    Was able to resolve the Server 500 error after I added Satisfy Any and Allow from all to the httpd-vhosts.conf file in between the virtual host <Directory section as listed below. Excellent tutorial overall for configuring Virtualhosts in XAMPP for Mac!

    # My custom host

    ServerName mysite.local
    DocumentRoot “/Users/yourusername/path/to/your/site”

    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Satisfy Any # Added to fix the Server 500 error
    Allow from all # Added to fix the Server 500 error
    Require all granted

    ErrorLog “logs/mysite.local-error_log”

  34. LORIS says:

    Excuse my English I translated with google.

    Thank you, your instructions above are excellent.
    I do not use mac, I use linux and I have adapted your instructions and it works
    perfectly.

    Greetings

  35. Barry Horbal says:

    You said that you like to store your sites on Dropbox, and I would like to set it up that way as well. Unfortunately, when I set it up, I am getting an “Access forbidden!” error. Here’s what I’ve got:

    httpd-vhosts.conf

    #Sing For Your Seniors

    ServerName sfys.dev
    DocumentRoot “/Users/barryhjames/dropbox/www/sfys”

    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted

    ErrorLog “logs/sfys.dev-error_log”

    Any ideas why this isn’t working? Thanks for the post!

  36. Jonathan says:

    @Barry Did the permissions fixes I suggested in the article help?

  37. Excellent article and detailed instructions… helped me fix what was wrong my my previous attempt to set up a virtual host :)

  38. Jaume says:

    To fix the 500 error a used that cde between the directory tag:

    Options Indexes Includes execCGI
    AllowOverride None
    Order Allow,Deny
    Allow From All

    Good tutorial!

  39. Moses says:

    If the host path is not detected after updating /etc/hosts, try /private/etc/hosts

  40. giuliam says:

    Hi,

    thank you for the great post!

    I’m trying to create a virtual host on my Mac OS X 10.9.1 where I installed XAMPP.

    I followed your tutorial step by step, but I keep getting the Error 403.

    As you suggested, I tried editing the httpd.conf file by substituting

    User daemon
    Group daemon

    with

    User my_Mac_username
    Group staff

    and also

    User my_Mac_username
    Group admin

    as well as

    User my_Mac_username
    Group daemon

    but I keep getting the Error 403, and specifically, as per the log file:

    [Sun Feb 02 11:19:04.439293 2014] [authz_core:error] [pid 24704] [client 127.0.0.1:55398] AH01630: client denied by server configuration: /Applications/XAMPP/xamppfiles/\xe2\x80\x9c

    I also made sure my_Mac_user has all the permissions for the XAMPP folder and subfolders but nothing.

    It would be really great if you could help!

    Thank you very much

  41. Jonathan says:

    @giuliam In recent versions of XAMPP your vhost needs to be configured slightly differently. Try removing:

    Allow From All
    Order Allow,Deny

    and replace with:

    Require all granted

    I have updated the post to reflect this change. Sorry for the confusion.

    Note: You will still need to update your user/group in httpd.conf. I have found that:

    User my_Mac_username
    Group staff

    works for me.

  42. Matt says:

    Thank you so much! Your thorough guide helped me with performing dev tests on my sites that are located in different directories and already had git installed, thus saving me the trouble of having to rely on htdocs solely for testing them.

  43. Bao Anh Le says:

    Thank you so much!….
    I found your tutorial really really useful!!

    Thank you so much once again!

  44. manjane says:

    The only good solution for the `Forbidden 403` error I’ve seen. Thank you sir!

  45. Doppelherz says:

    I followed the instructions but the 403 is still there.

    – Virtual Hosts enabled: check!
    – added custom host with a path folder to a simple index.html: check!
    – added hosts file entry: check!
    – change username: check!
    – don’t know if group also has to change: ?
    – restart apache server: check!
    – read/write privileges to lang.tmp: check!

    but still 403 :( Help me…

    EDIT:
    If the username is changed, I cannot restart the Apache Server or even stop it. When i change it back to daemon, I can stop the server. Now I change it to my Username agein but the Apache Server wont start. What can I do know?

  46. Doppelherz says:

    Log said: AH00544: httpd: bad group name
    so i changed the group name back but my username as my osx username. now the server starts and stops, but the 403 error is still there :(

  47. cloudxiao says:

    Thanks! I’ve solved my problem.

  48. Jeff S. says:

    I had so many problems getting around the Access Forbidden error. Your post helped me tremendously and now I am doing awesome! Thanks for your hard work.

  49. Fran Marzoa says:

    This didn’t work for me using quotes in Directory name, since Apache insists to take such file names as relative to /Applications/XAMPP/htdocs or whatever the root dir is. It worked by removing such quotes. I mean, instead of:

    DocumentRoot “/Users/yourusername/path/to/your/site”

    use:

    DocumentRoot /Users/yourusername/path/to/your/site

    Maybe there is another way to solve it, since this could be a problem if you have directory names with spaces between (a ‘\ ‘ for each space could solve that anyway), but for me it is working this way.

    Now I have a permission issue, but that’s another story… ;-)

    PS. The quotes problem is probably related to have copy&paste them from here. I have recalled that once I have a similar problem with code on other website because the quotes representing there weren’t the actual ” character, despite they look the same way.

  50. macandes says:

    Thank you Jonathan!
    User yourusername did it! :)

  51. shashika hashan says:

    Thank you very much… :)

  52. Don diego delavega says:

    great tutorial , Thanks

  53. As an .net iiS user, I felt a little cautious about using PHP and XAMPP. But your articled helped me through some of the initial challenges. I have to say Whew…. and thanks.
    I am certain you are on StackOverflow but I will give a shoutout to your site.
    Again, thank you for the help.

    M

Comments are closed.