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. Unfortunately 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
Turn on Apache:
- On Lion and below machines: turn Web Sharing on in your Mac's System Preferences 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 preferences;
- On Mountain Lion and above machines execute the following command sudo apachectl start;
Turn on PHP support in Apache by editing /private/etc/apache2/httpd.conf and uncommenting the line:
LoadModule php5_module libexec/apache2/libphp5.so
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 display_errors in php.ini and make sure that Default Value is set to On.
Create a /Users/[your_computer_username]/Sites directory if it doesn't already exist;
If you'd like to run projects from other directories (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 FollowSymLinks to Options Indexes Multiviews in /etc/apache2/users/[your_computer_username].conf. See this Stackoverflow question; OR follow along and use the setup below in the Using Virtual Hosts section.
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.
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
Make sure your /etc/apache2/users/[your_computer_username].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.
Restart Apache: sudo apachectl graceful;
Now you can either install dnsmasq, a lightweight DNS forwarder and DHCP server, to do the domain/hostnaming automatically, or you can opt to add one step to the website installation: 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 documentation, but for now I'm afraid you're on your own.
Resources
htaccess on a Mac
- Apache + .htaccess + Mac OS X;
- Stackoverflow question: 'Install CakePHP on Mac OS X: Apache Problems'.
Apache + PHP Setup
VirtualHosts
- VHOSTS on Mac Leopard;
- Install Apache PHP MySQL on Mac OS X;
- How to install Apache and PHP on a Mac OS X 10.8 MAMP part 1.
Mass Virtual Hosts
- Example VirtualHost Apache Configuration, this is the one I actually used;
- Apache Virtual Host;
- Apache Setup;
- Apache Dynamic Virtual Hosts;
- Apache Docs on Mass VHosts.
Dnsmasq