• nginx自动启动脚本


    nginx自动启动脚本

    环境准备提示:

    启动服务命令为:/application/nginx/sbin/nginx
    停止服务命令为: /application/nginx/sbin/nginx -s stop
    请用case语句开发脚本,以实现Nginx服务启动及关闭的功能,具体脚本命令为/etc/init.d/nginxd {status|start|stop|restart}

        如果读者对Ngnix环境还不是很熟悉,那么请参考跟老男孩学<<Linux运维:Web集群实战>>第5章的内容。
    参考资料:
    https://blog.51cto.com/398528/2053566

    # nginx安装
    
    cd /opt/
    wget https://ftp.pcre.org/pub/pcre/pcre-8.36.tar.gz
    tar -zxvf pcre-8.36.tar.gz
    cd pcre-8.36
    ./configure
    make && make install
    
    cd /opt/
    wget http://nginx.org/download/nginx-1.12.2.tar.gz
    tar -zxvf nginx-1.12.2.tar.gz
    useradd nginx -s /sbin/nologin -M
    yum -y install pcre-devel
    cd nginx-1.12.2
    ./configure --user=nginx --group=nginx --prefix=/application/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_module --with-pcre=/opt/pcre-8.36
    make && make install
    ln -s /application/nginx-1.12.2 /application/nginx
    /application/nginx/sbin/nginx -t
    启动
    /application/nginx/sbin/nginx
    查看端口
    netstat -lnt
    
    ps -ef|grep nginx
    
    反查
    lsof -i :8080
    
    nginx主页文件所在位置
    /application/nginx/conf/nginx.conf

    ---------------------------------------------------------------------------------------------------------------------
    解题思路:
    1 )    先判断Ngnix的PID文件是否存在(Nginx服务正常启动后PID文件就会存在),如果不存在,即表示Nginx没有运行,则运行Nginx服务的启动命令
    (可以把此部分写成start函数)。待要停止时,如果PID存在,就运行Nginx服务停止命令,否则就不运行停止命令(可以把此部分写成stop函数)。
    2 ) 通过脚本传参start或stop等,通过case语句获取参数进行判断。
    3 ) 为了看起来更专业,这里采用前文讲解的系统函数functions中的action函数。
    4 ) 对函数及命令运行的返回值进行处理,使脚本看起来更专业、规范。
    5 ) 通过chkconfig来管理Nginx脚本,实现开机自动启。

    cat >/etc/init.d/nginxd<<"EOF"
    #!/bin/bash
    # chkconfig: 2345 40 98
    # dectiption: Start/Stop Nginx server
    path="/application/nginx/sbin"
    pid="/application/nginx/logs/nginx.pid"
    RETVAL=0     # 设定RETVAL为0,作为返回值变量。
    . /etc/init.d/functions
    
    # start
    function usage(){
        echo $"`basename $0` {status|start|stop|restart}"
        exit 1
    }
    
    function status(){
        if [[ -f $pid ]]; then
            echo "nginx is running..."
        else
            echo "nginx is not running"
        fi
        exit $RETVAL
    }
    
    function start(){
        if [ ! -f $pid ]; then
            $path/nginx
            RETVAL=$?
            if [[ $RETVAL -eq 0 ]]; then
                action "nginx is started" /bin/true
            else
                action "nginx is started" /bin/false
                return RETVAL
            fi
        else
            echo "nginx is running..."
            return 0
        fi
    }
    
    function stop(){
        if [[ -f $pid ]]; then
            $path/nginx -s stop
            RETVAL=$?
            if [[ $RETVAL -eq 0 ]]; then
                action "nginx is stopped" /bin/true
                return $RETVAL
            else
                action "nginx is stopped" /bin/false
                return $RETVAL
            fi
        else
            echo "nginx is no running"
            return $RETVAL
        fi
    }
    
    case "$1" in
        start )
            start
            RETVAL=$?
            ;;
        stop )
            stop
            RETVAL=$?
            ;;
        restart )
            stop
            sleep 3
            start
            RETVAL=$?
            ;;
        status )
            status
            RETVAL=$?
            ;;
        * )
            usage
    esac
    exit $RETVAL
    EOF
    chmod +x /etc/init.d/nginxd 
    
    [root@db nginx-1.12.2]# /etc/init.d/nginxd status
    nginx is running...
    [root@db nginx-1.12.2]# /etc/init.d/nginxd stop
    nginx is stopped                                           [  OK  ]
    [root@db nginx-1.12.2]# /etc/init.d/nginxd status
    nginx is not running
    [root@db nginx-1.12.2]# /etc/init.d/nginxd start
    nginx is started                                           [  OK  ]
    [root@db nginx-1.12.2]# /etc/init.d/nginxd stop
    nginx is stopped                                           [  OK  ]
    [root@db nginx-1.12.2]# /etc/init.d/nginxd restart
    nginx is no running
    nginx is started                                           [  OK  ]
    [root@db nginx-1.12.2]# /etc/init.d/nginxd restart
    nginx is stopped                                           [  OK  ]
    nginx is started                                           [  OK  ]
    [root@db nginx-1.12.2]# 
  • 相关阅读:
    (一)主动学习概念与技术
    mybatis 分页插件PageHelper 使用方法
    单例模式-Singleton
    解决tomcat启动报 java.lang.IllegalArgumentException: Invalid <url-pattern> login in servlet mapping
    如何在MSDN上获取Win7镜像
    解决 Could not find resource com/baidou/dao/UserMapper.xml
    4、XML 配置
    3、使用Map传参 & 模糊查询
    图解python环境搭建
    2、CRUD
  • 原文地址:https://www.cnblogs.com/bjx2020/p/11176636.html
Copyright © 2020-2023  润新知