Setting up a PHP 5.4 development environment with Homebrew on OS X
One of my favorite features of PHP 5.4 so far has been the server mode that it provides. Sure, traits are awesome and the fix for closures and $this is great, but being able to finally roll my own development environment, without Apache and vHosts is amazing. Normally the task of custom compiling PHP 5.4 would be a major pain, however with the help of the Homebrew project, it is a breeze.
Note: These instructions are intended for Mac OS X and were written using v10.7 (Lion) and v10.8 (Mountain Lion) as a testing platform.
1) Let's get started by Installing Homebrew
Note: For this step you will to need to have X-Code installed. If you do not already, go to the App Store and install it now.
Per the instructions in the Homebrew installation wiki page:
Open a terminal and run the following:
/usr/bin/ruby -e "$(/usr/bin/curl -fsSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
As the guide says: Read the script if you like. It is always a good idea to make sure something like this isn't going to do something malicious to your computer. Personally, I found it to be clean, but be your own judge.
If You already have Homebrew installed, then be sure to update it as follows
brew update
2) Tap the homebrew-php formulas.
Run the following in your terminal window.
brew tap josegonzalez/homebrew-php
This will hook you up to a full repository of formulas for installing php 5.3 and php 5.4 extensions and addons. Everything from XDebug to Composer can be easily installed this way. For a full list of the recipes, checkout the formula list.
3) Install PHP 5.4
Run the following in your terminal window.
brew install php54
If you're using MySQL, try this install:
brew install php54 --with-mysql
This will take a few minutes to install.
4) Setup your path
While the official docs say this is unnecessary to access php, if you want to be able to easily install things with PEAR, like php-unit, then you need to setup your path correctly.
Add the following to your path, if you not sure where that is, then just edit the file ~/.bashrc
PATH="$(brew --prefix php54)/bin:$PATH"
This will make sure the PHP and all it's related binaries are easily found.
5) Running the local web server.
The web server can be accessed in a in a couple ways. The two most useful in my book are
a) As a dumb directory server (serving files directly from the current path)
php -S localhost:8000
Now http://localhost:8000 will serve files in the current directory.
This is useful if you have a very simple application and you either have flat html that you're mocking up or just a simple php script that you're working with. For me, this is how I like to launch a quick JavaScript based project, without the overhead of setting up a vhost.
b) As a fully routed application stack
php -S localhost:8000 router.php
In this case the router should look something like this:
<?php // router.php if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) { return false; // serve the requested resource as-is. } else { // Load your application framework here... echo "<p>Welcome to PHP</p>"; } ?>
Now all requests through http://localhost:8000/this-is-a-request will be routed through this router to decide what to do with it. If false is returned, then a matching file will bre returned instead.
To read more about PHP 5.4 web servers and the router file, read the full docs on php.net.









