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

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

我使用以下方式安装了 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.)

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

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

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

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

答案1

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

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

或者,如果您希望它在登录时启动,您可以将其放在同一个 plist 文件 ~/Library/LaunchAgents/ 中。这将允许您从您的用户名访问 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>

答案2

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

sudo cp /usr/local/opt/nginx/homebrew.mxcl.nginx.plist /Library/LaunchDaemons/

作为参考,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>

我已将 2 个别名添加到我的 $HOME/.profile,以便更轻松地启动和停止 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'

我的问题是,出于某种原因,nginx 最初无法正常启动。我只需运行 stop-nginx 来卸载它,然后使用 start-nginx 重新启动它。

答案3

不是一个精确的回答你的问题,但可能足够接近:你可以使用数据包过滤器pf(在 OS X 10.9 上测试)将端口 80 重定向到端口 8080。将以下行添加到你的/etc/pf.conf(它应该位于过滤规则之上):

rdr pass on lo0 inet proto tcp from any to any port http -> localhost port 8080

可以找到此行(大部分)元素的解释这里

有关详细信息,请参阅For more information, seehttps://superuser.com/a/521803http://www.openbsd.org/faq/pf/index.html,尽管后者似乎描述了的较新版本pf,其中给定的行将以不同的方式写出。

答案4

我可以想到两种解释:

  1. 您的文件中有一些类似这样的行.plist

    <key>Username</key>
    <string>some_user</string>
    

    这告诉 launchd 使用指定用户的权限而不是 root 来启动 nginx。

    如果希望 nginx 绑定到 80 端口,则应在 nginx 的 用户指令,而不是在 .plist 中。nginx 将删除工作进程的权限,尽管主进程仍然是 root 进程。

  2. 您有一个 nginx.plist位于~/Library/LaunchAgents/(请注意前导~) 中,而不是/Library/LaunchAgents

    请注意,可以激活和停用 .plists,因此您的系统上可能有两个 nginx .plists,但其中只有一个是活动的。

相关内容