Ubuntu安装官方源的Nginx

发布于 2019-08-02 20:59:31 阅读 1061

为什么要安装官方源的软件?

本次安装的是nginx,所以我就用nginx举例。因为如果使用ubuntu官方自带的apt install的时候,可能会出现安装的nginx的版本过老,因为ubuntu自带的源的软件不是官方维护的,所以会出现隔代升级或者某个软件没有维护导致源中的软件停止不更新的情况。所以我们为了方便后面软件更新,所以安装官方源的nginx可以在下次nginx有更新的时候,直接只能apt upgrade nginx命令即可更新最新的nginx。

教程

首先,我们先去nginx的官网找到设置官方源的命令,地址点我

Pasted Graphic.png

根据上面地址的教程,首先,我们先去把nginx的源添加到ubuntu的源列表中

vim /etc/apt/sources.list

deb http://nginx.org/packages/ubuntu/ {ubuntu-release-code} nginx
deb-src http://nginx.org/packages/ubuntu/ {ubuntu-release-code} nginx

其中,将{ubuntu-release-code}替换成你的nginx的版本,本人的ubuntu是Ubuntu 18.04.2 LTS,所以我设置的是

deb http://nginx.org/packages/ubuntu/ bionic nginx
deb-src http://nginx.org/packages/ubuntu/ bionic nginx

具体的ubuntu版本对应的版本代号,请根据这个地址查询

Pasted Graphic 1.png

可以看到ubuntu 18.04.2 LTS对应的是bionic

此时执行

apt update

此时更新的时候回报错

Pasted Graphic 2.png

报的错误如下

Get:4 http://nginx.org/packages/ubuntu bionic InRelease [2847 B]
Err:4 http://nginx.org/packages/ubuntu bionic InRelease
  The following signatures couldn't be verified because the public key is not available: NO_PUBKEY ABF5BD827BD9BF62
Reading package lists... Done
W: GPG error: http://nginx.org/packages/ubuntu bionic InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY ABF5BD827BD9BF62
E: The repository 'http://nginx.org/packages/ubuntu bionic InRelease' is not signed.
N: Updating from such a repository can't be done securely, and is therefore disabled by default.
N: See apt-secure(8) manpage for repository creation and user configuration details.

这个是因为没有刚刚添加了这个nginx的源,没有安全证书,所以报错了。就像访问https的时候浏览器验证安全证书时出错,就认为你是非法访问,这个访问就被拒绝。所以我们需要安装一下nginx提供的安全证书

wget http://nginx.org/keys/nginx_signing.key
## 再执行
sudo apt-key add nginx_signing.key

这就算把nginx的证书安装到本机了,所以再次执行一下

apt update

此时应该不会报错了。

然后再执行

apt install nginx

Pasted Graphic 3.png

此时nginx就安装成功了。

总结

给大家总结下如何在ubuntu中安装官方源的软件。

  1. 先设置官方源
  2. 执行apt update(如果出错可能需要安装下源提供的验证证书)
  3. 然后apt install software-name