#!/bin/bash
cat<<EOF
+----------------------选择编译安装的Nginx版本----------------------+
| (1)nginx-1.12.2 |
| (2)nginx-1.14.2 |
| (3)nginx-1.16.1 |
| (4)nginx-1.18.0 |
| (0)取消 |
+-------------------------------------------------------------------+
EOF
read -p "请输入你要安装的版本的编号: " Num
i=1
while [[ ! $Num =~ ^[0-9]+$ ]]
do
if [ $i -gt 2 ];then
echo -e "\033[31mERROR: 您的输入有误!已取消安装!\033[0m"
exit
else
echo -e "\033[33mWARNING: 你的输入有误,请重新输入!\033[0m"
read -p "请输入你要安装的版本的编号: " Num
fi
let i++
done
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
Confirm_Install(){
read -p "你是否确定编译安装${Version}版本【yes|no】: " Confirm
i=1
while [[ ! $Confirm == "yes" ]] && [[ ! $Confirm =~ "no" ]]
do
if [ $i -gt 2 ];then
echo -e "\033[31mERROR: 你的输入有误!已经消安装!\033[0m"
exit
else
read -p "你是否确定编译安装${Version}版本【yes|no】: " Confirm
fi
let i++
done
if [ $Confirm == "yes" ];then
continue
elif [ $Confirm == "no" ];then
echo "已取消安装${Version}"
exit
fi
}
Download_Nginx(){
local i=1
while true
do
rm -rf /tmp/$Ngx_Pkg
wget -O /tmp/$Ngx_Pkg $Ngx_Url
if [ $? -eq 0 -a -f /tmp/$Ngx_Pkg ];then
action "源码包${Ngx_Pkg}下载成功!" /bin/true
break
fi
if [ $i -gt 2 ];then
echo -e "\033[31mERROR: 源码包${Ngx_Pkg}下载失败次数过多!请检查网络后重.!\033[0m"
exit
fi
let i++
done
}
Check_Env(){
systemctl status nginx &>/dev/null
if [ $? -eq 0 -o $? -eq 3 ];then
echo -e "\033[33mWARNING: 系统中已安装nginx服务!\033[0m"
read -p "你是否要继续编译安装${Version} 【yes|no】: " Confirm
local i=1
while [[ ! $Confirm == "yes" ]] && [[ ! $Confirm =~ "no" ]]
do
if [ $i -gt 2 ];then
echo -e "\033[31mERROR: 你的输入有误!已经消安装!\033[0m"
exit
else
read -p "你是否要继续编译安装${Version} 【yes|no】: " Confirm
fi
let i++
done
if [ $Confirm == "yes" ];then
echo "你选择了继续编译安装${Version}"
continue
elif [ $Confirm == "no" ];then
echo "已经取消安装!"
exit
fi
fi
}
New_User(){
id www &>/dev/null
if [ $? -eq 0 ];then
echo -e "\033[33mWARNING: 用户www已存在!无需重新创建!"
else
groupadd -g 666 www
useradd -g666 -u666 -M -s /sbin/nologin www
action "用户www创建成功!" /bin/true
fi
}
Install_Dependencies(){
echo "正在安装依赖......"
yum -y install zip unzip gcc gcc-c++ automake autoconf libtool make glibc gd-devel pcre-devel \
libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype \
freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 \
bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel \
libidn libidn-devel openssl openssl-devel libevent libevent-devel
if [ $? -eq 0 ];then
action "依赖环境安装完成!" /bin/true
else
action "依赖环境安装失败!" /bin/false
fi
}
Precompiled(){
./configure --prefix=/application/$Version \
--user=www \
--group=www \
--with-compat \
--with-file-aio \
--with-threads \
--with-stream \
--with-http_addition_module \
--without-http_auth_basic_module \
--with-http_auth_request_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_module \
--with-http_realip_module \
--without-http_rewrite_module \
--with-http_sub_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_v2_module
}
ping -c1 -w1 www.baidu.com &> /dev/null
if [ $? -ne 0 ];then
echo -e "\033[31m网络连接出现问题!请检查网络后重试!\033[0m"
exit
fi
Ngx_Dir=/application
[ -d $Ngx_Dir ] || mkdir -p $Ngx_Dir
case $Num in
1)
Version=nginx-1.12.2
Confirm_Install
echo "开始下载Nginx源码包......"
Ngx_Url=http://download.wanhebin.com/package/nginx/${Version}.tar.gz
Ngx_Pkg=${Ngx_Url##*/}
echo -e "\033[32m=====================================开始下载Nginx源码包=====================================\033[0m"
Download_Nginx
echo -e "\033[32m======================================开始检查系统环境=======================================\033[0m"
Check_Env
New_User
echo -e "\033[32m======================================开始安装依赖环境=======================================\033[0m"
Install_Dependencies
echo -e "\033[32m======================================开始预编译源码包=======================================\033[0m"
cd /tmp/
rm -rf $Version
tar xf $Ngx_Pkg
if [ $? -eq 0 ];then
echo "源码包解压成功,开始预编译......"
cd $Version
Precompiled
if [ $? -eq 0 ];then
action "源码包${Version}预编译成功!" /bin/true
else
action "源码包${Version}预编译失败!" /bin/false
echo "已取消安装!"
fi
fi
echo -e "\033[32m========================================开始编译安装=========================================\033[0m"
if [ -d $Ngx_Dir/$Version ];then
echo -e "\033[33mWARNING: 系统已存在编译过的${Version}!\033[0m"
read -p "继续安装将删除已经安装的版本!是否要继续编译安装${Version} [yes|no]: " Confirm
i=1
while [[ ! $Confirm == "yes" ]] && [[ ! $Confirm =~ "no" ]]
do
if [ $i -gt 2 ];then
echo -e "\033[31mERROR: 你的输入有误!已取消安装!\033[0m"
exit
else
read -p "继续安装将删除已经安装的版本!是否要继续编译安装${Version} [yes|no]: " Confirm
fi
let i++
done
if [ $Confirm == "yes" ];then
rm -rf $Ngx_Dir/$Version
elif [ $Confirm == "no" ];then
echo "你已取消安装!"
exit
fi
fi
cd /tmp/$Version
make && make install
if [ $? -eq 0 ];then
action "${Version}编译安装成功!" /bin/true
else
action "${Version}编译安装失败!" /bin/true
exit
fi
;;
2)
Version=nginx-1.14.2
Confirm_Install
echo "开始下载Nginx源码包......"
Ngx_Url=http://download.wanhebin.com/package/nginx/${Version}.tar.gz
Ngx_Pkg=${Ngx_Url##*/}
echo -e "\033[32m=====================================开始下载Nginx源码包=====================================\033[0m"
Download_Nginx
echo -e "\033[32m======================================开始检查系统环境=======================================\033[0m"
Check_Env
New_User
echo -e "\033[32m======================================开始安装依赖环境=======================================\033[0m"
Install_Dependencies
echo -e "\033[32m======================================开始预编译源码包=======================================\033[0m"
cd /tmp/
rm -rf $Version
tar xf $Ngx_Pkg
if [ $? -eq 0 ];then
echo "源码包解压成功,开始预编译......"
cd $Version
Precompiled
if [ $? -eq 0 ];then
action "源码包${Version}预编译成功!" /bin/true
else
action "源码包${Version}预编译失败!" /bin/false
echo "已取消安装!"
fi
fi
echo -e "\033[32m========================================开始编译安装=========================================\033[0m"
if [ -d $Ngx_Dir/$Version ];then
echo -e "\033[33mWARNING: 系统已存在编译过的${Version}!\033[0m"
read -p "继续安装将删除已经安装的版本!是否要继续编译安装${Version} [yes|no]: " Confirm
i=1
while [[ ! $Confirm == "yes" ]] && [[ ! $Confirm =~ "no" ]]
do
if [ $i -gt 2 ];then
read -p "继续安装将删除已经安装的版本!是否要继续编译安装${Version} [yes|no]: " Confirm
else
echo -e "\033[31mERROR: 你的输入有误!已取消安装!\033[0m"
exit
fi
let i++
done
if [ $Confirm == "yes" ];then
rm -rf $Ngx_Dir/$Version
elif [ $Confirm == "no" ];then
echo "你已取消安装!"
exit
fi
fi
cd /tmp/$Version
make && make install
if [ $? -eq 0 ];then
action "${Version}编译安装成功!" /bin/true
else
action "${Version}编译安装失败!" /bin/true
exit
fi
;;
3)
Version=nginx-1.16.1
Confirm_Install
echo "开始下载Nginx源码包......"
Ngx_Url=http://download.wanhebin.com/package/nginx/${Version}.tar.gz
Ngx_Pkg=${Ngx_Url##*/}
echo -e "\033[32m=====================================开始下载Nginx源码包=====================================\033[0m"
Download_Nginx
echo -e "\033[32m======================================开始检查系统环境=======================================\033[0m"
Check_Env
New_User
echo -e "\033[32m======================================开始安装依赖环境=======================================\033[0m"
Install_Dependencies
echo -e "\033[32m======================================开始预编译源码包=======================================\033[0m"
cd /tmp/
rm -rf $Version
tar xf $Ngx_Pkg
if [ $? -eq 0 ];then
echo "源码包解压成功,开始预编译......"
cd $Version
Precompiled
if [ $? -eq 0 ];then
action "源码包${Version}预编译成功!" /bin/true
else
action "源码包${Version}预编译失败!" /bin/false
echo "已取消安装!"
fi
fi
echo -e "\033[32m========================================开始编译安装=========================================\033[0m"
if [ -d $Ngx_Dir/$Version ];then
echo -e "\033[33mWARNING: 系统已存在编译过的${Version}!\033[0m"
read -p "继续安装将删除已经安装的版本!是否要继续编译安装${Version} [yes|no]: " Confirm
i=1
while [[ ! $Confirm == "yes" ]] && [[ ! $Confirm =~ "no" ]]
do
if [ $i -gt 2 ];then
read -p "继续安装将删除已经安装的版本!是否要继续编译安装${Version} [yes|no]: " Confirm
else
echo -e "\033[31mERROR: 你的输入有误!已取消安装!\033[0m"
exit
fi
let i++
done
if [ $Confirm == "yes" ];then
rm -rf $Ngx_Dir/$Version
elif [ $Confirm == "no" ];then
echo "你已取消安装!"
exit
fi
fi
cd /tmp/$Version
make && make install
if [ $? -eq 0 ];then
action "${Version}编译安装成功!" /bin/true
else
action "${Version}编译安装失败!" /bin/true
exit
fi
;;
4)
Version=nginx-1.18.0
Confirm_Install
echo "开始下载Nginx源码包......"
Ngx_Url=http://download.wanhebin.com/package/nginx/${Version}.tar.gz
Ngx_Pkg=${Ngx_Url##*/}
echo -e "\033[32m=====================================开始下载Nginx源码包=====================================\033[0m"
Download_Nginx
echo -e "\033[32m======================================开始检查系统环境=======================================\033[0m"
Check_Env
New_User
echo -e "\033[32m======================================开始安装依赖环境=======================================\033[0m"
Install_Dependencies
echo -e "\033[32m======================================开始预编译源码包=======================================\033[0m"
cd /tmp/
rm -rf $Version
tar xf $Ngx_Pkg
if [ $? -eq 0 ];then
echo "源码包解压成功,开始预编译......"
cd $Version
Precompiled
if [ $? -eq 0 ];then
action "源码包${Version}预编译成功!" /bin/true
else
action "源码包${Version}预编译失败!" /bin/false
echo "已取消安装!"
fi
fi
echo -e "\033[32m========================================开始编译安装=========================================\033[0m"
if [ -d $Ngx_Dir/$Version ];then
echo -e "\033[33mWARNING: 系统已存在编译过的${Version}!\033[0m"
read -p "继续安装将删除已经安装的版本!是否要继续编译安装${Version} [yes|no]: " Confirm
i=1
while [[ ! $Confirm == "yes" ]] && [[ ! $Confirm =~ "no" ]]
do
if [ $i -gt 2 ];then
read -p "继续安装将删除已经安装的版本!是否要继续编译安装${Version} 【yes|no】: " Confirm
else
echo -e "\033[31mERROR: 你的输入有误!已取消安装!\033[0m"
exit
fi
let i++
done
if [ $Confirm == "yes" ];then
rm -rf $Ngx_Dir/$Version
elif [ $Confirm == "no" ];then
echo "你已取消安装!"
exit
fi
fi
cd /tmp/$Version
make && make install
if [ $? -eq 0 ];then
action "${Version}编译安装成功!" /bin/true
else
action "${Version}编译安装失败!" /bin/true
exit
fi
;;
0)
echo "已取消!"
exit
;;
*)
echo -e "\033[31mERROR: 您的输入有误!已取消安装!\033[0m"
exit
esac