前序

此脚本能识别本地离线安装包, 根据用户需求,是否使用线上资源download再安装,关键字选择y则执行运行脚本即可, 脚本会自动下载相关的版本(具体版本请在脚本开头说明),用户选择n则表示不联网, 直接安装本地工具包, 编译安装完成后会在当前目录生成nginx-version文件夹。
注:需要把安装包扔packages文件夹下,没有的则创建。

shell脚本

#*************************************************************************
#         > File Name: nginx.sh
#         > Author: chenglee
#         > Main : chengkenlee@sina.com
#         > Blog : http://www.cnblogs.com/chenglee/
#         > Created Time : 2019年03月06日 星期三 17时53分04秒
#*************************************************************************
#!/bin/bash
nginxv="1.13.12"
opensslv="1.1.1"
pcrev="8.40"
zlibv="1.2.11"
#************************************************************************#
prefix_name="nginx-${nginxv}"
prefix_path=$(cd $(dirname $0); pwd)
packages="packages"
#************************************************************************#
function environment(){
    GCCBIN=`which gcc`
    GCCV=$(echo $GCCBIN | grep "gcc")
    if [[ "$GCCV" != "" ]]
    then
        echo -e "\033[33;5m check gcc,g++ is installed, thanks!! \033[0m"
    else
        echo "gcc not installed!!"
        echo -e "\033[33;5m please install gcc gcc-c++ cmake now! \033[0m"
        exit;
    fi
}
function download(){
    if [ -d ${packages} ];then
        echo;
    else
        mkdir ${packages}
    fi
    #openssl
    wget -P ${packages} https://ftp.openssl.org/source/openssl-${opensslv}.tar.gz
    #pcre
    wget -P ${packages} http://ftp.pcre.org/pub/pcre/pcre-${pcrev}.tar.gz
    #zlib
    wget -P ${packages} http://www.zlib.net/fossils/zlib-${zlibv}.tar.gz
    #nginx
    wget -P ${packages} http://nginx.org/download/nginx-${nginxv}.tar.gz
}
function tar_version(){
    opensslv=`basename ${packages}/openssl-*.tar.gz .tar.gz | awk -F '-' '{print$2}'`
    pcrev=`basename ${packages}/pcre-*.tar.gz .tar.gz | awk -F '-' '{print$2}'`
    zlibv=`basename ${packages}/zlib-*.tar.gz .tar.gz | awk -F '-' '{print$2}'`
    nginxv=`basename ${packages}/nginx-*.tar.gz .tar.gz | awk -F '-' '{print$2}'`
    prefix_name="nginx-${nginxv}"
}
function tar_gz(){
    if [ -d "${prefix_name}/lib" ];then
        echo;
    else
        mkdir -p ${prefix_name}/lib
    fi
    tar -zxvf ${packages}/openssl-${opensslv}.tar.gz -C ${prefix_name}/lib
    tar -zxvf ${packages}/pcre-${pcrev}.tar.gz -C ${prefix_name}/lib
    tar -zxvf ${packages}/zlib-${zlibv}.tar.gz -C ${prefix_name}/lib
    tar -zxvf ${packages}/nginx-${nginxv}.tar.gz -C ${packages}
}
function install_anubis(){
    cd ${prefix_path}
    cd ${prefix_name}/lib/openssl* && openssl=`pwd`
    ./config --prefix=${openssl} && make && make install
    cd ${prefix_path}
    cd ${prefix_name}/lib/pcre* && pcre=`pwd`
    ./configure --prefix=${pcre} && make && make install
    cd ${prefix_path}
    cd ${prefix_name}/lib/zlib* && zlib=`pwd`
    ./configure --prefix=${zlib} && make && make install
    cd ${prefix_path}
    cd ${packages}/nginx-${nginxv} && nginx=`pwd`
    ./configure --prefix=${prefix_path}/${prefix_name} --with-pcre=${pcre} --with-openssl=${openssl} --with-zlib=${zlib} && make && make install
}
function main(){
    environment
    read -p "Connecting network download openssl,pcre,zlib,nginx ? y/n:" -t 30 chenglee
    case $chenglee in
        y)
            echo y
            download
            tar_gz
            install_anubis
            ;;
        n)
            echo n
            tar_version
            tar_gz
            install_anubis
            ;;
        *)
            echo -e "\033[31;5m input error! exit! \033[0m"
            exit;
    esac
}
main
文档更新时间: 2019-03-06 17:16   作者:杭州-雪翊寒|深圳-夏天