#!/bin/bash
#########################
#create by AL  2017/10/16
#########################

##支持环境安装
#yum install -y gcc gcc-c++ perl
### Ubuntu下安装sysv-rc-conf报错：“E: Unable to locate package sysv-rc-conf ### 
### 参考文档:   https://blog.csdn.net/willingtolove/article/details/107494719 ###
echo "deb http://archive.ubuntu.com/ubuntu/ trusty main universe restricted multiverse"  >>  /etc/apt/sources.list
sudo apt update
apt install build-essential -y
apt-get -y install g++
apt install openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev make
apt install -y  unzip
apt-get install sysv-rc-conf -y

##添加用户和组
groupadd www -g 504
useradd -s /sbin/nologin -M www -u 504 -g 504

##下载安装包


soft_path=/root/software

if [ ! -d $soft_path ];then
        mkdir $soft_path
        cd $soft_path
else
        cd $soft_path
fi

wget http://8.210.81.239/linux/nginx/nginx.txt

if [ ! -f nginx-1.12.2.tar.gz ];then
        wget http://8.210.81.239/linux/nginx/nginx-1.12.2.tar.gz
fi

if  [ ! -f pcre-8.34.tar.gz ];then
wget http://8.210.81.239/linux/nginx/pcre-8.34.tar.gz
fi 

if  [ ! -f openssl-1.0.1c.tar.gz ];then
wget http://8.210.81.239/linux/nginx/openssl-1.0.1c.tar.gz
fi

if  [ ! -f zlib-1.2.8.tar.gz ];then
wget http://8.210.81.239/linux/nginx/zlib-1.2.8.tar.gz
fi

if  [ ! -f master.zip ];then
wget http://8.210.81.239/linux/nginx/master.zip
fi

##解压

tar zxf pcre-8.34.tar.gz
tar zxf openssl-1.0.1c.tar.gz
tar zxf zlib-1.2.8.tar.gz
tar zxf nginx-1.12.2.tar.gz
unzip master.zip

cd  nginx-1.12.2
patch -p1 < /root/software/nginx_upstream_check_module-master/check_1.12.1+.patch


./configure --prefix=/opt/nginx \
--user=www --group=www \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--without-http_map_module \
--without-http_geo_module \
--with-http_flv_module \
--with-http_realip_module \
--with-pcre=/root/software/pcre-8.34 \
--with-zlib=/root/software/zlib-1.2.8 \
--with-http_ssl_module \
--with-openssl=/root/software/openssl-1.0.1c \
--add-module=/root/software/nginx_upstream_check_module-master \
--with-debug 

sed -i s#-Werror##g  /root/software/nginx-1.12.2/objs/Makefile 
### Nginx编译错误：objs/src/os/unix/ngx_user.o ###
### 参考文档:  https://blog.csdn.net/tigaobansongjiahuan8/article/details/105277912 ###
sed -i '36d' /root/software/nginx-1.12.2/src/os/unix/ngx_user.c
###  编译nginx时几种常见错误  ###
### 参考文档: https://blog.csdn.net/lijunliang2017/article/details/120400666  ###

make && make install
###   without 是不安装的意思??  ###
### 注释掉 或者删除 --without-http_map_module ### 注释不行 ###
### 注释掉 或者删除 --without-http_geo_module ### 注释不行 ###
### 才是安装 ngx_http_map_module 模块 ###
### map 指令 ###
### 在 /root/software/nginx-1.12.2 里面重新编译 ###
### ./configure --prefix=/opt/nginx --user=www --group=www --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_realip_module --with-pcre=/root/software/pcre-8.34 --with-zlib=/root/software/zlib-1.2.8 --with-http_ssl_module --with-openssl=/root/software/openssl-1.0.1c --add-module=/root/software/nginx_upstream_check_module-master --with-debug  ### 

###   make && make install 重新编译安装 nginx ### 
###   参考文档:  https://www.cnblogs.com/effortsing/p/10012359.html   ###

if [ $? == 0 ];then
printf  '''
########################
nginx  install success
########################
'''
else

echo "Please check error"
fi

cat /root/software/nginx.txt >/etc/init.d/nginx


chmod +x /etc/init.d/nginx
#chkconfig --add nginx
#chkconfig --level 2345 nginx on
sysv-rc-conf --add nginx
sysv-rc-conf --level 2345 nginx on
### 参考文档: https://blog.csdn.net/liudongdong19/article/details/79774171 ###

cd /opt/nginx/conf
rm -f nginx.conf
wget http://8.210.81.239/linux/nginx/nginx.conf

echo -e "\033[31m 新建 /opt/nginx/conf/conf.d/ 目录\033[0m"
mkdir -p /opt/nginx/conf/conf.d/ 
echo -e "\033[31m 新建 /opt/nginx/conf/cert/ssl/ 目录\033[0m"
mkdir -p /opt/nginx/conf/cert/ssl/

echo -e "\033[31m 进入 /opt/nginx/conf/conf.d 目录\033[0m"
cd /opt/nginx/conf/conf.d
echo -e "\033[31m 下载 baidu.com.conf 配置文件  \033[0m"
echo -e "\033[31m 这个配置文件是转发去端口的  \033[0m"
wget http://8.210.81.239/linux/nginx/baidu.com.conf
echo -e "\033[31m 下载 GooGle.com.conf 配置文件  \033[0m"
echo -e "\033[31m 这个配置文件是转发去文件夹的  \033[0m"
cd /opt/nginx/conf/conf.d
wget http://8.210.81.239/linux/nginx/GooGle.com.conf

### 输出 Nginx 命令 ### 
echo "/opt/nginx/sbin/nginx ## 启动nginx"  >> /root/Nginx.TXT
echo "/opt/nginx/sbin/nginx -s stop ## 停止nginx"  >> /root/Nginx.TXT
echo "/opt/nginx/sbin/nginx -s restart  ## 重启nginx"  >> /root/Nginx.TXT
echo "/opt/nginx/sbin/nginx -s reload  ## 重载nginx配置文件"  >> /root/Nginx.TXT
echo "systemctl stop nginx  ## 停止nginx"  >> /root/Nginx.TXT
echo "systemctl start nginx  ## 启动nginx"  >> /root/Nginx.TXT
echo "systemctl restart nginx  ## 重启nginx"  >> /root/Nginx.TXT



