ScaleScaleScaleScale

Tips / Nginx


Nginx init script for CentOS/RHEL

This is the nginx init script we use at nginxtips.com, it was tested under CentOS/RHEL 6.x, you may need to modify a few variables depending on how you configured Nginx while the installation from the tar.gz. The variables to set are often this three:

nginx=${NGINX-/usr/sbin/nginx}
conffile=${CONFFILE-/etc/nginx/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}

If you installed Nginx from an RPM file from nginx.org repo, you don’t need this as it is already installed at /etc/init.d/nginx.

Nginx init script:

#!/bin/sh
#
# nginx Startup script for nginx
#
# chkconfig: - 85 15
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# description: nginx is an HTTP and reverse proxy server
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop nginx
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/nginx ]; then
. /etc/sysconfig/nginx
fi

prog=nginx
nginx=${NGINX-/usr/sbin/nginx}
conffile=${CONFFILE-/etc/nginx/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/var/run/nginx.pid}
SLEEPMSEC=100000
RETVAL=0

start() {
echo -n $"Starting $prog: "

daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}

stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} ${prog}
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
echo -n $"Reloading $prog: "
killproc -p ${pidfile} ${prog} -HUP
RETVAL=$?
echo
}

upgrade() {
oldbinpidfile=${pidfile}.oldbin

configtest -q || return 6
echo -n $"Staring new master $prog: "
killproc -p ${pidfile} ${prog} -USR2
RETVAL=$?
echo
/bin/usleep $SLEEPMSEC
if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
echo -n $"Graceful shutdown of old $prog: "
killproc -p ${oldbinpidfile} ${prog} -QUIT
RETVAL=$?
echo
else
echo $"Upgrade failed!"
return 1
fi
}

configtest() {
if [ "$#" -ne 0 ] ; then
case "$1" in
-q)
FLAG=$1
;;
*)
;;
esac
shift
fi
${nginx} -t -c ${conffile} $FLAG
RETVAL=$?
return $RETVAL
}

rh_status() {
status -p ${pidfile} ${nginx}
}

# See how we were called.
case "$1" in
start)
rh_status >/dev/null 2>&1 && exit 0
start
;;
stop)
stop
;;
status)
rh_status
RETVAL=$?
;;
restart)
configtest -q || exit $RETVAL
stop
start
;;
upgrade)
upgrade
;;
condrestart|try-restart)
if rh_status >/dev/null 2>&1; then
stop
start
fi
;;
force-reload|reload)
reload
;;
configtest)
configtest
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
RETVAL=2
esac

exit $RETVAL

Set propper permissions to the nginx init script:

chmod 755 /etc/init.d/nginx -v

Add the script to the default runlevels:

chkconfig nginx on

Final step, test this out by running:

/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart

Enjoy 😀

Popular search terms:

  • ${nginx} -t -c ${conffile} $FLAG
  • /var/www nginx rhel
  • centos nginx installatino script
  • linux /etc/init/gninx conf
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Ronnie Flathers

    Great post! Really appreciate it. But one thing: your script is getting URL encoded. maybe you can post a link to a pastebin or something?

    • admin

      Ronnie,

      This has been fixed, init script should work fine now.
      Please let us know if you find any additional issues.

      Regards.