Ubuntu 默认 apt 源中的 Nginx 版本比较旧,今天介绍下如何在 Ubuntu 中安装最新版本的 Nginx。
要安装较新版本的 Nginx, 可以使用 Nginx 的 APT 源。执行如下脚本来添加 Nginx APT 源:
#!/bin/bash
release=$(lsb_release -cs)
cat <<EOF > /etc/apt/sources.list.d/nginx.list
deb https://nginx.org/packages/ubuntu/ $release nginx
deb-src https://nginx.org/packages/ubuntu/ $release nginx
EOF
之后更新 package 信息:
apt update
在执行上面的命令时,可能会遇到类似下面的报错信息:
W: GPG error: https://nginx.org/packages/ubuntu xenial InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY ABF5BD827BD9BF62
这是因为 APT 源中都会有一个 InRelease 文件[1],里面包含了 package 的 meta-information,通常这个文件都会通过私钥进行签名。在 apt update 命令执行过程中,通过公钥对这个签名做验证。如果本地不存在相应的公钥,就会报上面的错误。此时添加对应的 PUBKEY 就可以了:
apt-key adv --recv-key --keyserver keyserver.ubuntu.com ABF5BD