#!/bin/bash

#作者:星云法师(头条号:西西图图---专注美食领域的研究)

#环境:centos7,如果是其它的系统可以相应做调整。
#--------选择安装方式,网络晚装还是本地安装--------------
read -p "Install nginx from internet or localpacke?answer i or l:" installway
installways=`echo $installway| tr [a-z] [A-Z]`
read -p "Install some need tools(Y/N):" needpack
needpacks=`echo $needpack| tr [a-z] [A-Z]`

#----是否要配置安装环境---------------------------
if [ "$needpacks" == "Y" ];then
    cd /etc/yum.repos.d
    mkdir -p /etc/yum.repo.bak
    mv /etc/yum.repos.d/* /etc/yum.repo.bak
    curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    yum clean all
    yum -y install openssh-clients wget gcc gcc-c++ install zlib zlib-devel openssl openssl-devel pcre-devel
elif [ "$needpacks" != "Y" ]&&[ "$needpacks" != "N" ];then
    echo "Input error!"
    exit 1
fi

#-----网络实际安装---------------------
if [ "$installways" == "I" ];then
    cd /tmp
    curl http://nginx.org/download/|awk -F'"' '{print $2}'|grep "nginx"|grep "tar"|awk -F'.tar' '{print $1}' >/tmp/nginxtmp.txt
    sort /tmp/nginxtmp.txt|uniq

    #-------选择安装版本
    read -p "which version you want to install(input all you see):" version
    wget http://nginx.org/download/$version".tar.gz"
    tar xf $version".tar.gz"
        if [ $? -eq 0 ];then
            cd $version/
            groupadd nginx
            mkdir -p /var/cache/nginx
            useradd nginx -g nginx -s /sbin/nologin -M
            ./configure --user=nginx --group=nginx --prefix=/etc/nginx  --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-http_ssl_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-debug --with-stream --with-file-aio --with-threads --with-http_auth_request_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-mail --with-mail_ssl_module
            if [ $? -eq 0 ];then
                make && make install
                if [ $? -eq 0 ];then
                    echo "the bin in /usr/sbin/"
                    echo "Next is cmd about control nginx:"
                    echo "check nginx snock by \"/usr/sbin/nginx -t\""
                    echo "start nginx by \"/usr/sbin/nginx\""
                    echo "stop nginx  by \"/usr/sbin/nginx -s stop\""
                    echo "reload nginx  by \"/usr/sbin/nginx -s reload\""
                fi
            fi
        fi

#------本地安装--------------------
elif [ "$installways" == "L" ];then

    #-----安装包来源主机---------------
    read -p "which ip your nginx packge from:" fromip

    #-----安装包在远程主机的路径---------------
    read -p "which path your packge in:" path

    #-----安装包版本,要全称--------------------
    read -p "which version your packge is(all name:nginx-1.14.2):" lversion

    if [ "$fromip" != "" ]&&[ "$path" != "" ];then
        destpath=$fromip:$path$lversion
        scp root@$destpath /usr/local
        cd /usr/local
        tar xf $lversion".tar.gz"
        if [ $? -eq 0 ];then
            cd $lversion/
            groupadd nginx
            useradd nginx -g nginx -s /sbin/nologin -M
            ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module
            if [ $? -eq 0 ];then
                make && make install
                if [ $? -eq 0 ];then
                    echo "the bin in /usr/local/nginx/sbin/"
                fi
            fi
        fi
    fi
elif [ "$installways" != "I" ]&&[ "$installways" != "L" ];then
    echo "Input error!";exit 1
fi