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


24

我使用自制软件安装了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来执行此操作。我希望它从Redis和PostgreSQL这样的plist文件加载。

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

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

(以及另一条消息告诉我,由于nginxuser命令是在没有超级用户特权的情况下运行的,因此该指令被忽略了)


将其移动到LaunchDaemons文件夹中的任何一个时,请确保您的plist文件具有“ root:wheel”所有权。否则,launchctl不会因为“可疑所有权”而运行它。
risyasin 2014年

Answers:


12

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

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

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

这是一个有用的答案,但从技术上讲,它是在引导时启动nginx的,而不是登录时启动的。如果@Bryson确实只希望在登录某个GUI帐户后才运行它,那么这不是正确的答案。但我怀疑他真的想它一旦机器已经启动运行,即使GUI等待在登录窗口,而不是等到他居然在日志中。
斯皮夫

我将其添加到~/Library/LaunchAgents/控制台中,并出现以下错误:3/30/13 9:39:24.047 AM com.apple.launchd.peruser.501[884]: (nginx) Unknown key for boolean: NetworkState 3/30/13 9:39:24.047 AM com.apple.launchd.peruser.501[884]: (nginx) Ignored this key: UserName 3/30/13 9:39:24.047 AM com.apple.launchd.peruser.501[884]: (nginx) Ignored this key: UserName。我将Program字符串更改为/usr/local/Cellar/nginx/1.2.7/sbin/nginx是否重要。
hobbes3 2013年

3
实际上,我只是将原始图像带到,然后homebrew.mxcl.nginx.plist更改UserNameroot,然后sudo chown root运行就sudo launchctl load -w可以了。
hobbes3 2013年

我可以检查一下吗-您在〜/ Library / LaunchAgents /中有一个.plist(因此仅适用于一个用户),但它以root身份启动?我从未见过文档中提到过这种可能性,而当我在Mavericks上尝试时,它失败了。
Pitarou

@ hobbes3 @ rich-kroll大约一年后,但KeepAlive可以为true,也可以设置为包含NetworkState的字典。因此,上面的示例应该说<key>KeepAlive</key><dict><key>NetworkState</key><true/></dict>(请注意,我用true包含NetworkState的字典替换了KeepAlive的)。来源:developer.apple.com/library/mac/documentation/Darwin/Reference/...
getWeberForStackExchange

9

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

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>

我在$ 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'

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


4
替代方案:lunchy restart nginx,请参见github.com/mperham/lunchy
Andrei

1

并非您问题的确切答案,但可能足够接近:您可以使用packetfilter 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

有关此行(大部分)元素的说明,请参见此处

有关更多信息,请参见/superuser//a/521803http://www.openbsd.org/faq/pf/index.html,尽管后者似乎描述了的较新版本pf,其中行将以不同的方式编写。


0

我可以想到两种解释:

  1. 您的.plist文件中有几行是这样的:

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

    这告诉launchd以指定用户而不是root的特权启动nginx。

    如果您希望nginx绑定到端口80,则应在nginx的用户指令中而不是.plist中设置用户名 。尽管主进程仍将是root用户,但nginx随后将放弃工作进程的特权。

  2. .plist~/Library/LaunchAgents/(请注意开头)中有一个nginx ~而不是/Library/LaunchAgents

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


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.