shell编程Nginx一键管理脚本:安装、升级、删除

#!/bin/bash

#====================================================

# Author: lijingtao

# Create Date: 2020-02-22

# Description: 

#====================================================

#判断本机是否安装nginx以及它的版本信息

if [ -f /usr/local/nginx/sbin/nginx ];then

var=`/usr/local/nginx/sbin/nginx -v`

NGX_VERSION=$var

else

NGX_VERSION="nginx version not found please install first"

fi

if [[ $# == 0 ]];then

    echo -e  "\e[33m usege{$0 1.16.0/1.12.2 $NGX_VERSION }\e[0m"

    exit

elif [[ ! "$1" =~ ^[12]\.[1-9][0-9]\.[0-9]$ ]];then

    echo "usege{$0 1.16.0/1.12.2 $NGX_VERSION }"

    echo "please check you input "

    exit

fi

#安装nginx

function installnginx(){

    useradd -s /sbin/nologin -M nginx

    cd /usr/src

    wget -c  http://nginx.org/download/nginx-${1}.tar.gz 

    tar zxf nginx-${1}.tar.gz -C /usr/src

    cd /usr/src/nginx-${1}

    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module

    make&&make install

    /usr/local/nginx/sbin/nginx     

    /usr/local/nginx/sbin/nginx -v  

#添加虚拟主机配置文件

     #此处需要判定当前ngixn指定的配置文件在哪里,并取出值替换为变量nx

    sed -i  '/http {/a\include /usr/local/nginx/conf/\*\.conf'   /usr/local/nginx/conf/nginx.conf 

}

#升级nginx 判断nginx已安装才可执行

function updatenginx(){

    cd /usr/src

    wget -c http://nginx.org/download/nginx-${1}.tar.gz -P /usr/src

    tar zxf /usr/src/nginx-${1}.tar.gz -C /usr/src

    cd /usr/src/nginx-${1}

    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module

    make

    mv /usr/local/nginx/sbin/nginx{,.bak}

    cp objs/nginx /usr/local/nginx/sbin/

    kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`

    kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

    /usr/local/nginx/sbin/nginx -s reload

    /usr/local/nginx/sbin/nginx -V

}

#卸载源码安装的nginx  (可附加判断删除yum安装的nginx)

function removenginx(){

    rm -rf /usr/local/nginx/ 

    echo -e "\e[32m nginx install dir  is delated\e[0m"

    rm -rf /usr/src/nginx*

    echo -e "\e[32m nginx-${1}.tar.gz is delated \e[0m"

}

#nginx菜单

function menu(){

    select x in  addhost delhost \do love break

    do

    echo "you choice is $x,now inpupt enter"

    $x

    done

}

function delhost(){

    var=`cat /usr/local/nginx/conf/servername.txt`

    echo "${var}"

    read -p "what virulhost you want delate " hostdel

   for i in ${var}

   do

    rm -rf /usr/local/nginx/conf/${i}.conf

    sed -i "/${i}/d" /usr/local/nginx/conf/servername.txt

    done

}

#修改主配置文件内容添加include 路径。

function addhost(){

    shift 1

    read -p "input you virul servername: " servername

    read -p "input you port: " port

    echo "server {

    listen     ${port};                                                                                                                               

    server_name ${servername} ;

    location / {

      root   html;

      index index.html index.htm;

             }

             }

     "> /usr/local/nginx/conf/${servername}.conf

     echo "${servername}">>/usr/local/nginx/conf/servername.txt

}

#主菜单

PS3="usage{$0 1.16.0/1.12.2 $NGX_VERSION}"

select i in installnginx updatenginx removenginx addandrmhost exit

    do

#    if [ $i == "exit" ];then

#    exit

#   fi 

    case $i in

        installnginx)

        installnginx $1

        ;;

        updatenginx)

        if [ -f /usr/local/nginx/sbin/nginx ];then

        $i $1

        else

        echo -e  "\e[33m nginx has't installed in you system please install nginx first\e[0m"

        fi

        ;;

        removenginx)

        $i $1

        ;;

        addhost)

        menu

        ;;

        exit)

        exit

    esac

done

发布了14 篇原创文章 · 获赞 0 · 访问量 414

猜你喜欢

转载自blog.csdn.net/falnet/article/details/104467457
今日推荐