习惯了用service来启动、停止服务,nginx却没有一个官方的程序来实现。
随便写了一个,也方便需要的朋友,有什么问题也请提出来,我会进一步进行修改。

#!/bin/bash
##THIS SCRIPT HELP YOU START/STOP/RESTART Nginx AND PHP-FASTCGI##
##YOU MUST HAVE THE "spawn-fcgi" script, YOU CAN FIND IT IN "LIGHTTPD SERVER" AND COPY IT IN "$PHP_PATH/bin/spawn-fcgi" ##
##WARNNING:CANN'T SUPPORT BINARY##
##Copyright By Chancey##

##PHP-FASTCGI SETTING##
HOST=127.0.0.1
PORT=9000
CHILDS=5 #FAST-CGI threads (default 5)

##CHECK SHELL##

if [ “$SHELL” != “/bin/bash” ]; then
echo “shell isn’t the BASH ,script breaking”
exit 1;
fi

##CHECK PHP##

CHECK_PHPPATH=`whereis php`
PHP_PATH=${CHECK_PHPPATH#php: }
CGI=”$PHP_PATH/bin/php-cgi” #filename of the fcgi-application

if [ “$PHP_PATH” != “/usr/local/php” ]; then
echo “Cann’t find php’s path”
exit 1
fi

##CHECK NGINX##

CHECK_NGINXPATH=`whereis nginx`
NGINX_PATH=${CHECK_NGINXPATH#nginx: }

if [ “$NGINX_PATH” != “/usr/local/nginx” ];then
echo “Cann’t find Nginx’s path”
exit 1
fi

PHP_PID=`ps x|grep “php-cgi”|awk {’print $5′}|grep -v grep|uniq`
PHP_PIDFILE=”$PHP_PATH/cgi.pid”
PHP_BIN=”$PHP_PATH/bin/spawn-fcgi -a $HOST -p $PORT -C $CHILDS -P $PHP_PIDFILE -f $CGI”
NGINX_PID=`ps x|grep nginx|grep -v grep|awk {’print $5′}`
NGINX_PIDFILE=”/var/run/nginx.pid”
NGINX_BIN=”$NGINX_PATH/sbin/nginx”

function start(){

##Check PID and Run Server##
if [ “${PHP_PID#*bin/}” != “php-cgi” ];then
if [ “$NGINX_PID” != “nginx” ];then
$PHP_BIN
$NGINX_BIN
echo “Start Success”
else
echo “Nginx is running, Please stop first”
exit 1
fi
else
echo “PHP-FASTCGI is running, Please stop first”
exit 1

fi
}

function stop(){
##Stop daemon##

##kill PHP-FastCGI pid##
if [ ! -f “$PHP_PIDFILE” ];then
echo “PHP-FastCGI PID file could not be found!”
echo “If PHP-FasCGI is running, Please kill the threads manually”
else
PHP_TEMPPID=`cat $PHP_PIDFILE`
kill $PHP_TEMPPID
rm -f $PHP_PIDFILE
echo “PHP-FastCGI stoped”
fi

##kill Nginx pid##
if [ ! -f “$NGINX_PIDFILE” ];then
echo “Nginx PID file could not be found!”
echo “If Nginx is running, Please kill the threads manually”
exit 1
else
NGINX_TEMPPID=`cat $NGINX_PIDFILE`
kill $NGINX_TEMPPID
rm -f $NGINX_PIDFILE
echo “NGINX stoped”
fi
}

function restart(){
##Restart daemon##
##kill PHP-FastCGI pid##
if [ ! -f “$PHP_PIDFILE” ];then
echo “PHP-FastCGI is not running!”
else
PHP_TEMPPID=`cat $PHP_PIDFILE`
kill $PHP_TEMPPID
rm -f $PHP_PIDFILE
echo “PHP-FastCGI stoped”
fi

##kill Nginx pid##
if [ ! -f “$NGINX_PIDFILE” ];then
echo “Nginx PID file could not be found!”
echo “If Nginx is running, Please kill the threads manually”
exit 1
else
NGINX_TEMPPID=`cat $NGINX_PIDFILE`
kill $NGINX_TEMPPID
rm -f $NGINX_PIDFILE
echo “NGINX stoped”
fi
sleep 3
##Start daemon##
$PHP_BIN
$NGINX_BIN

##Test PID##

if [ ! -f “$PHP_PIDFILE” ];then
echo “PHP-FastCGI restart fail”
fi

if [ ! -f “$NGINX_PIDFILE” ];then
echo “Nginx restart fail”
exit 1;
fi
echo “Restart Success”

}

mode=$1

case “$mode” in
’start’)
start

;;

’stop’)
stop

;;

‘restart’)
restart

;;

*)
#usage
echo “Usage: $0 {start|stop|restart} [ Nginx server options ]”
exit 1
esac

exit 0