如何通过 upstart 启动 nginx?

chi*_*gsy 9 ubuntu upstart ubuntu-10.04

背景:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=10.04
DISTRIB_CODENAME=lucid
DISTRIB_DESCRIPTION="Ubuntu 10.04 LTS"
Run Code Online (Sandbox Code Playgroud)

我已经构建了 nginx,我想使用 upstart 来启动它:

来自该站点的 nginx 新贵脚本:

description "nginx http daemon"

start on runlevel 2

stop on runlevel 0
stop on runlevel 1
stop on runlevel 6

console owner

exec /usr/sbin/nginx -c /etc/nginx/nginx.conf  -g "daemon off;"

respawn
Run Code Online (Sandbox Code Playgroud)

当我尝试使用 initctl 运行它时,我得到了“未知工作”,我刚刚了解到这显然意味着存在错误,(用“错误”来描述错误有什么问题?)

有人可以指出我正确的方向吗?我已经阅读了文档,就像它一样,对于 SysV init 替代品来说,它似乎有点稀疏......但是只要将这个工作添加到列表中,运行它,然后继续我的生活。 .. 有小费吗?

编辑:initctl 版本 init(新贵 0.6.5)

Phi*_*ilT 16

我不止一次来到这里,所以我想我会在使用这里的答案后根据我自己的经验提供更新的答案。特别感谢@danorton 和@orj 的回答。

此脚本已在 Ubuntu 12.04 上运行的 Upstart 1.5 和 Nginx 1.0.11 和Passenger 3.0.11 上进行了测试。如果您不使用乘客,您可能需要玩弄post-stop线路。请参阅 Upstart 食谱。

在空白处/etc/init/nginx.conf添加以下几行(如果您愿意,可以删除注释):

description "nginx http daemon"

start on (filesystem and net-device-up IFACE=lo)
stop on runlevel [!2345]

env DAEMON=/usr/local/nginx/sbin/nginx
env PIDFILE=/var/run/nginx.pid

# Needed to allow Nginx to start, however, the wrong PID will be tracked
expect fork

# Test the nginx configuration (Upstart will not proceed if this fails)
pre-start exec $DAEMON -t

# Ensure nginx is shutdown gracefully
# Upstart will be tracking the wrong PID so the following is needed to stop nginx
post-stop exec start-stop-daemon --stop --pidfile $PIDFILE --name nginx --exec $DAEMON --signal QUIT

# Start Nginx
exec $DAEMON
Run Code Online (Sandbox Code Playgroud)

我从 Nginx Wiki 中获取了 Upstart 脚本并对其进行了调整,因为不需要多行,导致混淆或不起作用。

您可能需要根据安装 nginx 和写入 PID 的位置更改env DAEMONenv PID行。可以在 nginx 中配置 PID。

我尝试了所有形式的expect. 只有expect fork似乎工作。使用Passenger nginx 创建61 个分叉。Upstart 需要 0、1 或 2。正如其他人所暗示的那样,Upstart 将跟踪错误的 PID。respawn由于同样的原因,我也删除了它,因为它什么也不做。一些额外的启动前/启动后脚本可能能够通过获取真实的 PID 来解决这个问题。但是,我使用 monit 来处理重启,所以不需要它。

不要使用daemon off. 这仅用于开发。见http://wiki.nginx.org/CoreModule#daemon

参考:


Jac*_*zny 3

stop onUpstart >= 0.5 的 Upstart 职位描述中不能有多个指令。

并且console owner可能不是您想要的(这使得 nginx 成为系统控制台的所有者)。

尝试:

description "nginx http daemon"
start on runlevel 2
stop on runlevel [016]
console output
exec /usr/sbin/nginx -c /etc/nginx/nginx.conf  -g "daemon off;"
respawn
Run Code Online (Sandbox Code Playgroud)