Shell 스 크 립 트 프로 그래 밍 - Nginx 자동화 설치

3648 단어 ShellNginx
#!/bin/bash
# auth liheng
# date 2019-05-20

#      
. /etc/init.d/functions

#      
sys=`rpm -q centos-release|cut -d- -f3`

#    
DATE="`date +%F' '%H:%M:%S`"
install_path="/opt/payment/soft/"
install_log_name="install_nginx.log"
install_log_path="/var/log/install_log/"
download_path="/usr/local/software/"
nginx="nginx-1.8.0"
zlib="zlib-1.2.11"
pcre="pcre-8.12"
openssl="openssl-1.0.1h"
nginx_url="http://nginx.org/download/${nginx}.tar.gz"
zlib_url="http://www.zlib.net/${zlib}.tar.gz"
pcre_url="https://datapacket.dl.sourceforge.net/project/pcre/pcre/8.12/${pcre}.tar.gz"
openssl_url="https://www.openssl.org/source/${openssl}.tar.gz"

#      
common_func () {
    output_msg "        "
    useradd -s /sbin/nologin nginx && yum install gcc-c++ -y > /dev/null 2>&1
}

#      (    ,       ,        ,     )
output_msg () {
    for msg in $*;do
        action ${msg} /bin/true
    done
}

#        ,$1      ,$2       yum     
check_yum_command () {
    output_msg "    :$1"
    hash $1 >/dev/null 2>&1
    if [ $? -eq 0 ];then
        echo "${DATE} check command $1 " >> ${install_log_path}${install_log_name} && return 0
        else
            yum -y install $2 >/dev/null 2>&1
    fi
}

#        ,        
check_dir () {
    output_msg "    "
    for dirname in $*;do
        [ -d ${dirname} ] || mkdir -p ${dirname} >/dev/null 2>&1
        echo "${DATE} ${dirname} check success!" >> ${install_log_path}${install_log_name}
    done
}

#      
download_file () {
    output_msg "     "
    mkdir -p ${download_path}
    for file in $*;do
        wget ${file} -O ${download_path}${file##*/} &> /dev/null
        if [ $? -eq 0 ];then
           echo "${DATE} ${file} download success!">>${install_log_path}${install_log_name}
        else
           echo "${DATE} ${file} download fail!">>${install_log_path}${install_log_name} && exit 1
        fi
    done
}

#      ,              ,    。
extract_file() {
    output_msg "    "
    for file in $*;do
        tar xf ${download_path}${file} -C ${install_path} &&  echo "${DATE} ${file} extrac success!,path is ${install_path}" >> ${install_log_path}${install_log_name} || echo "${DATE} ${file} extrac fail!,path is ${install_path}" >> ${install_log_path}${install_log_name}
    done
}

install_func () {
    output_msg "    "
    cd ${install_path}${nginx} && ./configure --prefix=${install_path}${nginx%%-*} --with-pcre=${install_path}${pcre} --with-http_stub_status_module --with-http_ssl_module --with-openssl=${install_path}${openssl} --with-zlib=${install_path}${zlib} --user=nginx --group=nginx && make && make install
    if [ ${sys} -eq 7 ];then
cat >/lib/systemd/system/nginx.service <>${install_log_path}${install_log_name}
    elif [ ${sys} -eq 6 ];then
        cd ${install_path}nginx/sbin && ./nginx
        echo "${DATE}     ">>${install_log_path}${install_log_name}
    else
        echo "${DATE}         ">>${install_log_path}${install_log_name}
    fi
}

main () {
    common_func
    check_dir ${install_log_path} ${install_path} ${download_path}
    check_yum_command wget wget
    download_file ${nginx_url} ${pcre_url} ${zlib_url} ${openssl_url}
    extract_file ${nginx}.tar.gz ${pcre}.tar.gz ${zlib}.tar.gz ${openssl}.tar.gz
    install_func
    
}

main

좋은 웹페이지 즐겨찾기