Nginx启动脚本/重启脚本

jopen 9年前

1.介绍

    nginx 启动关闭,每次需要手动kill掉nginx,配置文件发生变化需要改变,提供该脚本解决问题

2.脚本

#!/bin/sh
#NGINX_HOME=`dirname $(cd "$(dirname "$0")"; pwd)`  NGINX_HOME='/usr/local/nginx'  # Source function library.   . /etc/rc.d/init.d/functions     # Source networking configuration.   . /etc/sysconfig/network     # Check that networking is up.   [ "$NETWORKING" = "no" ] && exit 0     nginx="$NGINX_HOME/sbin/nginx"   prog=$(basename $nginx)     NGINX_CONF_FILE="$NGINX_HOME/conf/nginx.conf"     lockfile=/var/lock/subsys/nginx     start() {       [ -x $nginx ] || exit 5       [ -f $NGINX_CONF_FILE ] || exit 6       echo -n $"Starting $prog: "       daemon $nginx -c $NGINX_CONF_FILE       retval=$?       echo       [ $retval -eq 0 ] && touch $lockfile       return $retval   }     stop() {       echo -n $"Stopping $prog: "       killproc $prog -QUIT       retval=$?       echo       [ $retval -eq 0 ] && rm -f $lockfile       return $retval   }     restart() {       configtest || return $?       stop       start   }     reload() {       configtest || return $?       echo -n $"Reloading $prog: "       killproc $nginx -HUP       RETVAL=$?       echo :wq  }     force_reload() {       restart   }     configtest() {     $nginx -t -c $NGINX_CONF_FILE   }     rh_status() {       status $prog   }     rh_status_q() {       rh_status >/dev/null 2>&1   }     case "$1" in       start)           rh_status_q && exit 0           $1           ;;       stop)           rh_status_q || exit 0           $1           ;;       restart|configtest)           $1           ;;       reload)           rh_status_q || exit 7           $1           ;;       force-reload)           force_reload           ;;       status)           rh_status           ;;       condrestart|try-restart)           rh_status_q || exit 0               ;;       *)           echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"           exit 2   esac


3.配置


    该脚本有两种方式部署,第一种放到/etc/init.d service启动,第二中放到 nginx bin目录下替代原来nginx 

3.1 service启动

将nginx脚本 复制到/etc/init.d/目录下,脚本名nginx

 赋予可执行权限 chmod +x /etc/init.d/nginx

检查一下/sbin/chkconfig nginx on; /sbin/chkconfig --list nginx

之后,就可以使用以下命令了

service nginx start
service nginx stop
service nginx restart
service nginx reload

/etc/init.d/nginx start
/etc/init.d/nginx stop
/etc/init.d/nginx restart

/etc/init.d/nginx reload

3.2 替换./sbin/nginx

     mv nginx /usr/local/nginx  /sbin/nginxs

     修改nginxs,改变NGINX_HOME值 

NGINX_HOME=`dirname $(cd "$(dirname "$0")"; pwd)`  #NGINX_HOME='/usr/local/nginx'
     chmod +x nginxs

     ./sbin/nginxs status

3.2.1 开启启动

   编辑  /etc/rc.local 放入

   /usr/local/nginx/sbin/nginxs start