My goal is to have nginx installed, listening on port 80 (running as root), and have it run when the computer starts.
The first part is easy:
$ brew install nginx
Now that nginx is installed, we need to move the plist file into ~/Library/LaunchAgents so launchd can load it:
$ mv /usr/local/Cellar/nginx/1.6.2/homebrew.mxcl.nginx.plist ~/Library/LaunchAgents/
(Notice the nginx version number here; make sure you use the right one for your installation!)
Now root needs to own it so it can run as root:
$ sudo chown root:wheel ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
The next part still baffles me, and it's where we try to get nginx to run when the computer starts. Run these next two commands to load the plist file into launchd and then start nginx:
$ sudo launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
$ sudo launchctl start ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
The -w option should tell launchd to enable the service by overriding the Disabled property. Ideally, we should be done here... everything I can find seems to indicate this will work. And it does in OS X Mavericks, but for some reason, when I rebooted in Yosemite, I got this error on the console:
10/22/14 10:10:29.932 AM otherbsd: Failed to bootstrap agent: path = /Users/matt/Library/LaunchAgents/homebrew.mxcl.nginx.plist, error = 119: Service is disabled
Well that's no good. Why is it disabled!? I have no idea, but a little Googling led me to an overrides.plist file. Edit it:
$ sudo nano /private/var/db/launchd.db/com.apple.launchd/overrides.plist
in line with all the other ones. From my understanding, -w is supposed to do this for you, but whatever.
Reboot your system (or log out then back in) and nginx should be running now:
$ ps aux | grep nginx
You can also check to make sure that launchd has it loaded with:
$ sudo launchctl list | grep nginx
If you see a 0 in the second column, that means it executed successfully. If you see a 1, something is wrong, and check the system logs (use the Console app and search for "nginx") to see what it is.
Good luck. May the brew be with you.