在 AlmaLinux 9.7 上使用源码编译安装 Nginx,可以让你精确控制模块的启用,尤其是当需要官方仓库未包含的第三方模块(如 mod_zip)时。
下面提供完整的源码安装步骤,包含 SSL 模块(内置)以及可选的 mod_zip 模块(用于动态打包 ZIP 文件)。
📋 前提条件
确保系统已更新:
bash
sudo dnf update -y拥有
sudo权限的用户。
1️⃣ 安装编译依赖
编译 Nginx 需要基本开发工具和一些库:
bash
sudo dnf groupinstall -y "Development Tools"
sudo dnf install -y pcre-devel zlib-devel openssl-devel wget gitpcre-devel:支持正则表达式(用于 rewrite 模块)。zlib-devel:支持 gzip 压缩。openssl-devel:支持 SSL/TLS。
2️⃣ 创建 Nginx 用户和组
为了安全,Nginx 工作进程应该使用非特权用户运行:
bash
sudo groupadd -r nginx
sudo useradd -r -g nginx -s /sbin/nologin nginx3️⃣ 下载 Nginx 源码
建议使用官方最新稳定版(此处以 1.26.0 为例,实际请替换为最新版本号):
bash
cd /usr/local/src
sudo wget https://nginx.org/download/nginx-1.26.0.tar.gz
sudo tar -xzf nginx-1.26.0.tar.gz
cd nginx-1.26.04️⃣ (可选)下载第三方模块
如果你需要 mod_zip 模块(用于动态打包 ZIP 文件),请在编译前下载源码:
bash
cd /usr/local/src
sudo git clone https://github.com/evanmiller/mod_zip.git如果不使用 mod_zip,跳过此步。
5️⃣ 配置编译选项
执行 ./configure 指定安装路径、启用所需模块。
基础配置(包含 SSL 和 gzip)
bash
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-stream \
--with-stream_ssl_module关键选项说明:
--with-http_ssl_module:启用 SSL。--with-http_gzip_static_module:支持预压缩文件(gzip)。--with-http_v2_module:启用 HTTP/2。--with-stream和--with-stream_ssl_module:支持 TCP/UDP 代理及 SSL。
如果需要 mod_zip 模块(动态模块)
在上述配置中添加:
bash
--add-dynamic-module=/usr/local/src/mod_zip完整命令示例:
bash
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--modules-path=/usr/lib64/nginx/modules \
--conf-path=/etc/nginx/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-stream \
--with-stream_ssl_module \
--add-dynamic-module=/usr/local/src/mod_zip运行 ./configure 后,会输出一个摘要,确认 SSL 模块已包含、动态模块已添加。
6️⃣ 编译与安装
bash
make -j$(nproc)
sudo make install-j$(nproc)使用所有 CPU 核心并行编译,加快速度。
7️⃣ 验证安装
检查 Nginx 二进制文件及其版本:
bash
/usr/sbin/nginx -v # 版本号
/usr/sbin/nginx -V # 查看编译参数,应包含 --with-http_ssl_module如果启用了 mod_zip,使用 nginx -V 会看到 --add-dynamic-module 指向 mod_zip 的路径。
8️⃣ 创建 systemd 服务文件
为了方便管理,创建一个 systemd 单元文件:
bash
sudo tee /etc/systemd/system/nginx.service << 'EOF'
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF重载 systemd 配置:
bash
sudo systemctl daemon-reload9️⃣ 启动 Nginx 并设置开机自启
bash
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx # 确认状态为 active (running)🔥 防火墙配置
开放 HTTP(80)和 HTTPS(443)端口:
bash
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload🌐 测试访问
在浏览器中输入 http://你的服务器IP,如果看到 Welcome to nginx! 页面,说明安装成功。
🔒 配置 SSL 证书(可选)
源码安装后,你可以手动配置 SSL 证书,或使用 Certbot 自动获取 Let's Encrypt 证书。
Certbot 通常需要配合系统包管理器安装,但它可以修改你的 Nginx 配置。如果使用源码安装,Certbot 的 --nginx 插件可能无法自动找到 Nginx 路径,但你可以使用 certbot certonly --webroot 模式手动获取证书,然后手动修改 Nginx 配置。
如果你需要 Certbot 的帮助,可以另行咨询。
🧩 加载动态模块(如果启用了 mod_zip)
如果你编译时添加了 --add-dynamic-module,需要在主配置文件中加载该模块。
编辑 /etc/nginx/nginx.conf,在文件开头添加:
nginx
load_module modules/ngx_http_zip_module.so;然后测试配置并重载:
bash
sudo nginx -t
sudo systemctl reload nginx关于 mod_zip 的具体用法,请参考其官方文档。