Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launchd not loading nginx on startup

I installed NGINX with homebrew then I got info and followed the instructions to load the launchd plist

$ brew info nginx
nginx: stable 1.6.2, devel 1.7.7, HEAD
...
To load nginx:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
Or, if you don't want/need launchctl, you can just run:
    nginx

The problem is nginx doesn't load when I restart.

The plist looks like this:

<?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>homebrew.mxcl.nginx</string>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/opt/nginx/bin/nginx</string>
        <string>-g</string>
        <string>daemon off;</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/usr/local</string>
  </dict>
</plist>                                                                                                                                                                                                                                                
like image 465
jwerre Avatar asked Dec 14 '22 17:12

jwerre


2 Answers

This is what worked for me:

sudo cp /usr/local/opt/nginx/*.plist /Library/LaunchDaemons
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist

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.

Read more here: http://derickbailey.com/2014/12/27/how-to-start-nginx-on-port-80-at-mac-osx-boot-up-log-in/

like image 62
jwerre Avatar answered Jan 05 '23 19:01

jwerre


I stumbled upon your question because I was having the same issue. Three things helped me make it work:

  • change ownership of plist file to root:wheel (sudo chown root:wheel /usr/local/opt/nginx/*.plist)
  • create the symlink in /Library/LaunchAgents instead of ~/Library/LaunchAgents, because you probably run nginx on port 80, which requires root privileges.
  • Remove the <string>-g</string> and <string>daemon off;</string> lines from the plist (before loading it with sudo launchctl load /Library/LaunchAgents/homebrew.mxcl.nginx.plist)

I'm not sure why those two lines are invalid, but I found out by trying to execute the /usr/local/opt/nginx/bin/nginx with -g daemon off; added to it, it also failed.

like image 38
schmkr Avatar answered Jan 05 '23 18:01

schmkr