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

##支持环境安装
yum install -y gcc gcc-c++ perl
###  Ubuntu 安装 gcc ###
# sudo apt-get  install  build-essential ##

##添加用户和组
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 

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/rc.d/init.d/nginx


chmod +x /etc/rc.d/init.d/nginx
chkconfig --add nginx
chkconfig --level 2345 nginx on
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

