#!/bin/sh
# ---------------------------------------------
# AUTHOR:xiongch
# EMAIL:554853886@qq.com
# DATE:2022.11.01
# ---------------------------------------------
ng_user='www'
ng_group='www'
ngLogDir='/data/logs'
ngSslDir='/etc/nginx/conf.d/ssl'


# check if nginx installed
if_nginx=$(rpm -qa | grep nginx-1.*x86_64 | wc -l)
if [ ${if_nginx} -eq 1 ];then
  echo 'nginx is installed'
  exit
fi

#nginx user check
if_exit_user=$(grep ${ng_user} /etc/passwd | grep -v nginx |wc -l)
if [ ${if_exit_user} -eq 0 ];then
    echo "user ${ng_user} not fount"
    exit
fi
if_exit_group=$(grep ${ng_group} /etc/group | grep -v nginx |wc -l)
if [ ${if_exit_group} -eq 0 ];then
    echo "group ${ng_group} not fount"
    exit
fi

# disable selinux
/usr/bin/sed -i 's/^SELINUX=.*$/SELINUX=disabled/' /etc/selinux/config
setenforce 0
 
# install  nginx
[ -f /usr/bin/wget ] || yum install wget -y >/dev/null 
wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
if [ $? -ne 0 ];
    then 
    	echo 'Cann`t wget nginx rpm file' 
	exit
fi
yum install ./nginx-release-centos-7-0.el7.ngx.noarch.rpm -y >/dev/null
yum install nginx -y 
 
# nginx configure
if [ $? -ne 0 ]
    then
      echo "nginx install faild, Please Check out!"
      exit
else 
      #nginx配置
      echo 'nginx download ok , now config.....'
      rm -rf /etc/nginx/conf.d/default.conf
      echo '' > /etc/nginx/nginx.conf
      chown -R ${ng_user}:${ng_group} /var/log/nginx/
      if [ ! -d @{ngLogDir}/nginx ]
        then
          mkdir -p ${ngLogDir}/nginx
          chown -R ${ng_user}:${ng_group} ${ngLogDir}
          chown -R ${ng_user}:${ng_group} /etc/nginx/conf.d
      fi
      if [ ! -d ${ngSslDir} ]
        then
          mkdir -p ${ngSslDir}
          chown -R ${ng_user}:${ng_group} ${ngSslDir}
      fi

      cat >> /etc/nginx/nginx.conf <<EOF
user ${ng_user} ${ng_group};
worker_processes  auto;

error_log  ${ngLogDir}/nginx_error.log warn;
pid        /var/run/nginx.pid;
worker_rlimit_nofile 65535;

events {
    use   epoll;
    worker_connections  65535;
}
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
    server_tokens  off;
    log_format  main  '\$remote_addr - \$remote_user [\$time_local] \$http_host "\$request" '
                      '\$status \$request_time \$upstream_response_time \$body_bytes_sent "\$http_referer" '
                      '"\$http_user_agent" "\$http_x_forwarded_for"';

    access_log  ${ngLogDir}/nginx_access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;
    gzip  on;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        return 404;
    }

    include /etc/nginx/conf.d/*.conf;
}
EOF

# nginx log cut
echo 'config nginx cut log.....'
rm -rf /etc/logrotate.d/nginx
cat >> /etc/logrotate.d/nginx  <<EOF
/data/logs/nginx/*.log {
        daily
        missingok
        rotate 7
        compress
        delaycompress
        notifempty
        create 640 ${ng_user} ${ng_group}
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 \`cat /var/run/nginx.pid\`
                fi
        endscript
}
/var/log/nginx/*.log {
        daily
        missingok
        rotate 7
        compress
        delaycompress
        notifempty
        create 640 ${ng_user} ${ng_group}
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 \`cat /var/run/nginx.pid\`
                fi
        endscript
}



EOF

echo  "nginx相关参数配置完成！"

echo  "nginx正在启动，请稍等........"
      /bin/systemctl start nginx.service	 
      if [ "$?"  -eq 0 ]
        then
          echo 'nginx安装完成，服务启动正常。'
        else
          echo 'nginx安装完成，服务启动失败，请排除启动故障。'
          exit
      fi
      echo '设置nginx服务为开机启动模式'
      systemctl enable nginx.service 2>/dev/null
fi

