#!/bin/bash

echo "nginx安装现在开始"
sleep 2
chown -R root:root `pwd`
#echo "请设置安装路径：格式:/\$dir/\$dir"
while read -p "请设置安装路径 (格式:/\$dir/\$dir):" DIR;do
  if [ "$DIR" = "" ];then
    echo "值为空，请重新输入"
  else
    echo "安装路径设置完成"  
    break
  fi
done

Dire(){
if [ ! -d "$DIR" ];then
  echo "开始建立目录"
  mkdir -p $DIR  
else
  echo "设置的安装目录已存在"
  sleep 2
  if [ "`ls $DIR`" != ""  ];then
    echo "设置的目录下存在文件,请检查"
    exit 10
  fi
fi
}

user(){
id nginx  &>/dev/null
if [ $? != 0 ];then
  echo "现在开始建立nginx用户"
  sleep 2
  useradd nginx
else
  echo "nginx用户已存在，继续安装"
  sleep 2
fi
}

Appli(){
ping -c3 baidu.com &>/dev/null
echo "现在检测网络连通性"
if [ $? -eq 0 ];then
  echo "网络连通正常"
else
  echo "网络连通异常，请检查"
  exit 10
fi
echo "现在检查yum源情况"
sleep 2
if [ -f -a "/etc/yum.repos.d/*.repo" ];then
  echo "yum源存在"
else
  echo "yum源不存在，请检查"
  exit 10
fi

echo "下面开始下载依赖插件"
sleep 2
yum -y install gcc-c++
}

confi(){
echo "下面开始进行编译安装"
tar xf openssl-1.1.1b.tar.gz
tar xf pcre-8.45.tar.gz
tar xf zlib-1.2.11.tar.gz
sleep 2
make clean
./configure \
--prefix=$DIR \
--sbin-path=$DIR/sbin/nginx  \
--modules-path=$DIR/modules \
--conf-path=$DIR/conf/nginx.conf \
--error-log-path=$DIR/log/error.log \
--http-log-path=$DIR/log/access.log  \
--pid-path=$DIR/nginx.pid  \
--lock-path=$DIR/nginx.lock  \
--http-client-body-temp-path=$DIR/client_temp \
--http-proxy-temp-path=$DIR/proxy_temp \
--http-fastcgi-temp-path=$DIR/fastcgi_temp \
--http-uwsgi-temp-path=$DIR/uwsgi_temp \
--http-scgi-temp-path=$DIR/scgi_temp \
--with-compat \
--user=nginx  \
--group=nginx \
--with-openssl=openssl-1.1.1b \
--with-pcre=pcre-8.45/ \
--with-zlib=zlib-1.2.11/ \
--with-file-aio \
--with-threads \
--with-http_addition_module \
--with-http_auth_request_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_mp4_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-http_v2_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream_realip_module \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \

make && make install
if [ $? != 0 ];then
  echo "!!编译失败，请检查，现在退出安装!!"
  exit 99
else
  echo "编译成功，现在继续安装"
fi
}

File(){
cat >$DIR/conf/nginx.conf <<EOF
user  nginx;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    server_tokens  off;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
    #access_log  logs/access.log  main;
    
    sendfile        on;
    #tcp_nopush     on;
    
    #keepalive_timeout  0;
    keepalive_timeout  65;
    
    include vhosts/*.conf;
    gzip  on;
    access_log  $DIR/log/access.log;
    error_log   $DIR/log/error.log;
    
#    server {
#        listen       80;
#        server_name  localhost;
#    
#        #charset koi8-r;
#    
#        #access_log  logs/host.access.log  main;
#    
#        location / {
#            root   html;
#            index  index.html index.htm;
#        }
#    
#        #error_page  404              /404.html;
#    
#        # redirect server error pages to the static page /50x.html
#        #
#        error_page   500 502 503 504  /50x.html;
#        location = /50x.html {
#            root   html;
#        }
#    }
}
EOF

mkdir $DIR/conf/vhosts
chown -R nginx:nginx $DIR
rm -rf {openssl-1.1.1b,pcre-8.45,zlib-1.2.11}
}


Tot(){
Dire
user
Appli
confi
File
}

Tot

$DIR/sbin/nginx -v &>version.log
VER=`awk '{print $3}' version.log | awk -F"/" '{print $2}'`
echo "nginx-${VER}已安装完成,请切换到nginx用户进行启动"
rm -rf version.log
sleep 2
#while read -p "nginx-1.18已安装完成,是否启动"[y/n]: CON;do
#if [ "$CON" = "y" ] || [ "$CON" = "" ];then
#  su - nginx <<EOF
#   /opt/nginx/sbin/nginx
#EOF
#  if [ $? -eq 0 ];then
#    echo "nginx启动成功"
#    sleep 2
#    break
#  else
#    echo "nginx启动失败,请检查"
#    exit 99
#  fi
#elif [ "$CON" = "n" ];then
#  echo "nginx安装完成,现在退出安装程序"
#  sleep 2
#  break
#else
#  echo "输入错误,请重新输入"
#fi
#done
