So far I was using MAMP for web development. It is a fine solution when used out of the box. For Localto, I needed to install MongoDB and I was able to do it within MAMP but that was a manual installation I would have to maintain with each new version of MAMP. I then decided to give Nginx a try, particularly for the Nginx-GridFS bridge to read files directly from MongoDB-GridFS.
At this point MAMP was not fit for the job anymore. I settled on installing my own stack and decided to use Homebrew the very good package manager for Mac OS X. Here is a walk down of the steps involved.
Installing Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"
The only thing to watch is to correct the PATH so that packages installed with Homebrew are found before the ones installed with the Mac OS X standard installation (for PHP for instance). Additionally, XCode needs to be installed together with Unix tools.
PATH=/usr/local/bin:/usr/local/sbin:$PATH
Installing Nginx
After installation, we need to copy the Nginx launch agent configuration to your LaunchAgents directory for automatic startup of Nginx after computer restart.
cp /usr/local/Cellar/nginx/1.0.7/org.nginx.nginx.plist ~/Library/LaunchAgents/
You can also start Nginx manually using
launchctl load -w ~/Library/LaunchAgents/org.nginx.nginx.plist
and stop it using
launchctl unload -w ~/Library/LaunchAgents/org.nginx.nginx.plist
Installing MongoDB
Same thing as with Nginx:
cp /usr/local/Cellar/mongodb/2.0.0-x86_64/org.mongodb.mongod.plist ~/Library/LaunchAgents/
You probably want to edit the configuration file of MongoDB to make it perfect for your need:
mate /usr/local/Cellar/mongodb/2.0.0-x86_64/mongod.conf
Installing PHP with FPM
There is no standard formula for php in Homebrew, but there are plenty of third party ones. This article gives all necessary details. Here we install PHP without Apache support but with FPM and internationalization and without MySQL support.
brew install https://github.com/adamv/homebrew-alt/raw/master/duplicates/php.rb --without-apache --with-intl --with-fpm
We need to create a log directory for FPM
mkdir /usr/local/Cellar/php/5.3.8/var/log
and create and edit the configuration file for FPM
cp /usr/local/Cellar/php/5.3.8/etc/php-fpm.conf.default /usr/local/Cellar/php/5.3.8/etc/php-fpm.conf
mate /usr/local/Cellar/php/5.3.8/etc/php-fpm.conf
for enabling the setting of pm.min_spare_servers and pm.max_spare_servers.
'Fix' the default PEAR permissions and config:
chmod -R ug+w /usr/local/Cellar/php/5.3.8/lib/php
pear config-set php_ini /usr/local/etc/php.ini
LaunchAgents configuration for PHP-FPM
In order to start PHP-FPM automatically with the Mac, like Nginx and MongoDB, we need to create a configuration file and place it in ~/Library/LaunchAgents/ (replace username by your username on the mac).
MongoDB PHP extension
That is to allow PHP connect to MongoDB.
That Mongo extension has to be added in /usr/local/etc/php.ini with the following line :
extension="/usr/local/Cellar/mongo-php/1.2.4/mongo.so"
Installing Xdebug
Note a necessary step but a very nice addition for debugging PHP code:
Xdebug has to be loaded in /usr/local/etc/php.ini with the following lines (asssuming PHP-FPM runs on port 9000 we have to choose another port than the default one which is also 9000, choosing here 9001):
zend_extension="/usr/local/Cellar/xdebug/2.1.2/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9001
xdebug.remote_autostart=1
Configuring Nginx
The configuration file for Nginx is found in /usr/local/etc/nginx/nginx.conf. You need to look in the documentation and in various examples on the web to make it the way you want. Here is a working configuration I came up with: