日期:2014-05-16  浏览次数:20982 次

Linux配置nginx为服务

一个在Linux下的nginx服务脚本
支持 启动/关闭/重启/状态/进程信息/测试配置文件

#!/bin/bash
# chkconfig: 2345 58 74
#
# nginx – This shell script takes care of starting and stopping nginx.
#
# desc: nginx [engine x] is light and fast http web/proxy server

nginx_path=/usr/local/nginx
nginx_exec=$nginx_path/sbin/nginx
nginx_conf=$nginx_path/conf/nginx.conf
nginx_pid=$nginx_path/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

[ -x $nginx_exec ] || exit 0

retval=0
exec=”nginx”

# Start daemons.
start() {
if [ -e $nginx_pid -a ! -z $nginx_pid ];then
echo “nginx already running…”
exit 1
fi

if [ -e $nginx_conf ];then
echo -n $”Starting $exec: ”
$nginx_exec -c $nginx_conf &
retval=$?
[ $retval -eq 0 ] && {
touch /var/lock/subsys/$exec
success $”$exec”
}
echo
else
retval=1
fi
return $retval
}

# Stop daemons.
stop() {
echo -n $”Stopping $exec: ”
killproc -d 10 $nginx_exec
retval=$?
echo
[ $retval = 0 ] && rm -f $nginx_pid /var/lock/subsys/$exec
}

reload() {
echo -n $”Reloading $exec: ”
ps auxww | grep nginx | grep master | awk ‘{print $2}’ | xargs kill -HUP
success $”$exec”
echo
}

test() {
$nginx_exec -t -c $nginx_conf
}

# See how we were called.
case “$1″ in
start)
start
;;

stop)
stop
;;

restart)
stop
start
;;

status)
status $exec
retval=$?
;;

show)
ps -aux|grep nginx
;;

reload)
reload
;;

test)
test
;;

*)
echo $”Usage: $0 {start|stop|restart|status|show|reload|test}”
exit 1
esac

exit $retval

# 脚本名称 nginx , 需要将该脚本复制到 /etc/rc.d/init.d 并执行以下命令
# chmod 755 nginx
# chkconfig --add nginx
# 此方法可能仅限红帽系列的Linux,包括CentOS。测试环境rhel6.0