02 - 安装与部署 / Installation & Deployment
安装与部署 / Installation & Deployment
🟢 基础 / Basics — 快速安装 / Quick Install
Ubuntu / Debian
# 使用系统包管理器(最简单,但版本可能较旧)
sudo apt update
sudo apt install -y nginx
# 验证安装
nginx -v
# nginx version: nginx/1.24.0 (Ubuntu)
# 启动 & 设为开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
# 检查状态
sudo systemctl status nginx
CentOS / RHEL / Fedora
# CentOS 8+ / RHEL 8+ / Fedora
sudo dnf install -y nginx
# CentOS 7 / RHEL 7
sudo yum install -y epel-release
sudo yum install -y nginx
# 启动
sudo systemctl start nginx
sudo systemctl enable nginx
macOS
# 使用 Homebrew
brew install nginx
# 启动(macOS 使用 launchctl 或前台运行)
brew services start nginx
# 或
nginx
# 默认监听 8080 端口(非 80,因为 macOS 需要 root 权限绑定 80)
Windows
# 1. 从官网下载:http://nginx.org/en/download.html
# 2. 解压到目录,如 C:\nginx
# 3. 命令行启动
cd C:\nginx
start nginx
# 停止
nginx -s quit
验证安装
# 查看版本和编译参数
nginx -V
# 测试配置文件
sudo nginx -t
# 查看安装路径和模块
nginx -V 2>&1 | grep -o 'with-http_\w*'
默认文件布局(Ubuntu/Debian)
/etc/nginx/
├── nginx.conf # 主配置文件
├── conf.d/ # 额外配置目录(推荐在此放站点配置)
│ └── default.conf
├── sites-available/ # 可用站点(Debian 特有)
│ └── default
├── sites-enabled/ # 已启用站点(符号链接)
│ └── default -> ../sites-available/default
├── mime.types # MIME 类型映射
├── fastcgi_params # FastCGI 参数
└── modules-enabled/ # 动态模块
/var/www/html/ # 默认网站根目录
├── index.nginx-debian.html
└── ...
/var/log/nginx/ # 日志目录
├── access.log
└── error.log
🟡 进阶 / Intermediate — 官方仓库 & 编译安装 / Official Repo & Compile
使用 Nginx 官方仓库(推荐)
系统自带的 Nginx 版本通常较旧。使用官方仓库可以获得最新稳定版:
Ubuntu / Debian:
# 1. 安装依赖
sudo apt install -y curl gnupg2 ca-certificates lsb-release
# 2. 添加 Nginx 官方 GPG 密钥
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo gpg --dearmor -o /usr/share/keyrings/nginx-archive-keyring.gpg
# 3. 添加官方仓库(稳定版)
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
# 4. 安装
sudo apt update
sudo apt install -y nginx
# 验证
nginx -v
# nginx version: nginx/1.26.x ← 最新稳定版
CentOS / RHEL:
# 创建仓库文件
sudo tee /etc/yum.repos.d/nginx.repo <<'EOF'
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
sudo yum install -y nginx
注意: 官方仓库的配置文件布局与系统包略有不同,配置文件路径为
/etc/nginx/nginx.conf,站点配置在/etc/nginx/conf.d/。
从源码编译安装(深度定制)
当需要特定模块、特定优化参数或嵌入式环境时:
# 1. 安装编译依赖
sudo apt install -y build-essential libpcre3 libpcre3-dev \
zlib1g zlib1g-dev libssl-dev libgeoip-dev
# 2. 下载源码
cd /tmp
wget http://nginx.org/download/nginx-1.26.2.tar.gz
tar -xzf nginx-1.26.2.tar.gz
cd nginx-1.26.2
# 3. 配置编译选项
./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 \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_sub_module \
--with-stream \
--with-stream_ssl_module
# 4. 编译 & 安装
make -j$(nproc)
sudo make install
# 5. 创建 systemd 服务文件
sudo tee /etc/systemd/system/nginx.service <<'EOF'
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl start nginx
常用编译选项速查
| 选项 | 作用 |
|---|---|
--with-http_ssl_module | HTTPS 支持(生产必选) |
--with-http_v2_module | HTTP/2 支持 |
--with-http_realip_module | 获取真实客户端 IP(反代场景必选) |
--with-http_gzip_static_module | 预压缩静态文件 |
--with-http_stub_status_module | 状态监控页 |
--with-http_sub_module | 响应内容替换 |
--with-stream | TCP/UDP 代理(四层代理) |
--with-stream_ssl_module | TCP/UDP SSL 支持 |
--with-http_auth_basic_module | 基本认证 |
--with-http_dav_module | WebDAV 支持 |
--add-module=path | 添加第三方模块 |
--with-pcre-jit | PCRE JIT 编译(正则性能提升) |
动态模块 / Dynamic Modules
Nginx 1.9.11+ 支持动态模块,无需重新编译整个 Nginx:
# 安装动态模块(以 geoip2 为例)
sudo apt install -y libnginx-mod-http-geoip2
# 在 nginx.conf 中加载
# load_module modules/ngx_http_geoip2_module.so;
# 查看已加载的模块
nginx -V 2>&1 | grep -o 'with-http_\w*'
🔴 高级 / Advanced — 多实例 & 灰度升级 / Multi-Instance & Graceful Upgrade
平滑升级(零停机)/ Graceful Upgrade
升级 Nginx 二进制文件而不中断任何连接:
# 1. 备份旧二进制
sudo cp /usr/sbin/nginx /usr/sbin/nginx.old
# 2. 编译新版(参考上面的编译步骤)
# 3. 用新二进制替换旧的
sudo cp objs/nginx /usr/sbin/nginx
# 4. 发送 USR2 信号给 Master 进程
sudo kill -USR2 $(cat /var/run/nginx.pid)
# 此时新旧 Master 并存:
# - 旧 Master 继续处理已有连接
# - 新 Master 启动新的 Worker 处理新请求
# 5. 优雅关闭旧 Worker
sudo kill -WINCH $(cat /var/run/nginx.pid.oldbin)
# 6. 确认一切正常后,关闭旧 Master
sudo kill -QUIT $(cat /var/run/nginx.pid.oldbin)
# 如果出问题,可以回滚:
# sudo kill -HUP $(cat /var/run/nginx.pid.oldbin) # 恢复旧 Worker
# sudo kill -QUIT $(cat /var/run/nginx.pid) # 关闭新 Master
信号速查表 / Signal Reference:
| 信号 | 发送目标 | 作用 |
|---|---|---|
HUP | Master | 重载配置(平滑) |
QUIT | Master | 优雅关闭(等待请求完成) |
TERM/INT | Master | 立即关闭 |
USR1 | Master | 重新打开日志文件(日志切割) |
USR2 | Master | 热升级:启动新 Master |
WINCH | Master | 优雅关闭 Worker |
HUP | 旧 Master | 恢复旧 Worker(回滚) |
多实例部署(端口隔离)
在同一台机器上运行多个 Nginx 实例,互不干扰:
# 实例 1:Web 服务(端口 80)
# /etc/nginx/nginx.conf — 默认配置
# 实例 2:API 网关(端口 8080)
# /etc/nginx-api/nginx.conf
# /etc/nginx-api/nginx.conf
worker_processes auto;
pid /var/run/nginx-api.pid; # 不同的 PID 文件
error_log /var/log/nginx-api/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080; # 不同的端口
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
}
# 启动第二个实例
sudo nginx -c /etc/nginx-api/nginx.conf
# 停止
sudo nginx -c /etc/nginx-api/nginx.conf -s quit
Docker 部署 / Docker Deployment
# Dockerfile
FROM nginx:1.26-alpine
# 复制自定义配置
COPY nginx.conf /etc/nginx/nginx.conf
COPY conf.d/ /etc/nginx/conf.d/
# 复制网站文件
COPY html/ /usr/share/nginx/html/
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]
# docker-compose.yml
version: "3.8"
services:
nginx:
build: .
ports:
- "80:80"
- "443:443"
volumes:
- ./conf.d:/etc/nginx/conf.d:ro # 配置热更新
- ./html:/usr/share/nginx/html:ro # 静态文件
- ./certs:/etc/nginx/certs:ro # SSL 证书
- nginx-logs:/var/log/nginx # 日志持久化
restart: unless-stopped
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost/health"]
interval: 30s
timeout: 3s
retries: 3
volumes:
nginx-logs:
systemd 深入配置
# /etc/systemd/system/nginx.service.d/override.conf
# 限制资源 & 安全加固
[Service]
# 资源限制
LimitNOFILE=65535
LimitNPROC=4096
# 安全加固
ProtectSystem=full
ProtectHome=true
NoNewPrivileges=true
PrivateTmp=true
ReadWritePaths=/var/log/nginx /var/cache/nginx /var/run
# OOM 保护
OOMScoreAdjust=-500
sudo systemctl daemon-reload
sudo systemctl restart nginx
小结 / Summary
| 层级 | 你需要知道的 / What You Need to Know |
|---|---|
| 🟢 基础 | apt/yum install nginx,systemctl start/enable |
| 🟡 进阶 | 官方仓库获取最新版,源码编译定制模块 |
| 🔴 高级 | 平滑升级(USR2 信号),多实例,Docker 部署,systemd 安全加固 |