Skip to content

Instantly share code, notes, and snippets.

@vdel26 vdel26/nginx
Last active Sep 24, 2019

Embed
What would you like to do?
Openresty init.d script
#!/bin/sh
#
# chkconfig: 2345 55 25
# Description: Nginx init.d script, put in /etc/init.d, chmod +x /etc/init.d/nginx
# For Debian, run: update-rc.d -f nginx defaults
# For CentOS, run: chkconfig --add nginx
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: nginx init.d script
# Description: OpenResty (aka. ngx_openresty) is a full-fledged web application server by bundling the standard Nginx core, lots of 3rd-party Nginx modules, as well as most of their external dependencies.
### END INIT INFO
#
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="Nginx Daemon"
NAME=nginx
PREFIX=/usr/local/openresty/nginx
DAEMON=$PREFIX/sbin/$NAME
CONF=$PREFIX/conf/$NAME.conf
PID=$PREFIX/logs/$NAME.pid
SCRIPT=/etc/init.d/$NAME
if [ ! -x "$DAEMON" ] || [ ! -f "$CONF" ]; then
echo -e "\033[33m $DAEMON has no permission to run. \033[0m"
echo -e "\033[33m Or $CONF doesn't exist. \033[0m"
sleep 1
exit 1
fi
do_start() {
if [ -f $PID ]; then
echo -e "\033[33m $PID already exists. \033[0m"
echo -e "\033[33m $DESC is already running or crashed. \033[0m"
echo -e "\033[32m $DESC Reopening $CONF ... \033[0m"
$DAEMON -s reopen -c $CONF
sleep 1
echo -e "\033[36m $DESC reopened. \033[0m"
else
echo -e "\033[32m $DESC Starting $CONF ... \033[0m"
$DAEMON -c $CONF
sleep 1
echo -e "\033[36m $DESC started. \033[0m"
fi
}
do_stop() {
if [ ! -f $PID ]; then
echo -e "\033[33m $PID doesn't exist. \033[0m"
echo -e "\033[33m $DESC isn't running. \033[0m"
else
echo -e "\033[32m $DESC Stopping $CONF ... \033[0m"
$DAEMON -s stop -c $CONF
sleep 1
echo -e "\033[36m $DESC stopped. \033[0m"
fi
}
do_reload() {
if [ ! -f $PID ]; then
echo -e "\033[33m $PID doesn't exist. \033[0m"
echo -e "\033[33m $DESC isn't running. \033[0m"
echo -e "\033[32m $DESC Starting $CONF ... \033[0m"
$DAEMON -c $CONF
sleep 1
echo -e "\033[36m $DESC started. \033[0m"
else
echo -e "\033[32m $DESC Reloading $CONF ... \033[0m"
$DAEMON -s reload -c $CONF
sleep 1
echo -e "\033[36m $DESC reloaded. \033[0m"
fi
}
do_quit() {
if [ ! -f $PID ]; then
echo -e "\033[33m $PID doesn't exist. \033[0m"
echo -e "\033[33m $DESC isn't running. \033[0m"
else
echo -e "\033[32m $DESC Quitting $CONF ... \033[0m"
$DAEMON -s quit -c $CONF
sleep 1
echo -e "\033[36m $DESC quitted. \033[0m"
fi
}
do_test() {
echo -e "\033[32m $DESC Testing $CONF ... \033[0m"
$DAEMON -t -c $CONF
}
do_info() {
$DAEMON -V
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
reload)
do_reload
;;
restart)
do_stop
do_start
;;
quit)
do_quit
;;
test)
do_test
;;
info)
do_info
;;
*)
echo "Usage: $SCRIPT {start|stop|reload|restart|quit|test|info}"
exit 2
;;
esac
exit 0
@rcosnita

This comment has been minimized.

Copy link

rcosnita commented Oct 24, 2014

Works great. Thank you for this wonderful snippet.

@Alexander-0x80

This comment has been minimized.

Copy link

Alexander-0x80 commented Aug 25, 2015

You saved me a lot of work , thanks

@mjguisado

This comment has been minimized.

Copy link

mjguisado commented Sep 1, 2015

Thanks Victor

@ask870

This comment has been minimized.

Copy link

ask870 commented Sep 18, 2015

Good,Thks

@zgao4

This comment has been minimized.

Copy link

zgao4 commented Nov 16, 2015

Thx a lot

@congkhoa

This comment has been minimized.

Copy link

congkhoa commented Jan 11, 2016

good job! thanks!

@parserpro

This comment has been minimized.

Copy link

parserpro commented Jan 13, 2016

fails if pid-file exists but process has gone.

@DigDeeply

This comment has been minimized.

Copy link

DigDeeply commented Jan 18, 2016

It works for me, Thanks~~

@Eric-Guo

This comment has been minimized.

Copy link

Eric-Guo commented Feb 8, 2016

For those who running in debian/ubuntu, there is more up-to-date version available.

@cthurston

This comment has been minimized.

Copy link

cthurston commented Mar 13, 2016

Thanks for this. Built my own nginx and the script from nginx.org was not working for me so I borrowed this one. Worked great!

@bor8

This comment has been minimized.

Copy link

bor8 commented Mar 23, 2016

@vdel26: Works. Thanks.

@Eric-Guo: The up-to-date version does not work for me!

@dengqingguo

This comment has been minimized.

Copy link

dengqingguo commented Apr 1, 2016

Thanks for this,but it does not work for me.
service starts expected,also in chkconfig list,but does not work after reboot!

@stephanebachelier

This comment has been minimized.

Copy link

stephanebachelier commented May 9, 2016

@bor8 the version from @Eric-Guo is not configured for openresty

@lojunren

This comment has been minimized.

Copy link

lojunren commented Sep 21, 2016

Thanks for your great job.

@supernintendo

This comment has been minimized.

Copy link

supernintendo commented Sep 25, 2016

Works perfectly on Debian 8, much thanks.

@apueee

This comment has been minimized.

Copy link

apueee commented Oct 31, 2016

This script works fine for me. But not start when reboot. I was using auto scaling in aws. So when new server start from an image it doesn't run nginx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.