#!/bin/bash
DIR=$( cd "$( dirname "$0" )" && pwd )
SRCDIR=/usr/local/src
nginxVersion="1.10.1"
nginxUser=nginx
nginxGroup=nginx
if [ $(id -u) -ne 0 ]; then
        echo "Please run as root"
        exit 1
fi
DISTRO=""
Distributions="CentOS Ubuntu Amazon Red"
for i in $Distributions
do
        cat /etc/*release |grep "$i" >/dev/null
        if [ $? -eq 0 ]; then
                DISTRO=$i
        fi
done
echo $DISTRO
egrep "^$nginxGroup" /etc/group >/dev/null
if [ $? -ne 0 ]; then
        groupadd -r $nginxGroup
fi
egrep "^$nginxUser" /etc/passwd >/dev/null
if [ $? -ne 0 ]; then
        [ $DISTRO = "Amazon" -o $DISTRO = "CentOS" ] && useradd -g $nginxGroup -s /sbin/nologin -M -r  $nginxUser
fi
cd $SRCDIR
[ ! -f nginx-$nginxVersion.tar.gz ] && wget http://nginx.org/download/nginx-$nginxVersion.tar.gz
tar -xzf nginx-$nginxVersion.tar.gz
cd nginx-$nginxVersion
## 添加第三方EPEL更新源
[ $DISTRO = "CentOS" ] && yum install -y epel-release
[ $DISTRO = "Red" ] && yum install -y http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm
## 依赖安装
if [ $DISTRO = "Amazon" -o $DISTRO = "CentOS"  -o $DISTRO = "Red" ]; then
        yum groupinstall -y "Development Tools"
        yum groupinstall -y "Development Libraries"
        yum install -y wget gcc gcc-c++ make zlib-devel pcre-devel openssl-devel libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed GeoIP-devel gperftools-devel
fi
./configure \
--prefix=/usr/local/nginx-$nginxVersion \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
--http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
--http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
--http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
--http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/subsys/nginx \
--user=$nginxUser \
--group=$nginxGroup \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_xslt_module \
--with-http_p_w_picpath_filter_module \
--with-http_geoip_module \
--with-http_dav_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_stub_status_module \
--with-http_perl_module \
--with-google_perftools_module \
--with-file-aio \
--with-pcre \
--with-ipv6 \
--with-debug \
--with-ld-opt="-Wl,-E"
if [ $? -eq 0 ]; then
        make && make install
else
        echo -e "\033[41;30m configured failed!!! \033[0m"
        exit 5
fi
cd /usr/local
ln -sv nginx-$nginxVersion nginx
ln -sv /usr/local/nginx/conf /etc/nginx
ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx
mkdir -p /var/lib/nginx/tmp/client_body