Ubuntu 安装最新稳定版 Nginx


来自官方说明:https://nginx.org/en/linux_packages.html#Ubuntu。实践版本:Ubuntu 22.04

1、安装一些依赖

sudo apt install curl gnupg2 ca-certificates lsb-release ubuntu-keyring

2、导入官方 nginx 签名密钥,以便 apt 可以验证包的真实性

curl https://nginx.org/keys/nginx_signing.key | gpg --dearmor \
    | sudo tee /usr/share/keyrings/nginx-archive-keyring.gpg >/dev/null

3、验证下载的文件包含正确的密钥

gpg --dry-run --quiet --no-keyring --import --import-options import-show /usr/share/keyrings/nginx-archive-keyring.gpg

输出应包含完整指纹 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62,如下所示

pub   rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
      573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid                      nginx signing key <signing-key@nginx.com>

4、为稳定版 nginx 软件包设置 apt 存储库

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

5、安装 nginx

# 更新
sudo apt update
# 查看版本
apt-cache madison nginx
# 记住要安装的版本名称,如:1.24.0-1~jammy
# 安装指定版本
sudo apt install nginx=1.24.0-1~jammy
# 完成