Ubuntu Gutsy - 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.


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 placed the nginx binary in a directory other than /usr/local/sbin/ then you may need to adjust the script shown below.


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 Ubuntu Gutsy '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

# 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


test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
        . /etc/default/nginx

set -e

case "$1" in
        echo -n "Starting $DESC: "
        start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
                --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
                --exec $DAEMON
        echo "$NAME."
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile \
                /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
          echo -n "Reloading $DESC configuration: "
          start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
              --exec $DAEMON 
          echo "$NAME."
            echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
            exit 1   

    exit 0

You can also download the script contents from here.

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.


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


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


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.


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


Article Comments:

James commented Tue Dec 04 04:13:01 UTC 2007:

The header "Exceute" should be "Execute" (same typo in similar articles).

John Griffiths commented Wed Dec 05 14:38:27 UTC 2007:

Excellent, hooked it up on my Gutsy box, all works fine

Just got to disable Apache2.2 from starting on reboot and hook mongrel_cluster to it

PickledOnion commented Fri Dec 07 13:38:01 UTC 2007:


Thanks for the nudge. I write the articles using Markdown and my spoll chicker doesn't catch anything between hashes.


Michael commented Wed Mar 05 06:02:57 UTC 2008:

These articles are absolutely awesome!!!

Tate commented Mon Mar 10 10:41:07 UTC 2008:

Great init script. Thanks!

Patrick D commented Mon Jan 19 04:04:44 UTC 2009:

Good job! Thanks man.

berat commented Mon Apr 20 06:59:34 UTC 2009:

i took welcome to nginx.But i cannot join forbidden site.My country banned some web site like youtube.I read full article.

Want to comment?

(not made public)


(use plain text or Markdown syntax)