Managing local development websites on your Mac
I recently had my hard drive fail in my MacBook Pro I use for work, which, while a little painful, gave me the opportunity get a fresh, clean install of everything—especially my Apache config files. Like a lot of developers, my Apache config files had been hacked to pieces. This was a great time to re-think how I was doing things.
Apache according to Apple
My previous strategy of ignoring every line Apple included in httpd.conf and trying to write my own super genius configuration was a nightmare. My new strategy would be to follow the guidelines and files that Apple includes with the 10.5 Apache.
Configuration files
As of 10.5, Apple arranges Apache configuration files in the following manner:
- /etc/apache2/httpd.conf - the über config file
- /etc/apache2/extras/httpd-vhosts.conf - virtual hosts
- /etc/apache2/users/your_user_name.conf - options and access control (IP exceptions)
We’ll use only these files to set up an individual site for each development app/website.
First things first
Before you start editing files, turn on your local web server by going to System Preferences > Sharing and checking the Web Sharing checkbox.
Clean up httpd.conf
Assuming you’ll want to do local Ruby on Rails (via Passenger), SSL (with a self-signed certificate), and PHP development, make the following changes to you /etc/apache2/httpd.conf:
Listen 443
LoadModule php5_module libexec/apache2/libphp5.so
Include /private/etc/apache2/extra/httpd-vhosts.conf
# Rails via Phusion Passenger (mod_rails)
LoadModule passenger_module /Library/Ruby/Gems/1.8/gems/passenger-1.0.5/ext/apache2/mod_passenger.so
RailsSpawnServer /Library/Ruby/Gems/1.8/gems/passenger-1.0.5/bin/passenger-spawn-server
RailsRuby /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby
Add these lines to the end or your httpd.conf or just download this version with the changes already in place.
Add some virtual hosts
We’ll set up virtual hosts that respond to re-routed local DNS requests like acoolwebsite.local or companysite.local. To do this, we’ll have to first add some lines to the /etc/hosts file to pick up these requests.
/etc/hosts
Add any local development websites to your /etc/hosts file and point them to the loopback IP address, 127.0.0.1:
127.0.0.1 sslwebsite.local
127.0.0.1 companywebsite.local
/etc/apache2/extra/httpd-vhosts.conf
Now we’ll add corresponding virtual host entries in /etc/apache2/extra/httpd-vhosts.conf:
NameVirtualHost *:443
<VirtualHost *:443>
DocumentRoot /Users/your_user_name/Sites/sslwebsite
ServerName sslwebsite.local
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /Users/your_user_name/Sites/companywebsite
ServerName companywebsite.local
</VirtualHost>
Access control
Since you’re the only one developing on your machine, you can safely open up all Apache options and enable overrides in /etc/apache2/users/your_user_name.conf:
<Directory "/Users/your_user_name/Sites/">
Options All
AllowOverride All
Order deny,allow
Deny from all
Allow from 127.0.0.1
Allow from 72.14.207.99 # Joe's computer
# Order allow,deny
# Allow from all
</Directory>
This only allows requests to be made from your own machine. You can add IP exceptions to allow access from coworkers’ machines or for demoing purposes. I leave the last two lines commented in case I need to temporarily open a site up to the world or if I don’t know the IP of a demo machine.
Restart Apache
That’s it—restart Apache with sudo apachectl restart and you should be in business.
Comment on this post