How can I start nginx via upstart?

Solution 1:

You cannot have multiple stop on directives in an upstart job description for Upstart >= 0.5.

And console owner is probably not what you want (this makes nginx the owner of the system console).

Try:

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

Solution 2:

I've ended up here more than once so I thought I'd provide an updated answer based on my own experience after using the answers here. Thanks especially to @danorton and @orj for their answers.

This script has been tested on Upstart 1.5 running on Ubuntu 12.04 with Nginx 1.0.11 and Passenger 3.0.11. If you're not using Passenger you may need to play around with the post-stop line. Refer to the Upstart cookbook.

In an empty /etc/init/nginx.conf add the following lines (You can remove the comments if you like):

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

I've taken the Upstart script from the Nginx Wiki and tweaked it as a number of lines are not needed, cause confusion or do not work.

You may need to alter env DAEMON and env PID lines depending on where you have installed nginx and are writing the PID. The PID can be configured in nginx.

I tried all forms of expect. Only expect fork seems to work. With Passenger nginx creates 61 forks. Upstart requires 0, 1 or 2. As others have hinted, Upstart will be tracking the wrong PID. I've also removed respawn as it does nothing probably because of the same reason. Some additional pre/post-start script may be able to fix that by grabbing the real PID. I, however, use monit to handle restarts so do not need it.

Do not use daemon off. This is for development only. See http://wiki.nginx.org/CoreModule#daemon

References:

  • http://upstart.ubuntu.com/cookbook/#expect-fork
  • http://wiki.nginx.org/NginxCommandLine#Stopping_or_Restarting_Nginx

Solution 3:

You can’t. At least not properly, anyway.

Nginx does not spawn its daemon in one of the two ways that upstart requires, either via “expect fork” or “expect daemon”, so upstart is unable to track the master nginx process. There are some hacks, but they have their own problems.

If you’re okay with the fact that upstart can’t keep track of the master process and kill it on shutdown, this will work:

start on local-filesystems \
  and (net-device-added INTERFACE=lo) \
  and (runlevel [12345])
stop on runlevel [06]

env DAEMON=/usr/sbin/nginx

respawn
respawn limit 10 5

expect daemon

pre-start script
  $DAEMON -t
end script

$DAEMON

Solution 4:

There is an Upstart config file example in the NGINX Wiki.

You may need to adjust the path to the nginx binary in the config file.

This config file is working fine for me with Ubuntu 10.04 and nginx 1.0.5.

I also installed an nginx symlink in /etc/init.d pointing at /lib/init/upstart-job so I could use the standard service command to start and stop nginx.

Note: If you install Phusion Passenger with NGINX you might need to add the following stanza to the Upstart config script:

env PID=/opt/nginx/logs/nginx.pid
post-stop script
    start-stop-daemon --stop --pidfile $PID --name nginx --exec $DAEMON --signal TERM
end script

I found this necessary on my Ubuntu config. Otherwise when I issued initctl stop nginx or service nginx stop nginx didn't actually stop. I also noticed that Upstart thought the nginx process had a PID that was actually the PID of one of the Passenger processes. So clearly NGINX/Passenger is confusing Upstart a little.