Building a LAMP development environment on Snow Leopard

How to install Apache, PHP, MySQL, MongoDB and a whole bunch of other useful packages on Mac OS X 10.6

You already have a copy of Apache that’s good enough for our purposes. I’m also assuming you’ll be happy with the bundled PHP 5.3. If you need a little bit more than the bundled PHP can offer, you’ll want to follow my instructions for reinstalling PHP 5.3 on Snow Leopard.

First, let’s make sure Apache is actually running

Sometimes Snow Leopard doesn’t have PHP enabled by default. Edit /etc/apache2/httpd.conf and look for a line that starts with:

#LoadModule php5_module

If there’s a # at the start, remove it. Save and exit.

Now make sure Apache is running. Either enable it in System Preferences > Sharing > Web Sharing or run this from the command line:

sudo apachectl start

Visit your new server in a browser and confirm that it works

You’re also going to need a few things that don’t come bundled with your Mac. For that you need to…

Install Xcode

You’ll need this later. We’re going to build things from source!

The easiest way is to install it from the App Store. If you don’t want to drop $5 on an app that you can get for free, it’s also on the DVD that came with your Mac (or when you upgraded to Snow Leopard). Next…

Install Homebrew

It’s a package manager for OS X. It’s hot.

There are a lot of ways to do this, but running this script is by far the simplest… The copypasta version:

ruby -e "$(curl -fsSLk"

Then install git (since that’s what Homebrew uses to update itself):

brew install git

Now that you have Homebrew you can…

Install MySQL via Homebrew

Check out how easy it is to do:

brew install mysql

But you prob’ly already guessed that. Now start it up. The instructions are in:

brew info mysql

As of right now, it looks like this:

unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp
mkdir -p ~/Library/LaunchAgents
cp /usr/local/Cellar/mysql/5.5.10/com.mysql.mysqld.plist ~/Library/LaunchAgents/
launchctl load -w ~/Library/LaunchAgents/com.mysql.mysqld.plist

But if you’re one of those bleeding edge kids, you can also…

Install MongoDB — or your favorite flavor of NoSQL — via Homebrew

brew install mongodb
brew install couchdb
brew install cassandra

Unfortunately, it’s not always that easy, because not everything lives in Homebrew.

If you already reinstalled PHP, you can skip ahead

… to the optional steps at the end. Otherwise, follow along as we…

Install PEAR from source

Unless you reinstalled PHP (e.g. from my aforementioned writeup) you’re going to want a copy of PEAR.

Homebrew lives in /usr/local (where your local code should be anyway), but it co-exists with anything else you want to put there… So we’ll test this out by installing PEAR in /usr/local.

cd /usr/local
curl | sudo php

When it finishes, the PEAR executable will be in /usr/local/bin, and the rest of PEAR will live in /usr/local/PEAR

Since this isn’t in Homebrew, it will take a bit more assembly

Add a php.ini (if you don’t already have one):

sudo cp /etc/php.ini.default /etc/php.ini

Edit your shiny new /etc/php.ini and find this line:

;include_path = ".:/php/includes"

And change it to this:

include_path = ".:/usr/local/PEAR:/php/includes"

And install PHPUnit (you’re doing unit tests, right?)

sudo pear channel-discover
sudo pear install phpunit/PHPUnit

This part is optional

(Optionally) install xdebug

brew tap josegonzalez/homebrew-php
brew install xdebug-php

When Homebrew is finished, it will give you a line to paste at the bottom of your php.ini. Do that too.

Grab a couple more tools to help you out

Bash completion makes life better:

brew install bash-completion

It won’t actually work until you add this to your ~/.bash_profile:

if [ -f `brew --prefix`/etc/bash_completion ]; then
  . `brew --prefix`/etc/bash_completion

Now set up bash completion for Homebrew:

ln -s `brew --prefix`/Library/Contributions/ `brew --prefix`/etc/bash_completion.d/

Apparently OS X forgot to ship with wget. Get it from Homebrew:

brew install wget

OS X can read /etc/hosts, but that’s not the Appley way to do things. Unfortunately, there isn’t a good manager for the canonical hosts management mechanism on your Mac. For that, you’ll need ghost:

sudo gem install ghost

Nobody likes using MySQL from the command line. Luckily, there’s a really sexy — and free — MySQL client for OS X. Download Sequel Pro.

Now go make something awesome!