找回密码
 立即注册
搜索
查看: 5|回复: 0

mdserver-wb 面板 openresty 报错修复脚本/1.29.2 Centos7.9

[复制链接]

16

主题

0

回帖

112

积分

管理员

积分
112
发表于 2026-6-16 16:44:09 | 显示全部楼层 |阅读模式
  1. #!/bin/bash
  2. set -e
  3. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  4. export PATH

  5. # 不全局导出CFLAGS,防止nginx第三方模块编译严格报错

  6. # 使用方式:bash install.sh install
  7. # cd /www/server/mdserver-web/plugins/openresty && bash install.sh install 1.29.2

  8. curPath=`pwd`
  9. rootPath=$(dirname "$curPath")
  10. rootPath=$(dirname "$rootPath")
  11. serverPath=$(dirname "$rootPath")

  12. # 路径校验,防止变量为空
  13. if [ -z "${serverPath}" ];then
  14.     echo "错误:serverPath 路径解析失败,请在插件目录执行脚本!"
  15.     exit 1
  16. fi

  17. sysName=`uname`
  18. action=$1
  19. type=$2

  20. VERSION=1.29.2.4
  21. # CentOS7兼容OpenSSL版本,3.x无法编译
  22. opensslVersion="1.1.1w"
  23. libresslVersion="3.9.1"
  24. pcreVersion='8.45'

  25. openrestyDir=${serverPath}/source/openresty
  26. mkdir -p ${openrestyDir}

  27. Install_openresty()
  28. {
  29.     # 已安装直接退出
  30.         if [ "${action}" == "install" ];then
  31.                 if [ -d $serverPath/openresty ];then
  32.                         echo "OpenResty已存在,无需重复安装"
  33.                         exit 0
  34.                 fi
  35.         fi
  36.        
  37.         # ----- cpu核心计算修复,强制整数 ------
  38.         cpuCore="1"
  39.         if [ -f /proc/cpuinfo ];then
  40.                 cpuCore=`grep -c "processor" /proc/cpuinfo`
  41.         fi

  42.         MEM_INFO=$(free -m|grep Mem|awk '{print int($2/1024)}')
  43.         if [ "${cpuCore}" -gt "${MEM_INFO}" ] && [ "${MEM_INFO}" -gt 0 ];then
  44.             cpuCore="${MEM_INFO}"
  45.         fi

  46.         # 最大并发不超过物理核心*0.8 取整数
  47.         if [ "$cpuCore" -gt "2" ];then
  48.                 cpuCore=$(echo "$cpuCore * 0.8" | awk '{print int($1)}')
  49.         fi
  50.         # 最低1线程
  51.         [ "$cpuCore" -lt 1 ] && cpuCore=1
  52.         echo "编译并发线程数:${cpuCore}"
  53.         # ----- cpu end ------

  54.         echo '====================================='
  55.         echo '正在下载OpenResty源码包...'
  56.         echo '====================================='

  57.         # 下载逻辑增强,空文件自动重下3次
  58.         download_file() {
  59.         local file=$1
  60.         local url=$2
  61.         local retry=3
  62.         for ((i=0; i<retry; i++)); do
  63.             if [ -f ${file} ];then
  64.                 local fsize=`wc -c ${file} | awk '{print $1}'`
  65.                 if [ "$fsize" -gt 100000 ];then
  66.                     return 0
  67.                 fi
  68.                 rm -rf ${file}
  69.             fi
  70.             wget --no-check-certificate -O ${file} ${url} -T 5 --tries=2
  71.         done
  72.         if [ ! -f ${file} ] || [ `wc -c ${file}|awk '{print $1}'` -lt 100000 ];then
  73.             echo "源码包下载多次失败,请检查网络!文件:${file}"
  74.             exit 1
  75.         fi
  76.         }

  77.         OR_FILE=${openrestyDir}/openresty-${VERSION}.tar.gz
  78.         download_file ${OR_FILE} https://openresty.org/download/openresty-${VERSION}.tar.gz
  79.         # 备用镜像
  80.         if [ ! -f ${OR_FILE} ];then
  81.             download_file ${OR_FILE} http://dl.midoks.icu/soft/openresty/openresty-${VERSION}.tar.gz
  82.         fi

  83.         cd ${openrestyDir} && tar -zxvf openresty-${VERSION}.tar.gz
  84.         OPTIONS=''

  85.         # Darwin MacOS逻辑保留,CentOS7走下方Linux逻辑
  86.         if [ "$sysName" == "Darwin" ];then
  87.                 if [ ! -f ${openrestyDir}/pcre-${pcreVersion}.tar.gz ];then
  88.                         download_file ${openrestyDir}/pcre-${pcreVersion}.tar.gz https://netix.dl.sourceforge.net/project/pcre/pcre/${pcreVersion}/pcre-${pcreVersion}.tar.gz
  89.                 fi
  90.                 [ ! -d ${openrestyDir}/pcre-${pcreVersion} ] && cd ${openrestyDir} && tar -zxvf pcre-${pcreVersion}.tar.gz
  91.                 OPTIONS="${OPTIONS} --with-pcre=${openrestyDir}/pcre-${pcreVersion}"

  92.                 if [ ! -f ${openrestyDir}/openssl-${opensslVersion}.tar.gz ];then
  93.                 download_file ${openrestyDir}/openssl-${opensslVersion}.tar.gz https://www.openssl.org/source/openssl-${opensslVersion}.tar.gz
  94.             fi
  95.             [ ! -d ${openrestyDir}/openssl-${opensslVersion} ] && cd ${openrestyDir} && tar -zxvf openssl-${opensslVersion}.tar.gz
  96.             OPTIONS="${OPTIONS} --with-openssl=${openrestyDir}/openssl-${opensslVersion}"
  97.         else
  98.             # CentOS7 Linux编译ssl
  99.                 if [ ! -f ${openrestyDir}/openssl-${opensslVersion}.tar.gz ];then
  100.                 download_file ${openrestyDir}/openssl-${opensslVersion}.tar.gz https://www.openssl.org/source/openssl-${opensslVersion}.tar.gz
  101.             fi
  102.             [ ! -d ${openrestyDir}/openssl-${opensslVersion} ] && cd ${openrestyDir} && tar -zxvf openssl-${opensslVersion}.tar.gz
  103.                 OPTIONS="${OPTIONS} --with-openssl=${openrestyDir}/openssl-${opensslVersion}"
  104.         fi

  105.         # CentOS7禁用http_v3,openssl1.1.1不支持http3,开启会configure报错
  106.         if [[ "$VERSION" =~ "1.29.2" ]];then
  107.                 echo "CentOS7环境禁用HTTP/3模块(OpenSSL1.1.1不兼容)"
  108.         fi

  109.         # Brotli压缩模块
  110.         if [ ! -d ${openrestyDir}/ngx_brotli ];then
  111.                 cd ${openrestyDir} && git clone https://github.com/wxx9248/ngx_brotli.git
  112.                 cd ${openrestyDir}/ngx_brotli && git submodule update --init
  113.                 OPTIONS="${OPTIONS} --add-module=${openrestyDir}/ngx_brotli"
  114.         fi

  115.         OPTIONS="${OPTIONS} --with-threads"
  116.         OPTIONS="${OPTIONS} --with-pcre-jit"
  117.         OPTIONS="${OPTIONS} --with-http_gzip_static_module"

  118.         # Linux开启AIO
  119.         if [ "$sysName" != "Darwin" ];then
  120.                 OPTIONS="${OPTIONS} --with-file-aio"
  121.         fi

  122.         # ZSTD压缩模块 CentOS7修复
  123.         if [ ! -d ${openrestyDir}/zstd-nginx-module-master ];then
  124.                 ZSTD_TGZ=${openrestyDir}/zstd-nginx-module.tar.gz
  125.                 download_file ${ZSTD_TGZ} https://github.com/tokers/zstd-nginx-module/archive/refs/heads/master.tar.gz
  126.                 cd ${openrestyDir} && tar -zxvf zstd-nginx-module.tar.gz

  127.                 if [ "$sysName" != "Darwin" ];then
  128.                     unset ZSTD_INC ZSTD_LIB
  129.                 fi

  130.                 if pkg-config --exists libzstd;then
  131.                         OPTIONS="${OPTIONS} --add-module=${openrestyDir}/zstd-nginx-module-master"
  132.                         echo "已加载zstd压缩模块"
  133.                 else
  134.                     echo "警告:未检测到zstd-devel,跳过zstd模块编译"
  135.                 fi
  136.         fi
  137.        
  138.         echo '====================================='
  139.         echo '开始configure编译配置...'
  140.         echo '编译参数:' $OPTIONS
  141.         echo '====================================='

  142.         cd ${openrestyDir}/openresty-${VERSION} && ./configure \
  143.         --prefix=$serverPath/openresty \
  144.         $OPTIONS \
  145.         --with-stream \
  146.         --with-http_v2_module \
  147.         --with-http_ssl_module  \
  148.         --with-http_slice_module \
  149.         --with-http_stub_status_module \
  150.         --with-http_sub_module \
  151.         --with-http_realip_module

  152. # ============ 更好的 lua 子模块 CFLAGS 修复 ============
  153. echo "【修复 lua 子模块】强制添加 -std=gnu99 -fPIC"

  154. # lua-cjson
  155. find ${openrestyDir}/openresty-${VERSION}/build -name "lua-cjson-*" -type d 2>/dev/null | while read dir; do
  156.     makefiles=$(find "$dir" -name Makefile 2>/dev/null)
  157.     for mk in $makefiles; do
  158.         if grep -q "^CFLAGS" "$mk" || grep -q "CFLAGS *=" "$mk"; then
  159.             # 先删除已有的 -std= 选项,避免重复
  160.             sed -i 's/-std=[^ ]*//g' "$mk"
  161.             # 强制替换或追加 -std=gnu99 -fPIC
  162.             sed -i '/^CFLAGS *=/c\CFLAGS = -std=gnu99 -fPIC -O3 -Wall -pedantic -DNDEBUG -g' "$mk"
  163.             echo "已修复: $mk"
  164.         fi
  165.     done
  166. done

  167. # lua-resty-signal(同样处理)
  168. find ${openrestyDir}/openresty-${VERSION}/build -name "lua-resty-signal-*" -type d 2>/dev/null | while read dir; do
  169.     makefiles=$(find "$dir" -name Makefile 2>/dev/null)
  170.     for mk in $makefiles; do
  171.         if grep -q "^CFLAGS" "$mk" || grep -q "CFLAGS *=" "$mk"; then
  172.             sed -i 's/-std=[^ ]*//g' "$mk"
  173.             sed -i '/^CFLAGS *=/c\CFLAGS = -std=gnu99 -fPIC -O3 -Wall -pedantic -DNDEBUG -g' "$mk"
  174.             echo "已修复: $mk"
  175.         fi
  176.     done
  177. done
  178. # =======================================================================

  179.         echo '====================================='
  180.         echo '开始编译,nginx内核使用默认宽松编译,lua插件单独强制gnu99+fPIC,线程:'${cpuCore}
  181.         echo '====================================='
  182.         make -j${cpuCore}
  183.         make install
  184.         make clean

  185.         echo '====================================='
  186.         echo '清理编译源码缓存'
  187.         echo '====================================='
  188.     [ -d ${openrestyDir}/pcre-${pcreVersion} ] && rm -rf ${openrestyDir}/pcre-${pcreVersion}
  189.     [ -d ${openrestyDir}/openssl-${opensslVersion} ] && rm -rf ${openrestyDir}/openssl-${opensslVersion}
  190.     [ -d ${openrestyDir}/libressl-${libresslVersion} ] && rm -rf ${openrestyDir}/libressl-${libresslVersion}
  191.     [ -d $openrestyDir/openresty-${VERSION} ] && rm -rf $openrestyDir/openresty-${VERSION}

  192.         echo -e "\033[32mOpenResty ${VERSION} 安装完成!安装目录:${serverPath}/openresty\033[0m"
  193. }

  194. Uninstall_openresty()
  195. {
  196.         echo '卸载逻辑未实现,如需卸载手动删除目录:'$serverPath/openresty
  197. }

  198. # 入口判断
  199. if [ "${1}" == "install" ] || [ "${1}" == "upgrade" ];then
  200.         Install_openresty
  201. else
  202.         Uninstall_openresty
  203. fi
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|六时吉祥

GMT+8, 2026-6-24 15:10 , Processed in 0.127784 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表