Skip to content

Instantly share code, notes, and snippets.

@bakins
Last active December 15, 2015 04:09
Show Gist options
  • Save bakins/5199830 to your computer and use it in GitHub Desktop.
Save bakins/5199830 to your computer and use it in GitHub Desktop.
Simple nginx init script for ubuntu
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/nginx
NAME=nginx
DESC=nginx
# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
. /etc/default/nginx
fi
test -x $DAEMON || exit 0
set -e
. /lib/lsb/init-functions
test_nginx_config() {
if $DAEMON -t $DAEMON_OPTS >/dev/null 2>&1
then
return 0
else
$DAEMON -t $DAEMON_OPTS
return $?
fi
}
status() {
status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx
}
start() {
echo -n "Starting $DESC: "
test_nginx_config
$DAEMON $DAEMON_OPTS
echo "$NAME."
}
stop() {
echo -n "Stopping $DESC: "
$DAEMON -s stop
echo "$NAME."
}
restart() {
echo -n "Restarting $DESC: "
status && stop
sleep 1
start
echo "$NAME."
}
reload() {
echo -n "Reloading $DESC configuration: "
test_nginx_config
status && $DAEMON -s reload || start
echo "$NAME."
}
reopen() {
echo -n "Reopening $DESC logs: "
test_nginx_config
status && $DAEMON -s reopen || start
echo "$NAME."
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart|force-reload)
restart
;;
reload)
echo -n "Reloading $DESC configuration: "
reload
echo "$NAME."
;;
configtest)
echo -n "Testing $DESC configuration: "
if test_nginx_config
then
echo "$NAME."
else
exit $?
fi
;;
reopen)
reopen
;;
status)
status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
;;
*)
echo "Usage: $NAME {start|stop|restart|reload|force-reload|status|configtest|reopen}" >&2
exit 1
;;
esac
exit 0
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
[ ! -f /var/run/nginx.pid ] || kill -USR1 `cat /var/run/nginx.pid`
endscript
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment