How do you get nginx to load and run when your Mac boots up, while still having it run on port 80?
it’s a problem I’ve been trying to solve for near a year now. And it seemed that no amount of blog post reading or trial and error would work… but I finally found the answer today!
Start nginx With launchctl, When Your Mac Boots Up
The root of the fix is one thing that I wasn’t aware of previously. You need to put your plist file in /Library/LaunchDaemons, not in ~/Library/LaunchAgents like the Homebrew instructions say to do. You also need to copy the file over, not just symlink it – again, going against the Homebrew instructions. Lastly, use the -w option with launchctl.
And now nginx will load when you reboot your system!
Why Does This Work?
The trick to this is that Mac OSX won’t let anything other than “root” or “system” level services use a port number below 1024. This is well documented and well known, but the solution to the problems seems to have been left to the “you should know this, and I’m not going to tell you if you don’t already know it” realm of open source community douche-baggery. But when you want and/or need nginx to load on port 80 (like I do), then you have to jump through some magic hoops that the homebrew installation doesn’t tell you.
The first trick is that you don’t want to run nginx as your user. If you do that, you have to run it as a sudo command, which typically involves a lot of dumb workarounds and script files. No thanks.
Instead of trying to use ~/Library/LaunchAgents, then, you need to use the system launch daemons folder. This will let the plist file run the command as a system level service, giving it access to port 80.
Copy The File. Don’t Symlink It
The second trick is that you need to copy the plist file in to the /Library/LaunchDaemons folder, instead of just symlinking it. I tried a symlink first, but it fails saying there are permissions errors with the path. Even if you sudo symlink it, it will fail due to permissions problems on the original file, not the symlink itself.
So instead of creating a symlink like the Homebrew instructions say, just copy the .plist file to the LaunchDaemons folder. The potential damage here, is if the default plist file for nginx changes. But you can always just replace the LaunchDaemons file when you update nginx.
Use -w With Launchctl
The last trick is the -w option when loading and unloading nginx. This might not strictly be necessary, but I read a few places that said it was needed. From what I understand, using -w will toggle the “disabled” flag when you unload / load the service. I guess that will stop the service from loading on startup when you unload it, and allow it to run when you load it with this flag.
Hope That Helps
You should know that this doesn’t start nginx when you log in. Rather, it starts it when OSX boots up. This is necessary, though, if you want to use port 80 without stupid hacks.
I certainly didn’t figure this out myself. It took me a ton of work and trying and digging to get it working. The largest help that I found was on Steven Octo’s blog (link no longer working, so I removed it). He got me turned in the right direction with copying the plist file and using LaunchDaemons. Thanks, Steven!