Debian Lenny - adding an Nginx init script

If you decided to install Nginx via source (see the previous article) you would have the latest and greatest version.

However, one disadvantage of installing from source is that init scripts are not created. No problem - let's go ahead and create one for easy control of Nginx and to ensure that it restarts on a reboot.


Assumption

I am assuming you have followed the previous article and installed Nginx from source.

If you have used other options or have placed the nginx binary in a directory other than '/usr/local/sbin/' then you will need to adjust the script shown below to match your installation.

Stop

If you have nginx running then stop the process using:

sudo kill `cat /usr/local/nginx/logs/nginx.pid`

Init script

The script I use below is from a Debian Lenny '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

. /lib/lsb/init-functions

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
        --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile \
        /usr/local/nginx/logs/$NAME.pid --exec $DAEMON || true
    sleep 1
    start-stop-daemon --start --quiet --pidfile \
        /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
          --exec $DAEMON || true
      echo "$NAME."
      ;;
  status)
      status_of_proc -p /usr/local/nginx/logs/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
      ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
    exit 1
    ;;
esac

exit 0

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 and pid files are located so nginx can be started correctly.

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

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

Start on Boot

Now that we have the base script prepared, we need to add it to the default run levels so that it will automatically start on boot:

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.

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 also in having it automatically start on a reboot.

In the next article we will learn how to mirror the Debian style layout for our nginx configuration files.

Ben B

Article Comments:

DH commented Wed Oct 27 18:18:04 UTC 2010:

Anyone having problems with the init.d/nginx script? I execute it and all it does it show a blank line .....

RH commented Tue Nov 09 19:20:40 UTC 2010:

Yes, same thing happens to me. Followed allot of examples on the net tho :(

acesabre commented Fri Nov 19 14:32:22 UTC 2010:

this script gets probably a default path for nginx when you install it from repository, when you install nginx from source you have to change daemon path to " /usr/local/nginx/sbin/nginx " otherwise script will not work and give you a blank line

subsume commented Tue Nov 23 20:28:32 UTC 2010:

garbage.

bob commented Tue Mar 29 18:08:36 UTC 2011:

The problem must have been fixed because it worked for me. It's nice to see such a well presented set of instructions. Thank you.

j03k64 commented Fri Apr 29 16:49:48 UTC 2011:

I have followed all previous instructions (installing nginx 1.0.0),however, when I go to save the start/stop script to /etc/init.d/nginx it throws an error stating that there is no such directory. When I checked for the directory using MC, there is indeed a directory in place. How do I correct this issue?

Pakdoz commented Sat Aug 11 02:27:43 UTC 2012:

Works like a charm under Debian Squeeze. Thanks!

Thomas commented Fri Apr 26 07:20:16 UTC 2013:

If you install from source with the defaults the daemon path in my case must be:

DAEMON=/usr/local/nginx/sbin/nginx

Thomas commented Fri Apr 26 07:20:33 UTC 2013:

If you install from source with the defaults the daemon path in my case must be:

DAEMON=/usr/local/nginx/sbin/nginx

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)