Search code examples
macosnginxhomebrew

Nginx not started, homebrew says it is


brew services says nginx is started..

MacBook-Pro-van-Youri:Homebrew youri$ brew services start nginx
Service `nginx` already started, use `brew services restart nginx` to restart.

Same for launchctl

MacBook-Pro-van-Youri:Homebrew youri$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist 
/Users/youri/Library/LaunchAgents/homebrew.mxcl.nginx.plist: service already loaded

My homebrew.mxcl.nginx.plist

<?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>

brew services list says the following:

MacBook-Pro-van-Youri:LaunchAgents youri$ brew services list
Name    Status  User  Plist
mariadb started youri /Users/youri/Library/LaunchAgents/homebrew.mxcl.mariadb.plist
nginx   error   youri /Users/youri/Library/LaunchAgents/homebrew.mxcl.nginx.plist
php71   started youri /Users/youri/Library/LaunchAgents/homebrew.mxcl.php71.plist

The syntax is oke:

MacBook-Pro-van-Youri:LaunchAgents youri$ plutil -lint homebrew.mxcl.nginx.plist 
homebrew.mxcl.nginx.plist: OK

When i run sudo nginx i can access my website


Solution

  • Because nginx is going to start at port 80, it needs to be root. LaunchAgents are run as non-root user when that user logs in. LaunchDaemons are loaded at boot as root user.

    launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist
    

    Move the homebrew.mxcl.nginx.plist

    sudo mv ~/Library/LaunchAgents/homebrew.mxcl.nginx.plist /Library/LaunchDaemons/
    

    Load the plist from the LaunchDaemons folder

    sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
    

    Now, sudo brew services list shows a running nginx process

    Name    Status  User  Plist
    nginx   started root  /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
    

    Running brew services list without root will result in an error status, because you need to be root to read the status.