Nginx和Tomcat的管理脚本

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx和Tomcat的管理脚本相关的知识,希望对你有一定的参考价值。

一、传统模式下Nginx的启动、关闭

1、启动:/usr/local/nginx/sbin/nginx
2、关闭:killall nginx
3、重启:/usr/local/nginx/sbin/nginx -s reload

二、通过配置/etc/init.d/nginx来管理Nginx

#!/bin/bash  
# nginx Startup script for the Nginx HTTP Server  
nginxd=/usr/local/nginx/sbin/nginx  
nginx_config=/usr/local/nginx/conf/nginx.conf  
nginx_pid=/var/run/nginx.pid  
RETVAL=0  
prog="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  
[ -x $nginxd ] || exit 0  
# Start nginx daemons functions.  
start() {  
if [ -e $nginx_pid ];then  
   echo "nginx already running...."  
   exit 1  
fi  
   echo -n $"Starting $prog: "  
   daemon $nginxd -c ${nginx_config}  
   RETVAL=$?  
   echo  
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx  
   return $RETVAL  
}  
# Stop nginx daemons functions.  
stop() {  
        echo -n $"Stopping $prog: "  
        killproc $nginxd  
        RETVAL=$?  
        echo  
        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid  
}  
# reload nginx service functions.  
reload() {  
    echo -n $"Reloading $prog: "  
    #kill -HUP `cat ${nginx_pid}`  
    killproc $nginxd -HUP  
    RETVAL=$?  
    echo  
}  
# See how we were called.  
case "$1" in  
start)  
        start  
        ;;  
stop)  
        stop  
        ;;  
reload)  
        reload  
        ;;  
restart)  
        stop  
        start  
        ;;  
status)  
        status $prog  
        RETVAL=$?  
        ;;  
*)  
        echo $"Usage: $prog {start|stop|restart|reload|status|help}"  
        exit 1  
esac  
exit $RETVAL  

保存并添加执行权限

service nginx start    //启动Nginx
service nginx stop    //关闭Nginx
service nginx restart  //重启Nginx
service nginx status  //查看Nginx的状态

添加至开机启动
chkconfig --add nginx
chkconfig nginx on

三、传统Tomcat的开启、关闭

1、启动:/usr/local/apache-tomcat-9.0.7/bin/startup.sh
2、关闭:/usr/local/apache-tomcat-9.0.7/bin/shutdown.sh

四、配置/etc/init.d/tomcat管理Tomcat

# tomcat startup script for the Tomcat server  
. /etc/rc.d/init.d/functions

prog=tomcat
JAVA_HOME=/usr/local/java/
export JAVA_HOME  
CATALANA_HOME=/usr/local/apache-tomcat-9.0.7/
export CATALINA_HOME  

case "$1" in
start)
    echo "Starting Tomcat..."  
    $CATALANA_HOME/bin/startup.sh
    ;;

stop)
    echo "Stopping Tomcat..."  
    $CATALANA_HOME/bin/shutdown.sh
    ;;

restart)
    echo "Stopping Tomcat..."  
    $CATALANA_HOME/bin/shutdown.sh
    sleep 2
    echo  
    echo "Starting Tomcat..."  
    $CATALANA_HOME/bin/startup.sh
    ;;

*)
    echo "Usage: $prog {start|stop|restart}"  
    ;;
esac
exit 0

service tomcat start
service tomcat stop
service tomcat restart

chkconfig --add tomcat
chkconfig tomcat on

以上是关于Nginx和Tomcat的管理脚本的主要内容,如果未能解决你的问题,请参考以下文章

etcd+confd实现Nginx配置文件自动管理

Nginx+Tomcat实现负载均衡动静分离(脚本方法)

Nginx+Tomcat动静分离脚本

管理多tomcat实例的shell脚本

02-日常管理脚本-ansible 程序断电修复;

Nginx实现动静结合(动态资源由tomcat管理: 静态资源由Nginx管理)