nginx.org 官方仓库安装与配置 NGINX
nginx安装
在 Ubuntu 22.04(Jammy)使用 nginx.org 官方仓库安装与配置 NGINX
-
采用 NGINX 官方软件仓库(nginx.org)提供的稳定版或主线版软件包
-
安装前准备与官方仓库添加
-
GPG 签名密钥与指纹校验
-
Stable 与 Mainline 的选择与切换
-
安装、启用、验证与常见问题排查
-
官方包与 Ubuntu 仓库包的关键差异
环境信息
- 操作系统:Ubuntu 22.04.5 LTS(jammy)
为什么使用 nginx.org 官方仓库
- 更及时的版本更新(特别是 Mainline)。
- 官方维护的构建选项与模块布局。
- 易于获得最新安全修复。
注意:官方包与 Ubuntu 仓库包在用户/组、目录结构等方面存在差异
1)安装前置依赖
sudo apt update
sudo apt install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring
2)导入官方 GPG 签名密钥
curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \| sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null
3)验证密钥指纹
官方指纹应为:573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
gpg --dry-run --quiet --no-keyring --import \--import-options import-show \/usr/share/keyrings/nginx-archive-keyring.gpg
4)添加 apt 源(Stable 推荐)
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
- 若需主线版(Mainline):
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
5)设置 Pin 优先级(优先使用 nginx.org 包)
echo -e "Package: *\nPin: origin nginx.org\nPin: release o=nginx\nPin-Priority: 900\n" \
| sudo tee /etc/apt/preferences.d/99nginx
6)安装 NGINX
sudo apt update
sudo apt install -y nginx
7)启动与开机自启
systemctl start nginx
systemctl status nginx
systemctl enable nginx
8)验证服务
nginx -v
systemctl is-active nginx && systemctl is-enabled nginx
curl -I http://127.0.0.1
# nginx -v
nginx version: nginx/1.28.0# systemctl is-active nginx
active# systemctl is-enabled nginx
enabled# curl -I http://127.0.0.1
HTTP/1.1 200 OK
Server: nginx/1.28.0
Date: Tue, 16 Sep 2025 02:33:44 GMT
Content-Type: text/html
Content-Length: 615
Last-Modified: Wed, 23 Apr 2025 11:48:54 GMT
Connection: keep-alive
ETag: "6808d3a6-267"
Accept-Ranges: bytes
官方包 vs Ubuntu 仓库包:关键差异
- 运行用户与组:
- 官方包:
nginx
- Ubuntu 仓库包:
www-data
- 官方包:
- 目录结构:
- 官方包默认站点根:
/usr/share/nginx/html
- 官方包虚拟主机默认文件:
/etc/nginx/conf.d/default.conf
- Ubuntu 包常用:
/etc/nginx/sites-available/
与sites-enabled/
结构
- 官方包默认站点根:
参考
- nginx.org