如何在 OS X 登录时在端口 80 上启动 nginx?

Bry*_*son 24 boot osx-snow-leopard permissions nginx launchctl

我使用自制软件安装了 Nginx ,完成安装后显示以下消息:

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.)
Run Code Online (Sandbox Code Playgroud)

但我希望Nginx 在端口 80 上在登录时运行,我不想打开终端并输入sudo nginx来执行此操作。我希望它从像 Redis 和 PostgreSQL 这样的 plist 文件加载。

我将 plist/Library/LaunchAgents/从等效的用户文件夹移到并更改了其所有权,还尝试usernginx.conf文件中设置指令,但在 Console.app 中仍然出现相同的错误消息:

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

(还有另一条消息告诉我,由于nginx在没有超级用户权限的情况下运行,该user指令被忽略)

小智 12

我发现一种更简单的方法是在 /Library/LaunchDaemons/ 中创建添加 plist 文件

sudo vi /Library/LaunchDaemons/org.nginx.nginx.plist
Run Code Online (Sandbox Code Playgroud)

或者,如果您希望它在登录时启动,您可以将它放在 ~/Library/LaunchAgents/,同一个 plist 文件中。这将允许您从您的用户名访问 launchd launchctl 命令,而无需调用 sudo。

并插入以下内容(确保更新您的 nginx 安装路径,并将用户名更新为您的用户名):

<?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>
Run Code Online (Sandbox Code Playgroud)

  • 其实我只是拿了原来的`homebrew.mxcl.nginx.plist`,把`UserName`改成`root`,然后改成`sudo chown root`,然后运行`sudo launchctl load -w`就可以了。 (3认同)

小智 9

我来到这里是因为我遇到了同样的问题。我的解决方案与上面的 Rich 的解决方案类似,只是我使用了 Homebrew nginx 启动脚本:

sudo cp /usr/local/opt/nginx/homebrew.mxcl.nginx.plist /Library/LaunchDaemons/
Run Code Online (Sandbox Code Playgroud)

作为参考,这是 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>UserName</key>
    <string>root</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/sbin/nginx</string>
    </array>
    <key>WorkingDirectory</key>
    <string>/usr/local</string>
  </dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

我在 $HOME/.profile 中添加了 2 个别名,以便更轻松地启动和停止 nginx。

# Nginx needs to bind to port 80 so must run as /Library/LaunchDaemon with sudo
alias start-nginx='sudo launchctl load /Library/LaunchDaemons/homebrew.mxcl.nginx.plist'
alias stop-nginx='sudo launchctl unload /Library/LaunchDaemons/homebrew.mxcl.nginx.plist'
Run Code Online (Sandbox Code Playgroud)

我的问题是由于某种原因 nginx 最初没有正确启动。我只需要运行 stop-nginx 来卸载它,然后用 start-nginx 重新启动它。

  • 替代方案:`lunchy restart nginx`,参见 https://github.com/mperham/lunchy (4认同)