Couldn’t find a working startup script for nginx on Ubuntu, so i quickly cobbled one together. This is principally based on the Lighttpd startup script and supports Start, Stop, Reload and friends. Works reliably on my Ubuntu Edge install, but should work on most other distros as well. Drop me a note if it worked on your distro, so i can updated the comments.

If you custom-configured nginx, make sure to update the DAEMON (the binary) and CONFIG_FILE variables to reflect your settings.

#!/bin/sh

#This is a start script for nginx. Tested on Unbuntu Edge. 
#Should work on Ubuntu, Debian and probably a few other Linux distros.
#Change DAEMON and CONFIG_FILE  if neccessary

PATH=/sbin:/bin:/usr/sbin:/usr/bin


#Location of nginx binary. Change path as neccessary
DAEMON=/usr/local/nginx/sbin/nginx
#Location of configuration file. Change path as neccessary
CONFIG_FILE =/usr/local/nginx/conf/nginx.conf


DAEMON_OPTS="-c $CONFIG_FILE"
NAME=nginx
DESC="web server"
PIDFILE=/var/run/$NAME.pid 
SCRIPTNAME=/etc/init.d/$NAME


#only run if binary can be found
test -x $DAEMON || exit 0

set -e

#import init-functions
. /lib/lsb/init-functions

case "$1" in
start)
log_daemon_msg "Starting $DESC" $NAME
if ! start-stop-daemon --start --quiet\
    --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS ; then
    log_end_msg 1
    else
    log_end_msg 0
fi
;;
stop)
log_daemon_msg "Stopping $DESC" $NAME
if start-stop-daemon --quiet --stop --oknodo --retry 30\
    --pidfile $PIDFILE --exec $DAEMON; then
    rm -f $PIDFILE
    log_end_msg 0
    else
    log_end_msg 1
fi
;;
reload)
log_daemon_msg "Reloading $DESC configuration" $NAME
if start-stop-daemon --stop --signal 2 --oknodo --retry 30\
    --quiet --pidfile $PIDFILE --exec $DAEMON; then
    if start-stop-daemon --start --quiet  \
        --pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS ; then
        log_end_msg 0
        else
        log_end_msg 1
    fi
    else
    log_end_msg 1
fi
;;
restart|force-reload)
$0 stop
sleep 1
$0 start
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
exit 1
;;
esac

exit 0

Download the script

3 Responses to “Boot Script for Nginx on Ubuntu, Debian etc.”

  1. fritzie Says:
    thanks for the script. i was wondering if anyone knows how to get the ssl passphrase to work with this. or should it be set somehow in the config file ?
  2. angelpopov Says:
    the space should be removed in row 'CONFIG_FILE =/usr/local/nginx/conf/nginx.conf' the script works with 'CONFIG_FILE=/usr/local/nginx/conf/nginx.conf
  3. louis Says:
    thanks for the script. nicely done. works on Ubuntu 7.10--but you knew that already. ;-) (ditto on the space between CONFIG_FILE and the '=', btw) in case fritzie is reading: the easiest way to bypass an ssl passphrase is to not have the key encrypted (so that the server doesn't need a person to enter it). openldap needs it like this, and i've set up apache servers using this too. just keep the key in a private dir w/ no access except to the daemon.

Leave a Reply