#!/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 << EOF [Unit] Description=The NGINX HTTP and reverse proxy server After=syslog.target network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=${install_path}nginx/logs/nginx.pid ExecStartPre=${install_path}nginx/sbin/nginx -t ExecStart=${install_path}nginx/sbin/nginx ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target EOF systemctl enable nginx && systemctl start nginx echo "${DATE} 安裝完成">>${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