veni vidi Scripsi

Mac OS X Apache Mass VHosts (with support for htaccess and symlinks)

A while ago I wrote two posts, one on using Twig and another on using htaccess with Mac's Apache. I was reasonably happy with the described setups until I discovered that when I used something like <link rel="stylesheet" href="/style.css" media="all"> in my HTML it wouldn't work because my site was running at the default http://localhost/~heleen/website/index.html and so it would map the 'root' slash to http://localhost/ rather than http://localhost/~heleen/website/. I found a solution to this using vhosts, which works really well. But then I had to add another website, and another, and realised that surely there should be an easier way to do this.

After some searching and trial and error I finally found my ideal setup. Un­for­tu­nate­ly I couldn't execute the last step of the process (dnsmasq) because of some silly errors, but here's my PHP/Apache setup if you're interested (running Mac OS X 10.9 - Mavericks -).

If I've missed anything or if you have questions please don't hesitate to send me an e-mail (or comment once I've got the comments up and running).

Note

My language of choice is actually Python and for a motivation for setting this up in the first place I would like to refer to my Twig post.

Turn on Apache and PHP on your Mac

  1. Turn on Apache:

    • On Lion and below machines: turn Web Sharing on in your Mac's System Pref­er­ences Sharing. It will display the IP on where you can view the pages from your Sites directory. I don't know how this would work on Mountain Lion as they've removed that option from the sharing pref­er­ences;
    • On Mountain Lion and above machines execute the following command sudo apachectl start;
  2. Turn on PHP support in Apache by editing /private/etc/apache2/httpd.conf and un­com­ment­ing the line:

    LoadModule php5_module        libexec/apache2/libphp5.so
    
  3. Create the PHP.ini file by going to /private/etc/ and doing sudo cp php.ini.default php.ini. I also turned debug mode on: search for dis­play_er­rors in php.ini and make sure that Default Value is set to On.

  4. Create a /Users/[y­our_­com­put­er_user­name]/Sites directory if it doesn't already exist;

  5. If you'd like to run projects from other di­rec­to­ries (like I do) I recommend simlinking to them from your Sites directory. However that does mean that you'll have to turn this feature on in Apache by adding Fol­lowSym­Links to Options Indexes Multiviews in /etc/apache2/users/[y­our_­com­put­er_user­name].conf. See this Stack­over­flow question; OR follow along and use the setup below in the Using Virtual Hosts section.

  6. Restart Apache: sudo apachectl graceful.

Using Mass Virtual Hosts

This method makes adding websites to your Sites directory and running them a one action event. All you have to do is create (or symlink) the folder in the Sites directory.

  1. Make sure that in /etc/apache2/httpd.conf the following lines are enabled (not commented out) (this requires Apache v1.3.6 or above (default in Mac OS X Mavericks is 2.2.24):

    Include /private/etc/apache2/extra/httpd-userdir.conf
    
    Include /private/etc/apache2/extra/httpd-vhosts.conf
    
    LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
    
  2. Make sure your /etc/apache2/users/[y­our_­com­put­er_user­name].conf consists of the following lines:

    <VirtualHost *:80>
        UseCanonicalName Off
        VirtualDocumentRoot /Users/[your_computer_username]/Sites/%0
    </VirtualHost>
    
    <Directory "/Users/[your_computer_username]/Sites">
        Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
    

    You will need the Directory directive to be able to use htaccess and symlinks.

  3. Restart Apache: sudo apachectl graceful;

Now you can either install dnsmasq, a light­weight DNS forwarder and DHCP server, to do the domain/hostnaming au­to­mat­i­cal­ly, or you can opt to add one step to the website in­stal­la­tion: adding a line to /etc/hosts for each website.

Note

This setup writes all log entries to the default Apache log /var/log/apache2/. For custom logging you will have to search the internet yourself.

No Dnsmasq

If you opt for the latter all you need to do is add the following line to your /etc/hosts file for each website you add to your Sites folder:

127.0.0.1   [your-site-folder-name]

I usually use my-site.dev for the folder name and hostname.

Dnsmasq

You can also use Dnsmasq to manage the domains. It is normally installed via Homebrew on a Mac, however some strange behaviour on my Mac prevented me from installing Dnsmasq with it (it also wouldn't build from source). I have included some doc­u­men­ta­tion, but for now I'm afraid you're on your own.

Getting htaccess to work on a Mac (Lion) with Apache and using symlinks » « Recipe: Spinach, Tomato, Goat's Cheese, Potato & Egg Wraps