# Nginx 安装最新版本

Last updated on 2019-08-24

# 解决依赖

[Ubuntu]
sudo apt install curl gnupg2 ca-certificates lsb-release

[CentOS]
yum -y install yum-utils

# Ubuntu

# 最新稳定版

echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

# 最新主线版本

echo "deb http://nginx.org/packages/mainline/ubuntu `lsb_release -cs` nginx" \
    | sudo tee /etc/apt/sources.list.d/nginx.list

# 导入签名

curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -

# 验证

sudo apt-key fingerprint ABF5BD827BD9BF62
  • 结果
pub   rsa2048 2011-08-19 [SC] [expires: 2024-06-14]
      573B FD6B 3D8F BC64 1079  A6AB ABF5 BD82 7BD9 BF62
uid   [ unknown] nginx signing key <signing-key@nginx.com>

# 安装 / 更新

sudo apt update
sudo apt install nginx

# CentOS

创建 YUM 仓库

vi /etc/yum.repos.d/nginx.repo

添加以下内容

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key

默认安装稳定版 Nginx,如果要使用主线版本,请运行以下命令:

yum-config-manager --enable nginx-mainline

安装 Nginx

yum install nginx

# 删除源

  • 这一步可选

如果有编译 Nginx 的需求则建议删除

[Ubuntu]
rm /etc/apt/sources.list.d/nginx.list

[Nginx]
rm /etc/yum.repos.d/nginx.repo

文章部分内容转载链接: