nginx + PHP on Mac OS X
I’m a big fan of the nginx server (it’s lighter weight than Apache, and at this point I’m just more familiar with it, so it’s my server of choice). When trying to get it to play nicely with PHP, however … well, it leaves something to be desired.
But, with some persistence (and quite a bit of help), I got it up and running on my development machine. The one thing that I wasn't ever able to find an answer for (at least, an answer that worked) was to have my system start up FastCGI on launch. I really dislike having to start these things by hand and feel that any performance trade-off (always having this run in the background) is well worth it for the productivity boost.
This article addresses using a plist that runs on load (utilizing launchd), but as the author notes, it works inconsistently. After doing some digging of my own (by reading the nginx plist that ships with my MacPorts install of the server), I came up with the following:
#### com.panpainter.php-fastcgi.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.panpainter.phpfcgi</string>
<key>EnvironmentVariables</key>
<dict>
<key>PHP_FCGI_CHILDREN</key>
<string>2</string>
<key>PHP_FCGI_MAX_REQUESTS</key>
<string>1000</string>
</dict>
<key>ProgramArguments</key>
<array>
<string>/opt/local/bin/daemondo</string>
<string>--label=php-cgi</string>
<string>--start-cmd</string>
<string>/opt/local/bin/php-cgi</string>
<string>-q</string>
<string>-b</string>
<string>127.0.0.1:8888</string>
</array>
<key>OnDemand</key><false/>
<key>Debug</key><false/>
<key>RunAtLoad</key><false/>
</dict>
</plist>
After adding this to the /Library/LaunchDaemons directory (or symlinking it in to there, if you’d rather it live somewhere else), don't forget to run the following to ensure that it’ll run on load:
$ sudo launchctl load -w /Library/LaunchDaemons/com.panpainter.php-fastcgi.plist
Now, this probably is not the most elegant solution, and I’m relying on the MacPorts-depedent daemondo, but it works. If you know of a better way to do this, I’d be happy to hear it.