How to start nginx on port 80 at OS X login

bootlaunchctlnginxosx-snow-leopardpermissions

I installed Nginx using homebrew and after completing the installation the following message was displayed:

In the interest of allowing you to run `nginx` without `sudo`, the default
port is set to localhost:8080.

If you want to host pages on your local machine to the public, you should
change that to localhost:80, and run `sudo nginx`. You'll need to turn off
any other web servers running port 80, of course.

You can start nginx automatically on login running as your user with:
  mkdir -p ~/Library/LaunchAgents
  cp #{prefix}/org.nginx.nginx.plist ~/Library/LaunchAgents/
  launchctl load -w ~/Library/LaunchAgents/org.nginx.nginx.plist

Though note that if running as your user, the launch agent will fail if you
try to use a port below 1024 (such as http's default of 80.)

But I want Nginx, on port 80, running at login and I don't want to have to open terminal and type in sudo nginx to do it. I want it to load from a plist file like Redis and PostgreSQL do.

I moved the plist to /Library/LaunchAgents/ from the user folder equivalent and changed its ownership, also tried setting the user directive in the nginx.conf file and still the same error message in Console.app:

nginx: [emerg] bind() to 0.0.0.0:80 failed (13: Permission denied)

(along with another message telling me that since nginx was being run without super-user privileges, the user directive was being ignored)

Best Answer

I found an easier approach was to create add plist file in /Library/LaunchDaemons/

sudo vi /Library/LaunchDaemons/org.nginx.nginx.plist

Or, if you want it to launch on login, you can put it in ~/Library/LaunchAgents/, the same plist file. This will allow you to access the launchd launchctl command from your username without the need to call sudo.

and insert the following (be sure to update the path to your nginx install, and the username to your username):

<?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/Cellar/nginx/1.6.2/bin/nginx</string>
    <key>KeepAlive</key>
    <true/>
    <key>NetworkState</key>
    <true/>
    <key>LaunchOnlyOnce</key>
    <true/>
    <key>UserName</key>
    <string>yourusername</string>
</dict>
</plist>