CentOS - Adding an Nginx Init Script

If you decided to install Nginx via source (see the previous article) you would have the latest and greatest version.

However, one disadvantage of installing from source is that init scripts are not created. No problem, let's go ahead and create one for easy control of Nginx and to ensure it restarts on a reboot.


Assumption

I am assuming you have followed the previous article and installed Nginx from source.

If you have used other options or have placed the Nginx binary in a directory other than /usr/local/sbin/ then you will need to adjust the script shown below to match your installation.

Stop

If you have Nginx running then stop the process using:

sudo kill `cat /usr/local/nginx/logs/nginx.pid`

Init script

The script I use below is from a CentOS 'yum install' and has been adapted to take into account our custom install of Nginx.

Let's go ahead and create the script:

sudo nano /etc/init.d/nginx

Inside the blank file place the following:

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /usr/local/nginx/conf/nginx.conf
# pidfile:     /usr/local/nginx/logs/nginx.pid

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

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

nginx="/usr/local/sbin/nginx"
prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"

lockfile=/var/lock/subsys/nginx

start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    configtest || return $?
    stop
    start
}

reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}

configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}

rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

There's not really the space to go into the workings of the script but suffice to say, it defines where the main Nginx binary and pid files are located so Nginx can be started correctly.

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

Chkconfig

Now we have the base script prepared, we need to add it to the default run levels:

sudo /sbin/chkconfig nginx on

Let's check our work to confirm:

sudo /sbin/chkconfig --list nginx
nginx           0:off   1:off   2:on    3:on    4:on    5:on    6:off

Done.

The script will now be called on a reboot so Nginx will automatically start.

Start, Stop and Restart

Now we can start, stop, restart, and reload Nginx using these commands:

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

You can also check the current status as well as the configuration syntax:

sudo /etc/init.d/nginx status
...
sudo /etc/init.d/nginx configtest

Summary

Adding a process to the run levels like this saves a lot of frustration and effort, not only in manually starting and stopping the process, but it having it automatically start on a reboot.

Article Comments:

Yeah commented Wed Apr 29 22:03:15 UTC 2009:

didn't work on the default install on mine, I had to alter the following line:

nginx="/usr/local/sbin/nginx"

to

nginx="/usr/local/nginx/sbin/nginx"

works for me now. Thanks for the script!

Steve commented Tue May 19 11:32:15 UTC 2009:

Nice one. It works, but when I do /etc/init.d/nginx status, it tells me "nginx dead but subsys locked" even though nginx is running fine.

Bogdan commented Wed Jul 01 11:23:08 UTC 2009:

Great article! 10x

Travis Bell commented Wed Jul 01 16:55:16 UTC 2009:

Used this on 4 or 5 different boxes, thanks for the awesome script guys!

Erkan Tugral commented Fri Mar 05 23:04:31 UTC 2010:

Thank you for this great article. It worked perfectly on Centos 5.4 X86_64 box.

Robin Winslow commented Thu Mar 11 14:20:36 UTC 2010:

Awesome! Works a treat. I have to learn me how to do init scripts one day. Thanks :D

Jihad Aravassery commented Sun Jul 11 15:16:04 UTC 2010:

Thanks for this article. It worked on Fedora 9.

Wonick Seo commented Wed Jul 28 03:44:03 UTC 2010:

nice script!.. thanks from korea

Anton Visser commented Thu Aug 19 18:32:55 UTC 2010:

brilliant! Thanks from Nashville TN

Aladin commented Tue Nov 23 23:36:55 UTC 2010:

Thanks.that help me a lot.

Stas Po commented Tue Dec 21 23:00:29 UTC 2010:

Thank you!

Hakan commented Fri Dec 24 08:52:32 UTC 2010:

10min nginx automatically in the status of a check and, if it is off to a script that will start again.

AR commented Mon Dec 27 08:59:37 UTC 2010:

Very helpful. Thank you!

Fernando commented Mon Mar 14 16:24:43 UTC 2011:

Nice Job! Thank You.

Russ commented Thu Apr 14 21:19:15 UTC 2011:

Seriously if you had a donation button I would donate!!

JerryKun commented Tue Jul 19 15:31:29 UTC 2011:

In some cases, it should be changed on line: nginx="/usr/local/sbin/nginx"

by

nginx="/usr/local/nginx/sbin/nginx"

BTW, This script is useful, Thanks

Trav commented Mon Aug 22 22:50:24 UTC 2011:

Wonderfully done! Thanks for this...

Shyam commented Thu Sep 15 19:09:30 UTC 2011:

Works fine on EL6 based distro's.

Jered commented Thu Sep 15 20:59:54 UTC 2011:

Excellent news. Thanks for passing that along, Shyam.

Roy commented Fri Oct 28 15:06:01 UTC 2011:

Yeah, this is absolutely great. It works fine from every angle. I had did a source install at /etc/nginx...so had to edit the paths.

One thing I did come across was while adding this to chkconfig.

chkconfig nginx on

service nginx does not support chkconfig

ikenna okpala commented Sun Dec 04 20:28:04 UTC 2011:

+1

Fábio Magalhães Costa commented Fri Apr 06 16:16:07 UTC 2012:

Veryy Good! Worked for me!

bogdan commented Sun Jun 24 14:09:58 UTC 2012:

Thanks. This was useful.

Racker commented Sat Sep 15 23:16:44 UTC 2012:

Basically, I love the slicehost articles. Thank you, you guys are awesome yet again.

sumitt commented Tue Feb 26 14:59:03 UTC 2013:

I tried but wen i do chkconfig nginx on output "service nginx does not support chkconfig"

Satish commented Tue Apr 30 07:27:43 UTC 2013:

I installed nginx on default location /opt/nginx so I made changes in /etc/init.d/nginx file are following:

nginx="/opt/nginx/sbin/nginx"

NGINXCONFFILE="/opt/nginx/conf/nginx.conf"

Thanks for helpful article... :)

Satish commented Tue Apr 30 07:28:09 UTC 2013:

I installed nginx on default location /opt/nginx so I made changes in /etc/init.d/nginx file are following:

nginx="/opt/nginx/sbin/nginx"

NGINXCONFFILE="/opt/nginx/conf/nginx.conf"

Thanks for helpful article... :)

Want to comment?


(not made public)

(optional)

(use plain text or Markdown syntax)