Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I was able to install and run nginx, and passenger but i'm not able to have nginx come up whenever i reboot my server. To start the nginx process i just type in sudo /opt/nginx/sbin/nginx. From my understanding anything i put in /etc/init.d with ubuntu hardy will be execute. Does anyone know the command that will mimic this command line call?

sudo /opt/nginx/sbin/nginx

share|improve this question
if you install nginx via apt-get install nginx, then it "automatically" starts at boot time (but doesn't auto start itself initially, odd...) – rogerdpack Jun 14 '13 at 2:22

/etc/init.d is just the location for the start up scripts to live. But having a script there doesn't do anything automatically.

The init system uses the symbolic links in the /etc/rc#.d directories to the scripts in the /etc/init.d folder. The name of the symbolic link needs to start with an S to run the script with the start option and K to run the stop option followed by a priority number and then the name of the script.

See the following for more info

Alternatively you can put your command you want to run into the /etc/rc.local script which is run after the system boots and finishes executing all the scripts in the /etc/rc2.d/ folder.

share|improve this answer
Btw the /etc/init.d/README file instructs you to use the update-rc.d command to create symbolic links in the /etc/rc?.d so the above answer just lets you know what that command is doing. And you can run man update-rc.d for more information on the update-rc.d command. – 3dinfluence Sep 28 '09 at 18:06
heh looks like the update-rc.d command manpage suggests that you should use sysv-rc-conf or bum to manage the init scripts being run at various run-levels. Guess I'll file a bug on the /etc/init.d/README – 3dinfluence Sep 28 '09 at 18:12
Looks like it's already been corrected in the next Ubuntu release. The update-rc.d man page no longer suggests sysv-rc-conf or bum. – 3dinfluence Sep 28 '09 at 18:24

Thanks for the info, if someone wants step by step instructions. Go to /etc/init.d and run sudo nano, then paste in this code:

# this script starts the nginx process attached to passenger
sudo /opt/nginx/sbin/nginx

save and exit. Make the file executable by typing sudo chmod +x /etc/init.d/ You can test to see if the script works by typing sudo /etc/init.d/ this will run all the code in the script. Verify that it launches nginx before continuing.

Then run sudo update-rc.d defaults while still in the /etc/init.d directory. Once all of this is in place, reboot your server and ngnix should now be automatically spawned on startup

share|improve this answer
Thanks for the direct instructions. That's what StackExchange is for. – Ryan Oct 7 '15 at 18:33

I am assuming you have installed nginx

If you have nginx running then stop the process using:

  • sudo killcat /usr/local/nginx/logs/

Init script

The script shown below is from an Ubuntu 10.04 install and has been adapted to take into account our custom install of nginx. Please 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/$ \
                --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$ \
                --exec $DAEMON
        echo "$NAME."
        echo -n "Restarting $DESC: "
        start-stop-daemon --stop --quiet --pidfile \
                /usr/local/nginx/logs/$ --exec $DAEMON
        sleep 1
        start-stop-daemon --start --quiet --pidfile \
                /usr/local/nginx/logs/$ --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$ \
          --exec $DAEMON
      echo "$NAME."
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1

exit 0

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:

sudo /usr/sbin/update-rc.d -f nginx defaults
 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

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.

share|improve this answer

Check this link:

They provide a nginx init.d script to run in ubuntu.

share|improve this answer

I use this script to start a passenger-backed nginx:

share|improve this answer

For reference: I just followed the instructions and script for Maverick provided by Linode at

share|improve this answer
Welcome to Server Fault! While this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. – freiheit Dec 15 '12 at 22:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.