#!bin/bash
#Date 20190313
#Auth Abel
#时间设置
Time=`date "+%Y-%m-%d_%H:%M:%S"`
#查看系统版本
#OS=`cat /etc/redhat-release |awk -F' ' '{print $4}'|awk -F'.' '{print $1}'`
OS=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
nginx_version="nginx -v"
#创建安装日志目录
if [ ! -d /tmp/logs ];then
mkdir -p /tmp/logs
else
continue
fi
log=/tmp/logs/install_nginx_$Time.log
#下载软件包:
Download_soft(){
wget -P /usr/local/src http://nginx.org/download/nginx-1.16.0.tar.gz &>>$log
wget -P /usr/local/src http://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz &>>$log
wget -P /usr/local/src http://www.zlib.net/zlib-1.2.11.tar.gz &>>$log
wget -P /usr/local/src http://distfiles.macports.org/openssl/openssl-1.0.2r.tar.gz&>>$log
}
#配置aliyun YUM源(源码)
init_yum_ym(){
if [ "$OS" == "7" ];then
echo "Begin install YUM Repo ....." &>>$log
cd /etc/yum.repos.d/ && tar -zcvf /opt/yum.bak.tar.gz /etc/yum.repos.d/*&& rm -rf /etc/yum.repos.d/*&>>$log
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>>$log
wait
yum repolist &>>$log
echo " End install YUM Repo ...." &>>$log
wait
continue
else
echo "Begin install YUM Repo ....." &>>$log
cd /etc/yum.repos.d/ && tar -zcvf /opt/yum.bak.tar.gz /etc/yum.repos.d/*&& rm -rf /etc/yum.repos.d/*&>>$log
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &>>$log
wait
yum repolist &>>$log
echo " End install YUM Repo ...." &>>$log
wait
continue
fi
}
#配置aliyun YUM源(RPM)
init_yum_rpm(){
if [ "$OS" == "7" ];then
echo "Begin install YUM Repo ....." &>>$log
cd /etc/yum.repos.d/ && tar -zcvf /opt/yum.bak.tar.gz /etc/yum.repos.d/*&& rm -rf /etc/yum.repos.d/*&>>$log
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo &>>$log
wait
rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm &>>$log
wait
yum repolist &>>$log
echo " End install YUM Repo ...." &>>$log
continue
else
echo "Begin install YUM Repo ....." &>>$log
cd /etc/yum.repos.d/ && tar -zcvf /opt/yum.bak.tar.gz /etc/yum.repos.d/*&& rm -rf /etc/yum.repos.d/*&>>$log
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo &>>$log
wait
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm &>>$log
wait
yum repolist &>>$log
echo " End install YUM Repo ...." &>>$log
continue
fi
}
Install_Source_Code(){
#创建软件安装目录
if [ ! -d /usr/local/src ];then
mkdir -p /usr/local/src
else
continue
fi
#配置网路YUM源
init_yum_ym
#安装wget gcc automake autoconf libtool make gcc-c++
echo "Begin install tools..." &>>$log
rm -rf /var/run/yum.pid && yum install wget gcc automake autoconf libtool make gcc-c++ net-tools -y &>>$log
wait
IP=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6 | awk '{print $2}' | tr -d "addr:"|sed -n 1p`
echo "End install tools..." &>>$log
#下载软件包
echo "Begin download software: nginx pcre zlib openssl " &>>$log
Download_soft
wait
if [ $? -eq 0 ];then
echo "Download software: nginx pcre zlib openssl sucess ...." &>>$log
continue
else
exit
fi
#创建用户和组
echo 'Begin create user and group' &>>$log
/usr/sbin/groupadd -f www &>>$log
/usr/sbin/useradd -g www www &>>$log
echo 'Sucessfully create user and group' &>>$log
#开始编译安装软件:
#安装pcre包:
echo "Begin install pcre ...." &>>$log
cd /usr/local/src/ && tar -xvf pcre-8.43.tar.gz && cd pcre-8.43 && ./configure && make && make install &>>$log
wait
echo " Install pcre sucess ...." &>>$log
#安装 zlib包
echo "Begin install zlib ...." &>>$log
cd /usr/local/src/ && tar -xvf zlib-1.2.11.tar.gz && zlib-1.2.11 && ./configure && make && make install &>>$log
wait
echo " Install zlib sucess ...." &>>$log
#安装openssl
echo "Begin install openssl ...." &>>$log
cd /usr/local/src/ && tar -xvf openssl-1.0.2r.tar.gz &>>$log
echo " Install openssl sucess ...." &>>$log
#安装 Nginx
echo "Begin install nginx ...." &>>$log
cd /usr/local/src/ && tar -xvf nginx-1.16.0.tar.gz &>>$log
cd nginx-1.16.0
./configure --prefix=/usr/local/nginx \
--user=www \
--group=www \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.43 \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-openssl=/usr/local/src/openssl-1.0.2r &>>$log
wait
make && make install &>>$log
if [ $? -eq 0 ];then
echo "Install nginx sucessfully ..." &>>$log
else
echo "Installation error, please refer to the log file:$log" &>>$log
fi
#启动Nginx
echo "Starting nginx .." &>>$log
/usr/local/nginx/sbin/nginx &>>$log
WebPort=`netstat -nlt |grep 80|awk '{print $4}'| cut -d : -f 2`
if [ "$WebPort" = "80" ];then
echo "Nginx service has started" &>>$log
echo "The directory of the nginx configuration file is '/usr/local/nginx/conf/nginx.conf'" &>>$log
echo "Nginx startup file directory at '/usr/local/nginx/sbin/nginx'" &>>$log
echo "Nginx The default port of nginx is '80'" &>>$log
echo "Nginx logs error and access file directory is /usr/local/nginx/logs" &>>$log
echo "Please use the following browser to access the test:http://$IP"
echo "Please use the following browser to access the test:http://$IP" &>>$log
exit 0
else
echo "Please start nginx manually, starting up as: cd /usr/local/nginx/sbin && ./nginx " &>>$log
fi
}
Install_Yum(){
#配置网路YUM源
init_yum_rpm
#安装nginx
echo 'Begin install nginx ...' &>>$log
rm -rf /var/run/yum.pid &&yum install -y nginx net-tools&>>$log
wait
IP=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6 | awk '{print $2}' | tr -d "addr:"|sed -n 1p`
echo "Install nginx sucessfully ..." &>>$log
#启动nginx
echo 'Staring nginx ...' &>>$log
nginx -t
WebPort=`netstat -nlt |grep 80|awk '{print $4}'| cut -d : -f 2`
if [ "$WebPort" = "80" ];then
echo "Nginx service has started" &>>$log
echo "The directory of the nginx configuration file is '/etc/nginx/nginx.conf'" &>>$log
echo "The way to start nginx is'nginx -t'" &>>$log
echo "Nginx The default port of nginx is '80'" &>>$log
echo "Please use the following browser to access the test:'http://$IP'"
echo "Please use the following browser to access the test:'http://$IP'" &>>$log
echo "Nginx version is:$nginx_version" &>>$log
exit 1
else
echo "Please start nginx manually, starting up as: nginx -t " &>>$log
fi
}
menu_1() {
cat <<-EOF
+------------------------------------------------+
| |
| ====================== |
| 一键安装Nginx脚本(源码) |
| ====================== |
| Nginx版本:nginx-1.16.0 |
| Pcre版本:pcre-8.43 |
| Zlib版本:zlib-1.2.11 |
| Openssl版本:openssl-1.0.2r |
| |
+------------------------------------------------+
EOF
}
menu_2() {
cat <<-EOF
+----------------------------------------------------------+
| |
| ====================== |
| 一键安装Nginx脚本(yum) |
| ====================== |
| Nginx配置文件:/etc/nginx/nginx.conf |
| PID目录:/var/run/nginx.pid |
| 错误日志:/var/log/nginx/error.log |
| 访问日志:/var/log/nginx/access.log |
| 默认站点目录:/usr/share/nginx/html |
| 启动方式及重启:nginx -t;nginx -s reload |
+----------------------------------------------------------+
EOF
}
read -p "请选择相应的操作[1、采用源码的安装方式;2、采用yum的安装方式], 退出[q]: " choice
case $choice in
1)
menu_1 &>>$log
Install_Source_Code
echo "安装日志,请查看 $log"
exit
;;
2)
menu_2 &>>$log
Install_Yum
echo "安装日志,请查看 $log"
exit
;;
q)
exit
;;
'')
;;
*)
echo "输入错误!"
read -p "请选择相应的操作[1 or 2 or q]: " choice
esac