源本科技 | 码上会

手动编译安装 Nginx

2026/01/20
90
0

学习目标

  • 掌握从 Nginx 官网下载源码包的正确方法

  • 理解并完成依赖安装、源码编译与安装全过程

  • 能够手动启动 Nginx 并验证其运行状态

  • 学会配置 systemd 服务实现开机自动启动


准备工作

安装编译依赖

Nginx 源码编译需要以下基础工具和库:

apt update
apt install -y build-essential libpcre3-dev zlib1g-dev libssl-dev

说明:

  • build-essential:包含 gcc、make 等编译工具

  • libpcre3-dev:支持正则表达式(用于 location 匹配)

  • zlib1g-dev:支持 gzip 压缩

  • libssl-dev:启用 HTTPS(SSL/TLS)支持


下载源码

https://nginx.org/en/download.html

选择 Stable version(稳定版)。以 Nginx 1.26.1 为例

# 创建下载目录
mkdir -p /usr/local/downloads

# 下载源码包(也可以在 Windows 上下载好后上传到 Linux)
wget https://nginx.org/download/nginx-1.26.1.tar.gz

# 验证文件完整性(可选)
ls -lh nginx-1.26.1.tar.gz

解压与配置编译

解压源码

tar -zxvf nginx-1.26.1.tar.gz
cd nginx-1.26.1

配置编译参数

运行 ./configure 脚本指定安装路径和模块:

./configure \
  --prefix=/usr/local/nginx \
  --sbin-path=/usr/local/nginx/sbin/nginx \
  --conf-path=/usr/local/nginx/conf/nginx.conf \
  --error-log-path=/usr/local/nginx/logs/error.log \
  --http-log-path=/usr/local/nginx/logs/access.log \
  --pid-path=/usr/local/nginx/logs/nginx.pid \
  --lock-path=/usr/local/nginx/logs/nginx.lock \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_realip_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --with-pcre \
  --with-file-aio \
  --with-threads

参数说明:

  • --prefix:主安装目录

  • --xxx-path:指定关键文件路径,便于管理

  • --with-http_ssl_module:启用 HTTPS 支持

  • --with-http_v2_module:支持 HTTP/2

  • --with-http_stub_status_module:开启状态监控页面(/status

执行后若无报错,将显示 Configuration summary,确认模块已启用。


编译与安装

# 编译(耗时约 1-5 分钟,取决于机器性能)
make

# 安装到指定目录
make install

安装完成后,Nginx 文件结构如下:

/usr/local/nginx/
├── conf/          # 配置文件
├── html/          # 默认网页根目录
├── logs/          # 日志文件(需手动创建)
└── sbin/          # 可执行文件 nginx

注意:logs 目录在安装后为空,首次启动时会自动创建日志文件。


启动与验证

启动 Nginx

/usr/local/nginx/sbin/nginx

验证是否运行

# 检查进程
ps aux | grep nginx

# 检查端口(默认监听 80)
sudo ss -tulnp | grep :80

# 访问本地测试页
curl http://localhost

若返回 Nginx 欢迎页面 HTML 内容,说明安装成功!

停止与重载

# 停止
/usr/local/nginx/sbin/nginx -s stop

# 优雅重载配置(不停机)
/usr/local/nginx/sbin/nginx -s reload

配置开机自启

现代 Linux 系统普遍使用 systemd 管理服务。我们将创建一个 Nginx 服务单元文件。

创建 systemd 服务文件

tee /etc/systemd/system/nginx.service <<'EOF'
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target
EOF

重载 systemd 配置并启用服务

# 重载 systemd 配置
sudo systemctl daemon-reload

# 启用开机自启
sudo systemctl enable nginx

# 启动服务
sudo systemctl start nginx

# 查看状态
sudo systemctl status nginx

现在,即使系统重启,Nginx 也会自动启动。


重点总结

  • 手动安装核心步骤:依赖 → 下载 → 配置 → 编译 → 安装 → 启动 → 自启

  • 所有文件集中于 /usr/local/nginx,便于隔离管理

  • 必须启用 --with-http_ssl_module 才能使用 HTTPS

  • 使用 systemd 服务而非传统 init 脚本,更符合现代 Linux 标准

  • 首次启动前确保 logs 目录存在,否则可能启动失败


思考题

  1. 如果你的服务器无法访问外网,如何将 Nginx 源码包从本地机器传输到服务器并完成安装?

  2. ./configure 阶段,如果不指定 --prefix,Nginx 默认会安装到哪个路径?

  3. 如何修改 Nginx 的默认监听端口(例如改为 8080),并在防火墙开放该端口?