Ubuntu 22.04 安装 Nginx 指南(apt/官方源/编译多方案)
在 Ubuntu 22.04 上通过 apt 默认源、Nginx 官方源、源码编译三种方式安装 Nginx,并完成启动、放行端口与验证。本文所有命令与回显均为 Ubuntu 22.04.5 实测结果
Ubuntu 22.04 安装 Nginx 指南(apt/官方源/编译多方案)
目标:在 Ubuntu 22.04 上通过 apt 默认源、Nginx 官方源、源码编译三种方式安装 Nginx,并完成启动、放行端口与验证。本文所有命令与回显均为 Ubuntu 22.04.5 实测结果。
环境信息
- 操作系统:Ubuntu 22.04.5 LTS(x86_64)
- apt 默认 Nginx:nginx/1.18.0(Ubuntu)
- 构建依赖:gcc、make、libpcre3-dev、zlib1g-dev、libssl-dev
- 默认监听:0.0.0.0:80、[::]:80
- 配置文件根目录:/etc/nginx
- 主页目录:/var/www/html
版本选型说明
| 版本线 | 类型/来源 | 说明 |
|---|---|---|
| nginx 1.18.0 | Ubuntu apt 默认 | 跟随发行版,稳定保守,生产可用 |
| nginx stable 1.2x | nginx 官方源 stable 仓库 | 官方维护,修复及时 |
| nginx mainline 1.2x | nginx 官方源 mainline 仓库 | 主线版,新特性先上 |
| 指定源码包 | 官网源码编译 | 可定制模块与编译参数 |
💡 说明:生产环境若求稳选发行版源即可;需要官方修复/新特性选官方源;需要自定义模块(如第三方模块)则走源码编译。
部署步骤
方式一:apt 默认源安装(最简单,推荐的兜底)
apt-get update
apt-get install -y nginx
nginx -v
systemctl start nginx
systemctl enable nginx
实测输出:
nginx version: nginx/1.18.0 (Ubuntu)
💡 说明:apt 安装会自带动态模块(
/usr/lib/nginx/modules/)、sites-available/sites-enabled方案目录与snippets,适合直接用发行版维护的分层配置。
方式二:Nginx 官方源安装(最新 stable,推荐生产)
- 安装依赖与导入官方 GPG 密钥:
apt-get install -y curl gnupg2 ca-certificates lsb-release ubuntu-keyring
curl -fsSL https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
-o /usr/share/keyrings/nginx-archive-keyring.gpg
- 添加官方源(Ubuntu 22.04 代号 jammy;主线版将
ubuntu换为mainline/ubuntu):
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] \
http://nginx.org/packages/ubuntu $(lsb_release -cs) nginx" \
> /etc/apt/sources.list.d/nginx.list
- 设置源优先级,确保优先使用官方源:
cat > /etc/apt/preferences.d/99nginx <<'EOF'
Package: *
Pin: origin nginx.org
Pin-Priority: 900
EOF
- 若装过旧版先彻底清理,再安装:
apt-get remove --purge -y nginx nginx-common nginx-full nginx-core
apt-get update
apt-get install -y nginx
方式三:源码编译安装(可定制模块/编译参数)
# 1. 安装编译依赖
apt-get install -y build-essential libpcre3-dev zlib1g-dev libssl-dev
# 2. 下载并解压指定版本源码
curl -fsSL -o /tmp/nginx.tar.gz http://nginx.org/download/nginx-1.24.0.tar.gz
cd /tmp && tar -xzf nginx.tar.gz && cd nginx-1.24.0
# 3. 配置(按需增删 --with/-without 参数)
./configure --prefix=/usr/local/nginx --with-http_ssl_module \
--with-http_stub_status_module --with-http_v2_module --with-stream
# 4. 编译并安装
make -j$(nproc)
make install
# 5. 启动
/usr/local/nginx/sbin/nginx
⚠️ 注意:编译安装默认安装到
/usr/local/nginx,通常无 systemd 服务单元,需自行配置nginx.service或直接用二进制启动;--with-stream用于四层 TCP 转发,建议保留。
编译参数详解(./configure)
以下为常用 --with/--without 模块开关与路径参数(可用 nginx -V 查看当前二进制实际参数,Ubuntu 1.18 实测含 --with-http_ssl_module --with-http_stub_status_module --with-http_v2_module --with-http_realip_module --with-pcre-jit 等):
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module # HTTPS 必需
--with-http_v2_module # HTTP/2
--with-http_stub_status_module# /status 状态页
--with-http_realip_module # 取真实 IP(代理场景)
--with-http_gzip_static_module# 预压缩 .gz 直接下发
--with-pcre-jit # 正则加速
--with-stream # 四层 TCP/UDP 转发
--without-http_autoindex_module # 可关掉不需要的
常用配置项作用速查:
--prefix=:安装根目录(默认/usr/local/nginx);--with-http_ssl_module/--with-http_v2_module:HTTPS/HTTP2,生产必备;--with-http_stub_status_module:状态页监控;--with-stream:四层转发(MySQL/Redis 等 TCP);--with-compat:动态加载模块兼容(配合.so免重编译)。
⚠️ 说明:编译安装默认无 systemd 单元,需要自己写
nginx.service或用二进制直接启动;升级需重新./configure(参数保持一致)再 make。
离线安装(.deb 包,内网/无外网环境)
在不联网的内网机安装时,可先在联网机器下载 .deb 后带去安装:
# 联网机:只下载不安装
apt-get download nginx nginx-core nginx-common nginx-full
# 将下载的 .deb 拷到目标机后,本地安装(自动解析本地目录依赖)
dpkg -i *.deb
# 若提示缺依赖,用下列补齐(需依赖包已在目录或系统内)
apt-get install -y -f
💡 说明:Ubuntu 的 nginx 拆成
nginx-core/nginx-common/nginx-full等多个包;用apt-get download同名多次可把相关包都下下来。更通用的离线方案是先在一台同发行版机器apt-get install后用dpkg-repack打包。
配置说明
无论哪种方式,核心配置文件都在 /etc/nginx/nginx.conf(编译安装为 <prefix>/conf/nginx.conf)。分发行版安装会拆分 sites-available/sites-enabled;官方源安装用 conf.d/;编译安装则需按 -V 的路径组织。
注意事项
- Ubuntu 22.04 若所在环境不提供 systemd(如极简运行环境),需直接用
nginx二进制启动。 - 编译安装升级时需重新
./configure(保持原编译参数 + 新增参数)再make && make install,避免丢失原配置路径。 - 改配置后务必
nginx -t检查语法,通过后再nginx -s reload平滑重载,勿直接重启中断服务。
常见问题
nginx: [emerg] could not build the server_names_hash ...:服务器名 hash 冲突,在http块加server_names_hash_bucket_size 64;。- 80 端口被占用:
ss -ltnp | grep :80找到占用进程,先停 Apache 等再启动。 - 访问 403/404:检查
root指向的站点目录权限与目录是否存在,nginx 以www-data用户运行。 - 编译报
./configure: error: the HTTP rewrite module requires the PCRE library:apt-get install -y libpcre3-dev。 nginx: command not found(编译安装):确认PATH,或用绝对路径/usr/local/nginx/sbin/nginx。
卸载方式
# apt 安装卸载
apt-get remove --purge -y nginx nginx-common nginx-full nginx-core
rm -rf /etc/nginx
# 编译安装卸载(无 make uninstall,需手动清理)
rm -rf /usr/local/nginx
参考链接
- Nginx 官网下载:http://nginx.org/en/download.html
- Nginx 官方源配置:https://nginx.org/en/linux_packages.html
- Nginx 文档:https://nginx.org/en/docs/