手动编译nginx
文章目录
之前在nginx-acme模块无法申请证书解决方案这个文章里提到我是自己编译的nginx,最近看到一个段子
让我们想象一下你司架构是 NGINX v1.23.0 作为边缘节点,后面挂着 PHP-FPM 7.3 (刚花了几周时间从 5.6 升级上来) 的源站 + 前段时间刚花了大价钱采购来的 TiDB NewDB + Node.js 24 LTS Brotli 中间件,开启了 Brotil 压缩功能、Go 作为对外访问的网络沙箱、使用 Python + LiteLLM AI 企业内部 Harness 工具 (CI 昨晚刚从 PyPI 拉了新包并推送到了生产环境,现在全公司电脑都装上了这套 Harness)、公司服务器全部跑在 Linux 内核 c6e50df8 之前、网站的 HTTPS 证书由公司前段时间刚采购的无保险低价 Sectigo DV 证书(还没上 ACME 自动签发)、公司的 Vyper 智能合约部署在 Docker 29 + Kubernetes + Traefik Ingress 上开着 mTLS fail-open,宿主机还跑着 http://ASP.NET Core 面板和 .NET 本地服务、公司的软件和外发机密文件通过 emSign 代码签名,根证书库用 Go crypto/x509 + Rust webpki-roots + Python certifi
而你,是这家公司的核心 on-call 运维。
检查了一下我的nginx版本1.31.6,也就升级一下nginx版本好了,顺便把openssl之类也升级一下
完整脚本
apt remove nginx nginx-core nginx-common
apt autoremove --purge
which nginx
ls -l /usr/sbin/nginx
apt update
apt install -y build-essential git curl wget
apt install -y libpcre2-dev libssl-dev zlib1g-dev
apt install -y libbrotli-dev libmaxminddb-dev libjson-c-dev
apt install -y clang llvm-dev
apt install -y libzstd-dev
apt install -y libxml2-dev libxslt1-dev
apt install -y libgd-dev libgeoip-dev pkg-config
apt install -y libperl-dev
apt install -y libjpeg-dev libpng-dev libwebp-dev libfreetype-dev
apt install -y ca-certificates
apt install -y libgeoip1 libmaxminddb0
apt install -y libgd3
ldconfig
git clone https://github.com/google/ngx_brotli.git --depth 1
git clone https://github.com/leev/ngx_http_geoip2_module.git --depth 1
git clone https://github.com/vislee/ngx_http_ipdb_module.git --depth 1
git clone https://github.com/tokers/zstd-nginx-module.git --depth 1
git clone https://github.com/nginx/nginx-acme.git --depth 1
# wget https://github.com/openssl/openssl/releases/download/openssl-3.6.0/openssl-3.6.0.tar.gz
wget https://github.com/openssl/openssl/releases/download/openssl-4.0.3/openssl-4.0.3.tar.gz
cd ../ngx_brotli && git submodule update --init && cd /root/build/nginx
#git clone https://salsa.debian.org/nginx-team/nginx.git --depth 1
#git clone https://github.com/nginx/nginx.git --depth 1
# wget https://github.com/nginx/nginx/archive/refs/tags/release-1.29.3.tar.gz
# tar zxf release-1.29.3.tar.gz
# mv nginx-release-1.29.3 nginx
wget https://github.com/nginx/nginx/archive/refs/tags/release-1.31.8.tar.gz
tar zxf release-1.31.8.tar.gz
mv nginx-release-1.31.8 nginx
cd nginx
# 没有Makefile的时候先执行configure才会生成Makefile,后续就可以make clean了
if [ -f Makefile ]; then
make clean
fi
./auto/configure \
--with-openssl=../openssl \
--with-openssl-opt=enable-ktls \
--prefix=/usr/share/nginx \
--conf-path=/etc/nginx/nginx.conf \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--lock-path=/var/lock/nginx.lock \
--pid-path=/run/nginx.pid \
--modules-path=/usr/lib/nginx/modules \
--http-client-body-temp-path=/var/lib/nginx/body \
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
--http-proxy-temp-path=/var/lib/nginx/proxy \
--http-scgi-temp-path=/var/lib/nginx/scgi \
--http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
--with-compat \
--with-debug \
--with-pcre-jit \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-http_auth_request_module \
--with-http_v2_module \
--with-http_v3_module \
--with-http_dav_module \
--with-http_slice_module \
--with-threads \
--with-http_addition_module \
--with-http_flv_module \
--with-http_geoip_module=dynamic \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_image_filter_module=dynamic \
--with-http_mp4_module \
--with-http_perl_module=dynamic \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_sub_module \
--with-http_xslt_module=dynamic \
--with-mail=dynamic \
--with-mail_ssl_module \
--with-stream=dynamic \
--with-stream_geoip_module=dynamic \
--with-stream_ssl_module \
--with-stream_ssl_preread_module \
--add-module=../nginx-acme \
--add-module=../ngx_brotli \
--add-module=../zstd-nginx-module \
--add-dynamic-module=../ngx_http_geoip2_module \
--add-dynamic-module=../ngx_http_ipdb_module
make -j$(nproc) && make install
结果
验证打包出来的nginx版本和编译参数:/usr/share/nginx/sbin/nginx -V
打包发布脚本
以下脚本保存一个tar.sh,并执行chmod +x tar.sh && ./tar.sh,会把编译好的nginx和so文件打包一个tar.gz压缩文件。复制到要部署的服务器上解压后执行install-nginx.sh即可
#!/usr/bin/env bash
set -euo pipefail
VERSION="Nginx版本"
ARCH="$(uname -m)"
NGINX_BUILD="/root/build/nginx"
NGINX_BIN="/usr/share/nginx/sbin/nginx"
NGINX_MODULE_DIR="/usr/lib/nginx/modules"
PERL_BUILD_DIR="$NGINX_BUILD/objs/src/http/modules/perl"
PERL_VERSION_FILE="$PERL_BUILD_DIR/version"
PERL_NGINX_PM="$PERL_BUILD_DIR/blib/lib/nginx.pm"
PERL_NGINX_SO="$PERL_BUILD_DIR/blib/arch/auto/nginx/nginx.so"
ROOT="/tmp/nginx-${VERSION}-root"
OUTPUT="/tmp/nginx-${VERSION}-linux-${ARCH}.tar.gz"
rm -rf "$ROOT"
mkdir -p "$ROOT"
echo "==> nginx version"
if [ ! -x "$NGINX_BIN" ]; then
echo "ERROR: nginx binary not found:"
echo " $NGINX_BIN"
exit 1
fi
"$NGINX_BIN" -v
ACTUAL_VERSION="$("$NGINX_BIN" -v 2>&1 | sed 's#nginx version: nginx/##')"
if [ "$ACTUAL_VERSION" != "$VERSION" ]; then
echo "ERROR: nginx binary version mismatch"
echo "expected: $VERSION"
echo "actual: $ACTUAL_VERSION"
exit 1
fi
echo
echo "==> check Perl nginx build files"
if [ ! -f "$PERL_VERSION_FILE" ]; then
echo "ERROR: Perl version file not found:"
echo " $PERL_VERSION_FILE"
exit 1
fi
if [ ! -f "$PERL_NGINX_PM" ]; then
echo "ERROR: nginx.pm not found:"
echo " $PERL_NGINX_PM"
exit 1
fi
if [ ! -f "$PERL_NGINX_SO" ]; then
echo "ERROR: nginx Perl XS nginx.so not found:"
echo " $PERL_NGINX_SO"
exit 1
fi
PERL_VERSION="$(tr -d '[:space:]' < "$PERL_VERSION_FILE")"
echo "Perl build version:"
echo " $PERL_VERSION"
echo "nginx.pm:"
echo " $PERL_NGINX_PM"
echo "nginx.so:"
echo " $PERL_NGINX_SO"
if [ "$PERL_VERSION" != "$VERSION" ]; then
echo "ERROR: nginx Perl module version mismatch"
echo "expected: $VERSION"
echo "actual: $PERL_VERSION"
exit 1
fi
echo
echo "==> Verify nginx.pm contains correct version"
if ! grep -F "$VERSION" "$PERL_NGINX_PM" >/dev/null; then
echo "ERROR: nginx.pm does not contain version $VERSION"
echo
grep -n 'VERSION' "$PERL_NGINX_PM" || true
exit 1
fi
echo "nginx.pm version check OK"
echo
echo "==> Check Perl nginx.so unresolved nginx symbols"
#
# nginx.so 本身依赖 ngx_http_perl_module.so 提供的符号。
# 出现 U ngx_http_perl_* 是正常现象。
#
nm -D "$PERL_NGINX_SO" 2>/dev/null \
| grep 'ngx_http_perl_' \
|| true
echo
echo "==> Copy nginx binary"
mkdir -p "$ROOT/usr/share/nginx/sbin"
cp -a \
"$NGINX_BIN" \
"$ROOT/usr/share/nginx/sbin/nginx"
echo
echo "==> Copy nginx dynamic modules"
mkdir -p "$ROOT/usr/lib/nginx/modules"
if compgen -G "$NGINX_MODULE_DIR/*.so" > /dev/null; then
cp -a \
"$NGINX_MODULE_DIR"/*.so \
"$ROOT/usr/lib/nginx/modules/"
else
echo "WARNING: no installed modules found in:"
echo " $NGINX_MODULE_DIR"
echo
echo "Using build directory modules instead."
cp -a \
"$NGINX_BUILD"/objs/*.so \
"$ROOT/usr/lib/nginx/modules/" \
2>/dev/null || true
fi
#
# 确认 ngx_http_perl_module.so 已经进入发布包
#
if [ ! -f "$ROOT/usr/lib/nginx/modules/ngx_http_perl_module.so" ]; then
echo
echo "ERROR: ngx_http_perl_module.so not found in package"
exit 1
fi
echo
echo "==> Copy Perl nginx runtime"
mkdir -p "$ROOT/nginx-package/perl"
cp -a \
"$PERL_NGINX_PM" \
"$ROOT/nginx-package/perl/nginx.pm"
cp -a \
"$PERL_NGINX_SO" \
"$ROOT/nginx-package/perl/nginx.so"
cp -a \
"$PERL_VERSION_FILE" \
"$ROOT/nginx-package/perl/version"
echo
echo "==> Create install script"
cat > "$ROOT/install-nginx.sh" <<'EOF'
#!/usr/bin/env bash
set -euo pipefail
BASE_DIR="$(cd "$(dirname "$0")" && pwd)"
NGINX_BIN="/usr/share/nginx/sbin/nginx"
NGINX_MODULE_DIR="/usr/lib/nginx/modules"
NEW_VERSION="$(tr -d '[:space:]' < "$BASE_DIR/nginx-package/perl/version")"
echo "========================================"
echo "NGINX upgrade"
echo "Target version: $NEW_VERSION"
echo "========================================"
echo
echo "==> Detect Perl architecture directory"
#
# 先从已经安装的 nginx.pm 找安装路径。
#
OLD_NGINX_PM="$(
perl -e '
for my $dir (@INC) {
my $file = "$dir/nginx.pm";
if (-f $file) {
print $file;
last;
}
}
'
)"
if [ -n "$OLD_NGINX_PM" ]; then
PERL_ARCH_DIR="$(dirname "$OLD_NGINX_PM")"
echo "Existing nginx.pm:"
echo " $OLD_NGINX_PM"
else
#
# 第一次安装则使用 Perl sitearch
#
PERL_ARCH_DIR="$(
perl -MConfig -e 'print $Config{sitearch}'
)"
echo "Existing nginx.pm not found."
echo "Using sitearch:"
echo " $PERL_ARCH_DIR"
fi
PERL_NGINX_PM="$PERL_ARCH_DIR/nginx.pm"
PERL_NGINX_SO="$PERL_ARCH_DIR/auto/nginx/nginx.so"
echo
echo "==> Backup"
BACKUP="/root/nginx-backup-$(date +%Y%m%d-%H%M%S)"
mkdir -p "$BACKUP"
if [ -f "$NGINX_BIN" ]; then
mkdir -p "$BACKUP/usr/share/nginx/sbin"
cp -a \
"$NGINX_BIN" \
"$BACKUP/usr/share/nginx/sbin/"
fi
if [ -d "$NGINX_MODULE_DIR" ]; then
mkdir -p "$BACKUP/usr/lib/nginx"
cp -a \
"$NGINX_MODULE_DIR" \
"$BACKUP/usr/lib/nginx/"
fi
if [ -f "$PERL_NGINX_PM" ]; then
mkdir -p "$BACKUP/perl"
cp -a \
"$PERL_NGINX_PM" \
"$BACKUP/perl/nginx.pm"
fi
if [ -f "$PERL_NGINX_SO" ]; then
mkdir -p "$BACKUP/perl"
cp -a \
"$PERL_NGINX_SO" \
"$BACKUP/perl/nginx.so"
fi
echo
echo "==> Install nginx binary"
install -m 755 \
"$BASE_DIR/usr/share/nginx/sbin/nginx" \
"$NGINX_BIN"
echo
echo "==> Install nginx dynamic modules"
mkdir -p "$NGINX_MODULE_DIR"
cp -af \
"$BASE_DIR/usr/lib/nginx/modules/"*.so \
"$NGINX_MODULE_DIR/"
echo
echo "==> Install nginx Perl files"
mkdir -p \
"$PERL_ARCH_DIR/auto/nginx"
install -m 644 \
"$BASE_DIR/nginx-package/perl/nginx.pm" \
"$PERL_NGINX_PM"
install -m 755 \
"$BASE_DIR/nginx-package/perl/nginx.so" \
"$PERL_NGINX_SO"
echo
echo "==> nginx version"
"$NGINX_BIN" -v
echo
echo "==> nginx configuration test"
#
# 这里才是真正有效的 Perl 模块兼容性测试:
#
# nginx 会:
#
# nginx
# -> load ngx_http_perl_module.so
# -> initialize embedded Perl
# -> load nginx.pm
# -> load auto/nginx/nginx.so
#
# 因此不要使用 perl -Mnginx 单独测试。
#
if ! "$NGINX_BIN" -t; then
echo
echo "ERROR: nginx configuration test failed."
echo "Backup directory:"
echo " $BACKUP"
exit 1
fi
echo
echo "==> Restart nginx"
systemctl restart nginx
echo
echo "==> Service status"
systemctl --no-pager --full status nginx | head -30 || true
echo
echo "========================================"
echo "NGINX upgrade completed"
echo
echo "Version:"
"$NGINX_BIN" -v
echo
echo "Backup:"
echo " $BACKUP"
echo "========================================"
EOF
chmod +x "$ROOT/install-nginx.sh"
echo
echo "==> Generate SHA256SUMS"
(
cd "$ROOT"
find . \
-type f \
! -name SHA256SUMS \
-print0 \
| sort -z \
| xargs -0 sha256sum \
> SHA256SUMS
)
echo
echo "==> Package contents"
find "$ROOT" -type f | sort
echo
echo "==> Create tar package"
tar \
-C "$ROOT" \
-czf "$OUTPUT" \
.
echo
echo "========================================"
echo "Package created:"
echo " $OUTPUT"
echo
ls -lh "$OUTPUT"
echo "========================================"
相似文章
评论 · 0
文章作者 pengxiaochao
上次更新 2026-10-23
许可协议 不允许任何形式转载。
还没有评论,写下第一条吧。