PHP in nginx on OS X

Written . Tagged OS X, PHP, nginx.

I recently got myself a Mac Mini to use as HTPC and server for all things serveable.

I’m trying out the nginx web server, but I had some trouble getting PHP working on OS X Leopard. I finally did, with lots of help from Christoffer.

I roughly followed these instructions, but with these differences:

I installed php with MacPorts like so:

sudo port install php5 +fastcgi +mysql5

Obviously remove the +mysql5 bit if you don’t want MySQL; if you want MySQL to autostart, install it separately first with

sudo port install mysql5 +server

and follow the instructions.

It’s possible that you need to get the fcgi port before installing PHP:

sudo port install fcgi

I’m not sure it’s necessary – we didn’t realize we should be using the php-cgi binary and not the php one until after I installed fcgi, so I didn’t check if it supported fcgi even before then.

You can confirm php-cgi works with fcgi like so:

$ php-cgi -m | grep fcgi

The script from those instructions needed some modifications. I put mine in /opt/local/etc/nginx/, by the way.

Obviously, the path to the PHP binary should be changed:


Also, the su line must be changed, since the BSD su that comes with OS X doesn’t take all those flags. Instead, you can do either of these two:

EX="/usr/bin/su -m $USERID \"$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\""
EX="/usr/bin/sudo -u $USERID $PHPFCGI -q -b \"$FCGIADDR:$FCGIPORT\""

The full shell script I use is available here. I should mention I’m not actually running under a “www-data” user; OS X doesn’t come with one by default, unlike some Linux distributions.

Hope this helps someone.

I only just got it running, so I’m not sure yet it works well, but it is running.