diff --git a/openresty.sh b/openresty.sh deleted file mode 100644 index f93b9f1..0000000 --- a/openresty.sh +++ /dev/null @@ -1,85 +0,0 @@ -#!/bin/bash -export PATH=$PATH:/usr/sbin - -# =================配置区域================= -# 严格按照你的要求定义变量 -OPENRESTY_VERSION="1.27.1.2" -OPENSSL_VERSION="3.6.0" -# ========================================= - -# 设置遇到错误立即停止 -set -e - -# 获取 CPU 核心数 -JOBS=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 2) - -echo "=== 准备编译 OpenResty ${OPENRESTY_VERSION} 与 OpenSSL ${OPENSSL_VERSION} ===" -echo "=== 使用 ${JOBS} 个线程进行编译 ===" - -# 0. 基础依赖检查 (简单提示) -if [ -f /etc/debian_version ]; then - echo "检测到 Debian/Ubuntu,建议安装: build-essential libpcre3-dev zlib1g-dev curl" -elif [ -f /etc/redhat-release ]; then - echo "检测到 CentOS/RHEL,建议安装: gcc gcc-c++ pcre-devel zlib-devel perl make curl" -fi - -# 1. 创建临时目录 (在用户主目录 ~ 下) -# 使用 mktemp 在 $HOME 下创建安全目录 -WORK_DIR=$(mktemp -d -p "$HOME" -t openresty_build.XXXXXX) -echo "=== 进入临时工作目录: ${WORK_DIR} ===" -cd "${WORK_DIR}" - -# 2. 下载源码 -echo "=== 正在下载源码... ===" -curl -fL --retry 5 -O "https://openresty.org/download/openresty-${OPENRESTY_VERSION}.tar.gz" -curl -fL --retry 5 -O "https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz" - -# 3. 解压源码 -echo "=== 正在解压源码... ===" -tar -zxvf "openresty-${OPENRESTY_VERSION}.tar.gz" -tar -zxvf "openssl-${OPENSSL_VERSION}.tar.gz" - -# 清理压缩包 -rm *.tar.gz - -# 4. 进入 OpenResty 目录 -cd "openresty-${OPENRESTY_VERSION}" - -# 5. 配置 (Configure) -# 严格使用你指定的参数 -echo "=== 开始配置 (Configure)... ===" -./configure \ - --with-http_ssl_module \ - --with-http_v2_module \ - --with-http_v3_module \ - --with-http_realip_module \ - --with-http_sub_module \ - --with-stream \ - --with-stream_ssl_module \ - --with-stream_ssl_preread_module \ - --with-openssl="../openssl-${OPENSSL_VERSION}" - -# 6. 编译 (Make) -echo "=== 开始编译 (Make)... ===" -if command -v gmake >/dev/null; then - gmake -j"${JOBS}" -else - make -j"${JOBS}" -fi - -# 7. 安装 (Install) -echo "=== 开始安装 (Install)... ===" -if command -v gmake >/dev/null; then - sudo gmake install -else - sudo make install -fi - -# 8. 清理 -echo "=== 清理临时文件... ===" -cd ~ -rm -rf "${WORK_DIR}" - -echo "==============================================" -echo "OpenResty ${OPENRESTY_VERSION} (with OpenSSL ${OPENSSL_VERSION}) 安装完成!" -echo "==============================================" \ No newline at end of file diff --git a/proxy-openresty.sh b/proxy-openresty.sh new file mode 100644 index 0000000..7eda1da --- /dev/null +++ b/proxy-openresty.sh @@ -0,0 +1,148 @@ +#!/bin/bash +export PATH=$PATH:/usr/sbin:/usr/local/openresty/bin + +# 遇到错误立即停止 +set -e + +CONF_DIR="/usr/local/openresty/nginx/conf" +CONFD_DIR="${CONF_DIR}/conf.d" + +echo "=== 开始配置 OpenResty 配置文件 ===" + +# 1. 确保 www-data 用户存在 +echo "-> 检查运行用户 www-data..." +if ! id -u www-data >/dev/null 2>&1; then + echo "创建 www-data 用户..." + groupadd www-data || true + useradd -M -s /usr/sbin/nologin -g www-data www-data || true +fi + +# 2. 写入 nginx.conf +echo "-> 写入主配置文件: ${CONF_DIR}/nginx.conf" +cat > "${CONF_DIR}/nginx.conf" < 写入流配置文件: ${CONF_DIR}/stream.conf" +cat > "${CONF_DIR}/stream.conf" < 写入站点配置文件: ${CONFD_DIR}/default.conf" +# 确保目录存在 +mkdir -p "${CONFD_DIR}" + +cat > "${CONFD_DIR}/default.conf" < 调整目录权限..." +# 确保 logs 目录存在且 www-data 可写 +mkdir -p /usr/local/openresty/nginx/logs +chown -R www-data:www-data /usr/local/openresty/nginx/logs +# 配置文件只要 root 可写,www-data 可读即可,保持默认安全权限 + +# 6. 检查配置并重载 +echo "-> 检查 Nginx 配置语法..." +/usr/local/openresty/bin/openresty -t + +if [ $? -eq 0 ]; then + echo "-> 语法检查通过,正在重启 OpenResty 服务..." + if systemctl is-active --quiet openresty; then + systemctl restart openresty + else + systemctl start openresty + fi + echo "=== 配置完成并已应用 ===" + systemctl status openresty --no-pager +else + echo "!!! 配置文件语法错误,请检查输出日志 !!!" + exit 1 +fi \ No newline at end of file