Ubuntu 安装 Nginx 官方源最新版

1、安装先决条件

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

2、导入官方 nginx 签名密钥

获取密钥:

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

验证下载的文件是否包含正确的密钥:

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

输出:

pub   rsa4096 2024-05-29 [SC]
      8540A6F18833A80E9C1653A42FD21310B49F6B46
uid                      nginx signing key <signing-key-2@nginx.com>

pub   rsa2048 2011-08-19 [SC] [expires: 2027-05-24]
      573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62
uid                      nginx signing key <signing-key@nginx.com>

pub   rsa4096 2024-05-29 [SC]
      9E9BE90EACBCDE69FE9B204CBCDCD8A38D88A2B3
uid                      nginx signing key <signing-key-3@nginx.com>

为 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

默认情况下,安装稳定版(stable)nginx,如果要安装主线版本(mainline) nginx包,请运行以下命令:

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

3、安装nginx

sudo apt update
sudo apt install nginx

如果出现异常:

N: Skipping acquire of configured file 'nginx/binary-i386/Packages' as repository 'http://nginx.org/packages/mainline/ubuntu focal InRelease' doesn't support architecture 'i386'

修改 /etc/apt/sources.list.d/nginx.list 文件,加入 arch=amd64,如:

deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg] http://nginx.org/packages/ubuntu focal nginx

改为

deb [signed-by=/usr/share/keyrings/nginx-archive-keyring.gpg arch=amd64] http://nginx.org/packages/ubuntu focal nginx

修改完后再执行

sudo apt update
sudo apt install nginx

相关文献:https://askubuntu.com/questions/1446076/nginx-doesnt-support-architecture-i386

查看nginx版本

nginx -v

nginx version: nginx/1.24.0

3、启动 nginx

#启动
systemctl start nginx
#检查状态
systemctl status nginx
#设置自启动
systemctl enable nginx

到此,Nginx安装结束。

官方英文原文:https://nginx.org/en/linux_packages.html#Ubuntu