10

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

  • 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
10

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

#!/bin/bash
# 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/nginx-passenger.sh. You can test to see if the script works by typing sudo /etc/init.d/nginx-passenger.sh this will run all the code in the script. Verify that it launches nginx before continuing.

Then run sudo update-rc.d nginx-passenger.sh 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

  • Thanks for the direct instructions. That's what StackExchange is for. – Ryan Oct 7 '15 at 18:33
9

/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
/etc/init.d/README
/etc/rc1.d/README
/etc/rc2.d/README

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.

  • 1
    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
2

I am assuming you have installed nginx

If you have nginx running then stop the process using:

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

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

### 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/$NAME.pid \
                --exec $DAEMON -- $DAEMON_OPTS
        echo "$NAME."
        ;;
  stop)
        echo -n "Stopping $DESC: "
        start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
                --exec $DAEMON
        echo "$NAME."
        ;;
  restart|force-reload)
        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."
        ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
          --exec $DAEMON
      echo "$NAME."
      ;;
  *)
        N=/etc/init.d/$NAME
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

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.

2

sudo systemctl enable service_name, e.g. sudo systemctl enable nginx This won't start the service, ensure the service starts on boot. To start it immediately, run sudo system start nginx

  • Ah, the modern answer (modern as of 2019, that is). Welcome to the site ButterHub :) – kubanczyk Dec 4 at 20:23
  • Thanks @kubanczyk :) – ZenBoat 2 days ago
1

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

http://library.linode.com/development/frameworks/ruby/ruby-on-rails/nginx-ubuntu-8.04-hardy

1

Check this link:

https://github.com/JasonGiedymin/nginx-init-ubuntu

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

0

For reference: I just followed the instructions and script for Maverick provided by Linode at http://library.linode.com/frameworks/ruby-on-rails-nginx/ubuntu-10.10-maverick

  • 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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy