Skip to content
返回

Nginx 安装步骤(2026 最新持续补充)

一、Ubuntu / Debian(apt)

# 更新源
sudo apt update

# 安装
sudo apt install nginx -y

# 验证
nginx -v

启动 & 开机自启:

sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx

二、CentOS / RHEL / Rocky / AlmaLinux(yum)

# 安装 EPEL(CentOS 7 需要)
sudo yum install epel-release -y

# 安装
sudo yum install nginx -y

# 启动 & 开机自启
sudo systemctl start nginx
sudo systemctl enable nginx

三、官方最新主线版(Ubuntu/Debian)

官方源默认是稳定版,主线版功能更新更快:

# 安装依赖
sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring -y

# 导入官方签名key
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
  | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

# 添加主线版源
echo "deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
  http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \
  | sudo tee /etc/apt/sources.list.d/nginx.list

# 安装
sudo apt update
sudo apt install nginx -y

mainline 换成空就是稳定版源。


四、压缩包源码编译安装

适用于:需要自定义模块、指定安装路径、或系统没有包管理器的场景。

4.1 安装编译依赖

# Ubuntu / Debian
sudo apt update
sudo apt install build-essential libpcre3-dev zlib1g-dev libssl-dev -y

# CentOS / RHEL
sudo yum groupinstall "Development Tools" -y
sudo yum install pcre-devel zlib-devel openssl-devel -y

4.2 下载 & 解压

# 下载(去 https://nginx.org/en/download.html 查看最新版本号)
wget https://nginx.org/download/nginx-1.27.4.tar.gz

# 解压
tar -zxvf nginx-1.27.4.tar.gz
cd nginx-1.27.4

4.3 配置编译选项

# 基础配置(最常用)
./configure \
  --prefix=/usr/local/nginx \
  --with-http_ssl_module \
  --with-http_v2_module \
  --with-http_realip_module \
  --with-http_gzip_static_module \
  --with-http_stub_status_module \
  --with-stream \
  --with-stream_ssl_module

# 常用模块说明:
# --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 → 监控页面(stub_status)
# --with-stream                 → TCP/UDP 四层代理
# --with-stream_ssl_module      → stream 的 SSL 支持
# --prefix=/usr/local/nginx     → 安装路径(默认就是这个)

4.4 编译 & 安装

make
sudo make install

4.5 验证

/usr/local/nginx/sbin/nginx -v
/usr/local/nginx/sbin/nginx -t

4.6 注册为 systemd 服务

编译安装不会自动生成 systemd 配置,需要手动创建:

sudo 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

[Install]
WantedBy=multi-user.target
EOF

# 加载 & 启动
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx

五、Docker 方式(最干净)

docker run -d --name nginx \
  -p 80:80 -p 443:443 \
  -v /path/to/nginx.conf:/etc/nginx/nginx.conf:ro \
  -v /path/to/html:/usr/share/nginx/html:ro \
  nginx:latest

六、Windows(开发/测试用)

  1. 下载:https://nginx.org/en/download.html(选 Stable 或 Mainline 的 zip)
  2. 解压到 C:\nginx
  3. 启动:双击 nginx.exe 或命令行 start nginx
  4. 停止:nginx -s stop 重载:nginx -s reload

⚠️ Windows 版仅建议开发测试,生产环境用 Linux。


七、常用命令速查

7.1 服务管理(apt/yum 安装)

sudo systemctl start nginx       # 启动
sudo systemctl stop nginx        # 停止
sudo systemctl restart nginx     # 重启
sudo systemctl reload nginx      # 重载配置(不断连接)
sudo systemctl enable nginx      # 开机自启
sudo systemctl disable nginx     # 取消开机自启
sudo systemctl status nginx      # 查看状态

7.2 信号控制(通用,编译安装主要用这个)

nginx -s stop        # 快速停止(直接断开)
nginx -s quit        # 优雅停止(处理完当前请求再停)
nginx -s reload      # 重载配置
nginx -s reopen      # 重新打开日志文件(日志切割用)

7.3 检查 & 调试

