Nginx 没有启动,自制程序说它是

Nginx not started, homebrew says it is

brew 服务说 nginx 已启动..

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

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

我的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 服务列表如下:

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

语法没问题:

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

当我 运行 sudo nginx 我可以访问我的网站时

因为nginx要在80端口启动,所以需要root。当用户登录时,LaunchAgents 运行 作为非 root 用户。LaunchDaemons 在启动时作为 root 用户加载。

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

移动homebrew.mxcl.nginx.plist

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

从 LaunchDaemons 文件夹加载 plist

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

现在,sudo brew services list 显示一个 运行ning nginx 进程

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

运行 brew services list 没有root会导致错误状态,因为你需要是root才能读取状态。

如前所述,发生这种情况是因为端口 80 已保留,需要 root。

更简单的方法是 运行 brew servicessudo

sudo brew services start nginx