#!/bin/env bash
#最后一次修改时间：2024-11-15
#SYSTEM："CentOS 7 complie openresty"

pack_path="/usr/local/src/"
OpenResty_path="/usr/local/openresty/"
setenforce 0

# 检查操作系统是否为 CentOS 7
#os_id=$(grep -oP '(?<=^ID=")[^"]+' /etc/os-release)
#os_version=$(grep -oP '(?<=VERSION_ID=")[^"]+' /etc/os-release)
#if [ "$os_id" != "centos" ] || [ "$os_version" != "7" ]; then
#    echo -e "\e[31m此脚本仅支持 CentOS 7，脚本退出。\e[0m"
#    exit 1
#fi

# 创建cache目录
cache_dir="/data/cache"
if [ ! -d "$cache_dir" ]; then
    mkdir -p "$cache_dir"
    chmod 777 $cache_dir
fi

#先关闭nginx服务（防止出现意外）
systemctl stop nginx
systemctl disable nginx

# 安装依赖库 
yum install -y wget gcc gcc-c++ pcre pcre-devel openssl openssl-devel zlib zlib-devel gd gd-devel epel-release libmaxminddb brotli-devel libmaxminddb-devel

#准备vts(监控流量模块)
#cd ${pack_path}
#if [ ! -d ${pack_path}nginx-module-vts-0.2.2 ];then  
#    wget https://github.com/vozlt/nginx-module-vts/archive/refs/tags/v0.2.2.tar.gz
#    tar -xf  v0.2.2.tar.gz
#fi

#准备geoip2（处理不同国家城市的访问）
#mkdir /usr/local/nginx/modules/ -p && cd /usr/local/nginx/modules/
#if [ ! -d /usr/local/nginx/modules/ngx_http_geoip2_module-3.4 ];then  
#    wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz
#    tar -xf 3.4.tar.gz
#fi

#准备ip库（一个月一更新）
mkdir -p /usr/share/GeoIP/ && cd /usr/share/GeoIP/
if [ ! -f /usr/share/GeoIP/GeoLite2-Country.mmdb ] && [ ! -f /usr/share/GeoIP/GeoLite2-Country_latest.tar.gz ];then
    curl -sS -O http://deb.aorta.global/SuperCDN/Geo/GeoLite2-Country_latest.tar.gz
    tar -xf GeoLite2-Country_latest.tar.gz
    cp /usr/share/GeoIP/GeoLite2-Country_20*/GeoLite2-Country.mmdb /usr/share/GeoIP
fi

# 2024-11-15 修改为直接解压已经编译好的整个openresty目录
# 弃用下面的源码编译
cp /root/cdninstall/pkg/openresty.tgz /usr/local/ && cd /usr/local/ && tar -zxf openresty.tgz

# 下载openresty
#cd ${pack_path}
#if [ ! -f /usr/local/src/openresty-1.21.4.4.tar.gz ];then
#    wget https://openresty.org/download/openresty-1.21.4.4.tar.gz
#    tar -xf openresty-1.21.4.4.tar.gz
#fi

# 编译
# TODO: 找个时间这里最好做成rpm包或者整体下载,不要现场编译
#cd openresty-1.21.4.4
#useradd -s /sbin/nologin nginx
#if [ ! -d /usr/local/openresty/nginx ];then
#   ./configure --add-module=/usr/local/nginx/modules/ngx_http_geoip2_module-3.4 --prefix=/usr/local/openresty --user=nginx --group=nginx --with-pcre --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_image_filter_module --with-http_slice_module --with-mail --with-threads --with-file-aio --with-stream --with-mail_ssl_module --with-stream_ssl_module --add-module=/usr/local/src/nginx-module-vts-0.2.2
#   sleep 3 && make && make install
#fi
#if [[ $? -eq 0 ]];then
#    echo "OpenResty 编译成功"
#else
#    echo "OpenResty 编译失败...."
#    exit 1
#fi


# 配置文件替换
cd ${OpenResty_path}nginx/conf
cp -f /root/cdninstall/pkg/nginx_conf/nginx.conf ${OpenResty_path}nginx/conf/nginx.conf
cp -f /root/cdninstall/pkg/nginx_conf/gzip.conf ${OpenResty_path}nginx/conf/gzip.conf
cp -f /root/cdninstall/pkg/nginx_conf/brotli.conf ${OpenResty_path}nginx/conf/brotli.conf
cp -f /root/cdninstall/pkg/nginx_conf/zstd.conf ${OpenResty_path}nginx/conf/zstd.conf
cp -f /root/cdninstall/pkg/nginx_conf/cache.conf ${OpenResty_path}nginx/conf/cache.conf
cp -f /root/cdninstall/pkg/nginx_conf/server.conf ${OpenResty_path}nginx/conf/server.conf

# nginx配置default_server
cp -f /root/cdninstall/pkg/server.crt ${OpenResty_path}nginx/certs/
cp -f /root/cdninstall/pkg/server.key ${OpenResty_path}nginx/certs/

# default_server配置
cp -f /root/cdninstall/pkg/nginx_conf/default.conf ${OpenResty_path}nginx/conf.d/default.conf

# 验证openresty配置是否正确, 使用nginx -t 命令
output=$(/usr/local/openresty/nginx/sbin/nginx -t 2>&1)
if echo "$output" | grep -q  "successful"; then
    echo -e "\e[32mnginx -t 成功\e[0m"
else
    echo -e "\e[31mnginx -t 失败,退出\e[0m"
    exit 1
fi

#配置service文件
cat > /usr/lib/systemd/system/nginx.service <<'EOF'
[Unit]  
Description=The OpenResty HTTP and reverse proxy server  
After=network.target remote-fs.target nss-lookup.target  
  
[Service]  
Type=forking  
PIDFile=/usr/local/openresty/nginx/logs/nginx.pid  
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf  
ExecStart=/usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf  
ExecReload=/bin/kill -s HUP $MAINPID  
ExecStop=/bin/kill -s QUIT $MAINPID  
PrivateTmp=true  
  
[Install]  
WantedBy=multi-user.target
EOF
chmod +x /usr/lib/systemd/system/nginx.service

#配置环境变量
#CDN_DOMAIN_CONF变量
echo 'export CDN_DOMAIN_CONF=/usr/local/openresty/nginx' > /etc/profile.d/CDN_DOMAIN_CONF.sh
source /etc/profile.d/CDN_DOMAIN_CONF.sh 
echo $CDN_DOMAIN_CONF|grep -w nginx > /dev/null 2>&1
if [ $? -eq 0 ]; then
    echo -e "\e[32mCDN_DOMAIN_CONF环境配置成功\e[0m"
else
   echo -e "\e[31mCDN_DOMAIN_CONF环境配置失败\e[0m"
    exit 1  # 失败时返回 1
fi

#openresty变量
ln -s /usr/local/openresty/nginx/sbin/nginx /usr/bin/nginx  &>/dev/null
if [ -L /usr/bin/nginx ]; then
    echo -e "\e[32mopenresty环境配置成功\e[0m"
else
    echo -e "\e[31mopenresty环境配置失败\e[0m"
    exit 1
fi

#重载
systemctl daemon-reload

# systemctl方式启动openresty服务
systemctl enable --now nginx.service