#!/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