nginx -t             # 测试配置文件语法(修改后必跑)
nginx -T             # 测试语法 + 打印完整生效配置(排查神器)
nginx -v             # 查看版本
nginx -V             # 查看版本 + 编译参数(查安装了哪些模块)

7.4 日志相关

# 日志默认位置
# apt/yum 安装:/var/log/nginx/
# 编译安装:/usr/local/nginx/logs/

# 实时查看访问日志
tail -f /var/log/nginx/access.log

# 实时查看错误日志
tail -f /var/log/nginx/error.log

# 日志切割:先 rename 再 reopen
sudo mv /var/log/nginx/access.log /var/log/nginx/access.log.$(date +%Y%m%d)
sudo nginx -s reopen

7.5 配置文件位置

安装方式主配置文件conf.d 目录
apt (Ubuntu/Debian)/etc/nginx/nginx.conf/etc/nginx/sites-enabled/
yum (CentOS/RHEL)/etc/nginx/nginx.conf/etc/nginx/conf.d/
编译安装(默认)/usr/local/nginx/conf/nginx.conf无,自行在 http{} 里 include
Docker/etc/nginx/nginx.conf/etc/nginx/conf.d/

八、常见问题 & 解决方案

#问题原因解决
1端口 80 被占用其他程序(Apache、IIS、残留 nginx 进程)占用了 80sudo lsof -i :80sudo ss -tlnp | grep :80 查占用的进程,kill 掉或改 nginx.conf 里的 listen 端口
2启动报 host not found配置里 server_name 写了无法解析的域名先改成本机可解析的域名或 IP,或在 /etc/hosts 加映射
3修改配置后没生效没有 reloadsudo nginx -t && sudo nginx -s reload永远先 -t 测试再 reload
4403 Forbidden网站根目录权限不对,或 index 文件不存在确认目录权限 chmod 755、文件存在、nginx.confuser 有读权限;检查 index 指令是否匹配实际文件名
5防火墙拦了ufw/firewalld 没放行sudo ufw allow 80/tcp && sudo ufw allow 443/tcp(Ubuntu)或 sudo firewall-cmd --permanent --add-service=http && sudo firewall-cmd --reload(CentOS)
6SELinux 阻止(CentOS/RHEL)SELinux 默认阻止 nginx 读取非标准目录sudo setenforce 0 临时关闭验证;永久方案:sudo semanage fcontext -a -t httpd_sys_content_t "/your/path(/.*)?" && sudo restorecon -Rv /your/path
7nginx -s reloadinvalid PID numbernginx 进程实际没跑起来或 PID 文件丢失sudo nginx -t 检查配置,然后 sudo systemctl restart nginx
8编译安装缺 PCRE/Zlib/OpenSSL源码编译时缺依赖sudo apt install libpcre3-dev zlib1g-dev libssl-dev -y(Debian系)或 sudo yum install pcre-devel zlib-devel openssl-devel -y(RHEL系)
9Windows 下 nginx -s reload 报错进程 PID 找不到或路径含中文/空格确保解压路径无中文和空格,用任务管理器确认 nginx.exe 在运行
10HTTPS 证书配置后浏览器不信任证书链不完整把中间证书拼到证书文件里:cat your.crt intermediate.crt > fullchain.crt,nginx 配 ssl_certificate fullchain.crt
11编译安装后 nginx 命令找不到二进制不在 PATH 里ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx,或加到 PATH
12编译后想加新模块新模块需要重新编译nginx -V 查看之前的 configure 参数,追加新模块重新 ./configure && make && sudo make install

九、快速验证清单

装完跑一遍:

nginx -t                         # 配置语法检查
curl -I http://localhost          # 本地访问测试
sudo systemctl status nginx       # 服务状态(apt/yum 安装)
# 或编译安装:
/usr/local/nginx/sbin/nginx -t

三个都通过,基本就 OK 了。


Share this post on:

Previous Post
AI来了,前端工程师到底有没有危机
Next Post
别再手动管理异步状态了!Pinia Colada 给 Vue 开发者带来的新选择