22

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 02:22

8 Answers8

27

To start nginx on boot: sudo systemctl enable nginx (doesn't start it immediately)

To start nginx: sudo systemctl start nginx

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
Ben Butterworth
  • 502
  • 5
  • 12
13

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

user222758
  • 103
  • 5
10

/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.

3dinfluence
  • 12,409
  • 2
  • 27
  • 41
  • 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 kill `cat /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.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
1

My nginx is a self-compiled version provided with Passenger. No init scripts were included.

Nginx provides a systemd service file:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

I had to edit the paths and signals to fit my installation. For a CentOS system, like mine, the file path should be /etc/systemd/system/nginx.service.

Christopher H
  • 338
  • 2
  • 16
Jussi Hirvi
  • 151
  • 4
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.

hdanniel
  • 4,253
  • 22
  • 25
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

S.R. Pouyet
  • 111
  • 1
  • 2
  • Welcome to Server Fault! While this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – freiheit Dec 15 '12 at 22:45