25

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

1
  • 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, 2013 at 2:22

8 Answers 8

33

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

To start nginx: sudo systemctl start nginx

1
  • Ah, the modern answer (modern as of 2019, that is). Welcome to the site ButterHub :)
    – kubanczyk
    Dec 4, 2019 at 20:23
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

1
  • Thanks for the direct instructions. That's what StackExchange is for.
    – Ryan
    Oct 7, 2015 at 18:33
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.

3
  • 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. Sep 28, 2009 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 Sep 28, 2009 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. Sep 28, 2009 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.

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

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.

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

1
  • 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, 2012 at 22:45

You must log in to answer this question.