Register now and start sharing your code snippets.
-->

nginx startup script for Debian

Shell Script (Bash) posted 8 months ago by christian

   1  sudo vim /etc/init.d/nginx

Paste in the following (remember to run ‘set :paste’ in VIM when pasting):

   1  #! /bin/sh
   2  ##
   3  # nginx start script
   4  ##
   5  
   6  PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
   7  DAEMON=/usr/local/sbin/nginx
   8  NAME=nginx
   9  DESC=nginx
  10  
  11  if [ ! -x $DAEMON ]
  12  then
  13     echo "Couldn't find $DAEMON. Please set path to DAEMON."
  14     exit 0
  15  fi
  16  
  17  
  18  # Include nginx defaults if available
  19  if [ -f /etc/default/nginx ] ; then
  20  	. /etc/default/nginx
  21  fi
  22  
  23  set -e
  24  
  25  case "$1" in
  26    start)
  27  	echo -n "Starting $DESC: "
  28  	start-stop-daemon --start --pidfile /var/run/$NAME.pid \
  29  		--exec $DAEMON -- $DAEMON_OPTS
  30  	echo "$NAME."
  31  	;;
  32    stop)
  33  	echo -n "Stopping $DESC: "
  34  	start-stop-daemon --stop --pidfile /var/run/$NAME.pid \
  35  		--exec $DAEMON
  36  	echo "$NAME."
  37  	;;
  38    restart|force-reload)
  39  	echo -n "Restarting $DESC: "
  40  	start-stop-daemon --stop --pidfile \
  41  		/var/run/$NAME.pid --exec $DAEMON
  42  	sleep 1
  43  	start-stop-daemon --start --pidfile \
  44  		/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
  45  	echo "$NAME."
  46  	;;
  47    reload)
  48        echo -n "Reloading $DESC configuration: "
  49        start-stop-daemon --stop --signal HUP --pidfile /var/run/$NAME.pid \
  50            --exec $DAEMON
  51        echo "$NAME."
  52        ;;
  53    *)
  54  	N=/etc/init.d/$NAME
  55  	echo "Usage: $N {start|stop|restart|force-reload}" >&2
  56  	exit 1
  57  	;;
  58  esac
  59  
  60  exit 0

Now make the script executable with this command:

   1  sudo chmod 755 /etc/init.d/nginx

Lastly, run this command to make the script run when the server starts and stops:

   1  sudo /usr/sbin/update-rc.d -f nginx defaults

Tagged nginx, start, script, linux, debian