Debian Etch - adding an nginx init script
In the previous article we installed nginx from source. However, installing from source does not create an init script.
Let's create such a script so we can easily start, stop and restart nginx and, perhaps more importantly, ensure it automatically starts on a reboot.
Assumption
I am assuming you have installed nginx from source using the options shown in the previous article.
If you have used other options or have place the nginx binary in a directory other than /usr/local/sbin/ then you may need to adjust the script shown below.
Stop
If you have nginx running then stop the process using:
sudo kill `cat /usr/local/nginx/logs/nginx.pid`
Init script
The script shown below is from an Debian Etch 'aptitude' install and has been adapted to take into account our custom install of nginx.
Let's go ahead and create the script:
sudo nano /etc/init.d/nginx
Inside the blank file place the following:
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx
test -x $DAEMON || exit 0
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
set -e
case "$1" in
start)
echo -n "Starting $DESC: "
start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/nginx.pid \
--exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: "
start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/nginx.pid \
--exec $DAEMON
echo "$NAME."
;;
restart|force-reload)
echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --pidfile \
/usr/local/nginx/logs/nginx.pid --exec $DAEMON
sleep 1
start-stop-daemon --start --quiet --pidfile \
/usr/local/nginx/logs/nginx.pid --exec $DAEMON -- $DAEMON_OPTS
echo "$NAME."
;;
reload)
echo -n "Reloading $DESC configuration: "
start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/nginx.pid \
--exec $DAEMON
echo "$NAME."
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|force-reload}" >&2
exit 1
;;
esac
exit 0
You can also download the script from here (as it's a shell script, you may get a warning about it being a 'BIN' file, just save as normal and open in any text editor).
There's not really the space to go into the workings of the script but suffice to say, it defines where the main nginx binary is located so nginx can be started correctly.
It also defines where to find the nginx.pid file so the process and be correctly stopped and restarted.
Execute
As the init file is a shell script, it needs to have executable permissions.
We set them like so:
sudo chmod +x /etc/init.d/nginx
update-rc
Now we have the base script prepared, we need to add it to the default run levels:
sudo /usr/sbin/update-rc.d -f nginx defaults
The output will be similar to this:
Adding system startup for /etc/init.d/nginx ...
/etc/rc0.d/K20nginx -> ../init.d/nginx
/etc/rc1.d/K20nginx -> ../init.d/nginx
/etc/rc6.d/K20nginx -> ../init.d/nginx
/etc/rc2.d/S20nginx -> ../init.d/nginx
/etc/rc3.d/S20nginx -> ../init.d/nginx
/etc/rc4.d/S20nginx -> ../init.d/nginx
/etc/rc5.d/S20nginx -> ../init.d/nginx
Done.
Start, Stop and Restart
Now we can start, stop and restart nginx just as with any other service:
sudo /etc/init.d/nginx start
...
sudo /etc/init.d/nginx stop
...
sudo /etc/init.d/nginx restart
The script will also be called on a reboot so nginx will automatically start.
Summary
Adding a process to the run levels like this saves a lot of frustration and effort, not only in manually starting and stopping the process, but in having it automatically start on a reboot.
PickledOnion.


Article Comments:
Mason commented Sat Dec 15 01:42:46 UTC 2007:
PickledOnion, I love you. I'm not exactly a sysadmin, so I typically ran with whatever version of nginx that was in the repos...
Your tutorials have helped a lot.
Jaime Iniesta commented Fri Feb 08 13:09:25 UTC 2008:
Yes I love you too. :)
It would be interesting to see an article that explains how to start all rails mongrel clusters when restarting the server. I guess mongrel won't wake up by itself if not said so.
PickledOnion commented Fri Feb 08 13:19:23 UTC 2008:
Jamie,
http://articles.slicehost.com/2007/9/24/debian-etch-mongrel-clusters-and-surviving-a-reboot
:)
PickledOnion
weakish commented Fri Mar 07 17:20:27 UTC 2008:
Although slicehost's vps is a bit expensive for me (who only need a host with less resource but cheaper), this article is very clear, helpful and newbie-friendly. With this article, I don't need to start nginx manually. Thanks.
adam.setzler commented Tue Apr 29 22:27:46 UTC 2008:
It should be noted that the latest stable release of nginx will survive a reboot.
Ben Reubenstein commented Fri May 09 03:53:51 UTC 2008:
Another great way to manage what comes up with the system is rcconf:
Then just execute
and put stars next to what you want to start on boot.
arbolurbano commented Sun Aug 31 20:37:16 UTC 2008:
When do we gonna have the "setup nginx with rubyonrails" tutorial?
Anyway... thanx fot this one!
Ken Y. commented Wed May 27 21:32:49 UTC 2009:
Great, article, P.O.! I used the setup described on these two article with Debian Lenny, using apt-get to install Nginx (as you would with Etch), I set up the start-stop-restart script, and it works like a charm!