Installing Nginx And PHP on OS X

I hate using PHP. There was a time I loved it and looked forward to the release of PHP 5. These days I I use it only when I really have to, for example being asked to update or troubleshoot an old code base. Normally I would just create a virtual machine with linux on it, but this time I thought I would use homebrew to instal a local stack on my Maverick development machine. So here goes:

  • Mac OS X Maverick
  • Nginx
  • PHP 5.4 as a FastCGI using PHP-FPM

Installing Homebrew

[Error: Macro 'alert' doesn't exist]

If you have not used or heard of homebrew before, then Homebrew is a package manager very similar to gentoo's package management system. It will download and compile and install any open souirce software you need.

First install Apple Command Line Developer Tools

The command line developer tools can be downloaded from Apple's developer website at https://developer.apple.com/downloads/index.action?name=for%20Xcode%20-

Once the command line tools have been installed, installing Homebrew is as simple as:

ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
    ...
    ==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/. /usr/local/bin
    ==> Downloading and Installing Homebrew...
    ==> Installation successful!
    You should run `brew doctor' *before* you install anything.

Install PHP 5.4 With FPM on Mac OS X

brew search php

This will give you a long list of available brews for php 5.2, 5.3, 5.4 packages. We need 5.4 and specifically from a certain brew

brew tap josegonzales/php
brew tap homebrew/dupes

We can list the options for installing php54 with the following command.

brew options php54

We shall build it with the following command

brew install php54 --with-fpm  --with-imap  --without-apache --with-debug

This will take some time. Go make some coffee, play with your kids if you have any or whatever.. After a long wait we can verify the php and php-fpm version we have installed using the following commands:

php -v
php-fpm

Ensuring that PHP-FPM starts when we start OS X

[Error: Macro 'alert' doesn't exist]

If you want that php-fpm is automatically started do:

cp /usr/local/Cellar/php54/5.4.15/homebrew-php.josegonzalez.php54.plist ~/Library/LaunchAgents/

Otherwise you can use the following to start and stop

To Start:

launchctl load -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php54.plist

To Stop:

launchctl unload -w ~/Library/LaunchAgents/homebrew-php.josegonzalez.php54.plist

Installing Nginx on Mac OS X

brew install nginx
[Error: Macro 'alert' doesn't exist]

Copy the start/stop script. If you plan to use them. On my development machine, I do not use them since I prefer to use my own start stop scripts and also use custom configurations which will become clear further down in the Configurations Section. You can just skip this part if you wish.

cp /usr/local/Cellar/nginx/1.4.1/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/

To Start

launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

To Stop

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist

Configuration

OK we now have the required software in place. We only now need to configure. For my projects and websites I like to have the following structure. At the minimum below is what I have:

$PROJECT_HOME/bin          # The bin directory shall contain our start/stop scripts
$PROJECT_HOME/etc          # The etc directory  shall contain the nginx and php-fpm configuration
$PROJECT_HOME/htdocs    # This is where the website home is. 
$PROJECT_HOME/logs        #  This is where we keep the logs