# Nginx Startup Script

**URL:** <https://www.ruby-forum.com/t/nginx-startup-script/111544>\
**Category:** Deployment\
**Created:** [August 8, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/nginx-startup-script/111544 "2007-08-08T13:28:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jwesonga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jwesonga](https://www.ruby-forum.com/u/jwesonga)\
**Post date:** [August 8, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/nginx-startup-script/111544/1 "2007-08-08T13:28:39Z")

</div>

Hi,

I’ve found a very good Nginx startup script here  
[http://topfunky.net/svn/shovel/nginx/init.d/nginx](http://topfunky.net/svn/shovel/nginx/init.d/nginx) but I’m having a  
problem setting it up.

this command does not see to be working on CentOS:  
update-rc.d nginx defaults

Could anyone assist?

---

<div class="post-metadata">

**Author:** ![jwesonga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jwesonga](https://www.ruby-forum.com/u/jwesonga)\
**Post date:** [August 8, 2007, 2:26pm UTC](https://www.ruby-forum.com/t/nginx-startup-script/111544/2 "2007-08-08T14:26:55Z")

</div>

On 8/8/07, jwesonga [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Could anyone assist?

Red hat based distros use the chkconfig tool to configure services.

See the man page for more info. Note the part about adding “chkconfig”  
and “description” comments to your script.

Isak

---

<div class="post-metadata">

**Author:** ![jwesonga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jwesonga](https://www.ruby-forum.com/u/jwesonga)\
**Post date:** [August 15, 2007, 8:37pm UTC](https://www.ruby-forum.com/t/nginx-startup-script/111544/3 "2007-08-15T20:37:38Z")

</div>

I think this script has a problem…

you won’t be able to stop the server, because the PID file is not  
there… it starts the server without passing it a custom location for  
PID… and the PID file really is in /usr/local/nginx/logs (by default).

jwesonga wrote:

> Hi,
> 
> I’ve found a very good Nginx startup script here  
> [http://topfunky.net/svn/shovel/nginx/init.d/nginx](http://topfunky.net/svn/shovel/nginx/init.d/nginx) but I’m having a  
> problem setting it up.
> 
> this command does not see to be working on CentOS:  
> update-rc.d nginx defaults
> 
> Could anyone assist?

---

<div class="post-metadata">

**Author:** ![jwesonga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/54ee81/32.png) [@jwesonga](https://www.ruby-forum.com/u/jwesonga)\
**Post date:** [August 15, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/nginx-startup-script/111544/4 "2007-08-15T21:13:01Z")

</div>

there is also some minor inconsistencies, for example:  
usage prints: force-reload (should be only “reload”).

I made some small edits, so you don’t have to:

#! /bin/sh

# Description: Startup script for nginx webserver on Debian. Place in

/etc/init.d and

# run ‘sudo update-rc.d nginx defaults’, or use the appropriate command

on your

# distro.

# 

# Author: Ryan N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

# Modified: Geoffrey Grosenbach [http://topfunky.com](http://topfunky.com)

# Modified: David K. [http://davidhq.com](http://davidhq.com)

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  
DESC=“nginx daemon”  
NAME=nginx  
DAEMON=/usr/local/nginx/sbin/$NAME  
CONFIGFILE=/usr/local/nginx/conf/nginx.conf  
PIDFILE=/usr/local/nginx/logs/$NAME.pid  
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.

test -x $DAEMON || exit 0

d\_start() {  
$DAEMON -c $CONFIGFILE || echo -en “\n already running”  
}

d\_stop() {  
kill -QUIT `cat $PIDFILE` || echo -en “\n not running”  
}

d\_reload() {  
kill -HUP `cat $PIDFILE` || echo -en “\n can’t reload”  
}

case “$1” in  
start)  
echo -n “Starting $DESC: $NAME”  
d\_start  
echo “.”  
;;  
stop)  
echo -n “Stopping $DESC: $NAME”  
d\_stop  
echo “.”  
;;  
reload)  
echo -n “Reloading $DESC configuration…”  
d\_reload  
echo “.”  
;;  
restart)  
echo -n “Restarting $DESC: $NAME”  
d\_stop  
# One second might not be time enough for a daemon to stop,  
# if this happens, d\_start will fail (and dpkg will break if  
# the package is being upgraded). Change the timeout if needed  
# be, or change d\_stop to have start-stop-daemon use --retry.  
# Notice that using --retry slows down the shutdown process  
somewhat.  
sleep 1  
d\_start  
echo “.”  
;;  
\*)  
echo “Usage: $SCRIPTNAME {start|stop|restart|reload}” \>&2  
exit 3  
;;  
esac

exit 0
