Linux配置nginx为服务

10 篇文章 0 订阅

一个在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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值