24 May, 2009

running nginx on OSX

I run Leopard Server, and I have no need for Apache 1.3, Apache 2.x, Apple's wikid, their calendar server, and a bunch of other things that are all fixated on Apache and its configurations in launchd. Right now would be a good time for you to familiarize yourself with the launchd and launchctl man pages, although the referenced ("See Also") command, launch, does not exist.

I've written an nginx launchd configuration file which happily starts and stops on command, starts on boot of course, and has the required configurations. nginx, in general, is a simpler, better written, faster httpd than apache is (which is, after all, a patchy web server). It follows:



<?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>nginx</string>
<key>Program</key>
<string>/usr/local/sbin/nginx</string>
<key>KeepAlive</key>
<true/>
<key>NetworkState</key>
<true/>
<key>StandardErrorPath</key>
<string>/var/log/system.log</string>
<key>LaunchOnlyOnce</key>
<true/>
</dict>
</plist>



You will want to place this in /System/Library/LaunchDaemons/nginx.plist, and it is helpful to issue "launchctl load -F /System/Library/LaunchDaemons/nginx.plist", after which it should behave correctly. Of course, the corresponding unload command is simply "unload", with or without the -F. Note that this launches nginx on boot, rather than when you choose it to. It also tells it to log stderr to syslog, which may not be to your preference (nginx likes to log to /usr/local/log/nginx.err). But I think you get the idea.

Please Donate To Bitcoin Address: [[address]]

Donation of [[value]] BTC Received. Thank You.
[[error]]

1 comment:

Christopher Johnson said...

Two things:

The NetworkState parameter has been deprecated and moved into the KeepAlive dictionary. I just removed it, which may have caused thing 2.

For some reason it is important to not daemonize nginx. Doing so causes launchd to intermittently fail. Because I also wanted to start nginx from the command line daemonized, I changed the program invocation to this:

ProgramArguments

/usr/local/nginx/sbin/nginx
-g
daemon off;


Thank you for the post. It was most useful!