Files
tools/build-openresty.sh
2026-01-25 12:24:43 +08:00

129 lines
3.7 KiB
Bash

#!/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 "=== [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 "=== [6/8] 创建 Systemd 服务文件 ==="
cat > /etc/systemd/system/openresty.service <<EOF
[Unit]
Description=The NGINX HTTP and reverse proxy server with lua
After=syslog.target network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
ExecStartPre=${INSTALL_PREFIX}/bin/openresty -t
ExecStart=${INSTALL_PREFIX}/bin/openresty
ExecReload=${INSTALL_PREFIX}/bin/openresty -s reload
ExecStop=/bin/kill -s QUIT \$MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
# 重载 systemd
systemctl daemon-reload
echo "=== [7/8] 清理与启动 ==="
# 清理构建文件
cd ~
rm -rf "${WORK_DIR}"
echo "清理了临时目录: ${WORK_DIR}"
# 检查配置
echo "正在检查配置语法..."
${INSTALL_PREFIX}/bin/openresty -t
# 启动服务
echo "启动 OpenResty..."
systemctl enable openresty
systemctl restart openresty
echo "=== [8/8] 全部完成 ==="
systemctl status openresty --no-pager