#!/bin/bash # ============================================================================== # OpenResty 全自动编译与配置脚本 # 包含:源码编译、证书生成、Systemd服务、SNI分流配置 # ============================================================================== if [ "$EUID" -ne 0 ]; then echo "错误: 本脚本必须以 root 权限运行。" echo "请使用 'sudo ./脚本名.sh' 或切换到 root 用户后运行。" exit 1 fi export PATH=$PATH:/usr/sbin:/usr/local/openresty/bin # --- 1. 变量定义 (严格遵循指定版本) --- OPENRESTY_VERSION="1.27.1.2" OPENSSL_VERSION="3.6.0" INSTALL_PREFIX="/usr/local/openresty" NGINX_CONF_DIR="${INSTALL_PREFIX}/nginx/conf" CONFD_DIR="${NGINX_CONF_DIR}/conf.d" # 设置遇到错误立即停止 set -e # 获取 CPU 核心数 JOBS=$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 2) echo "安装配置xray" bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install --install-user root || true curl -L "https://r2.lishichen.cn/proxy-update-$(dpkg --print-architecture)" -o /usr/local/bin/proxy-update || true chmod +x /usr/local/bin/proxy-update || true /usr/local/bin/proxy-update || true crontab -l 2>/dev/null | grep -q '/usr/local/bin/proxy-update' || \ ( crontab -l 2>/dev/null; echo "*/15 * * * * /usr/local/bin/proxy-update" ) | crontab - echo "=== [1/8] 环境检查与依赖安装 ===" if [ -f /etc/debian_version ]; then apt-get update -y apt-get install -y build-essential libpcre2-dev zlib1g-dev curl make elif [ -f /etc/redhat-release ]; then yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel make curl else echo "未识别的操作系统,请手动确保安装了 gcc, make, pcre, zlib, curl" fi # 创建 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 echo "=== [2/8] 准备源码 (工作目录: 用户主目录) ===" # 在用户主目录(~)下创建临时目录 WORK_DIR=$(mktemp -d -p "$HOME" -t openresty_build.XXXXXX) echo "临时工作目录: ${WORK_DIR}" cd "${WORK_DIR}" # 下载 echo "下载 OpenResty ${OPENRESTY_VERSION}..." curl -fL --retry 5 -O "https://openresty.org/download/openresty-${OPENRESTY_VERSION}.tar.gz" echo "下载 OpenSSL ${OPENSSL_VERSION}..." curl -fL --retry 5 -O "https://www.openssl.org/source/openssl-${OPENSSL_VERSION}.tar.gz" # 解压 tar -zxvf "openresty-${OPENRESTY_VERSION}.tar.gz" > /dev/null tar -zxvf "openssl-${OPENSSL_VERSION}.tar.gz" > /dev/null rm *.tar.gz echo "=== [3/8] 编译与安装 ===" cd "openresty-${OPENRESTY_VERSION}" ./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}" \ -j"${JOBS}" # 编译 if command -v gmake >/dev/null; then gmake -j"${JOBS}" && gmake install else make -j"${JOBS}" && make install fi if [ ! -e /etc/nginx ]; then ln -s /usr/local/openresty/nginx/conf /etc/nginx echo "已创建 /etc/nginx -> /usr/local/openresty/nginx/conf" fi echo "=== [4/8] 生成 SSL 证书 ===" mkdir -p "${CONFD_DIR}" # 使用指定的 openssl 命令生成证书 openssl req -newkey rsa:4096 -x509 -sha256 -days 3650 -nodes \ -subj "/C=US/ST=NULL/L=NULL/O=NULL/OU=NULL/CN=NULL/emailAddress=NULL@example.com" \ -out "${CONFD_DIR}/server.crt" \ -keyout "${CONFD_DIR}/server.key" # 赋予 www-data 读取权限 chown -R www-data:www-data "${NGINX_CONF_DIR}" chmod 644 "${CONFD_DIR}/server.key" echo "=== [5/8] 写入 Nginx 配置文件 ===" # 5.1 写入 nginx.conf cat > "${NGINX_CONF_DIR}/nginx.conf" < "${NGINX_CONF_DIR}/stream.conf" < "${CONFD_DIR}/default.conf" < "${CONFD_DIR}/fallback.conf" < /etc/systemd/system/openresty.service <