如何在 Ubuntu 18.04 上使用 Let's Encrypt 加密 Nginx

介绍

Let's Encrypt 是一个证书授权机构(CA),它提供了获取和安装免费的 TLS/SSL 证书的方法,从而在 Web 服务器上允许加密 HTTPS。它通过提供软件客户端 Certbot 来简化过程,试图自动化大多数(如果不是所有)所需步骤。

在本教程中,您将使用 Certbot 在 Ubuntu 18.04 上获得免费的 Nginx SSL 证书,并设置您的证书自动更新。

本教程将使用单独的 Nginx 服务器封锁文件,而不是默认文件. 我们建议 为每个域创建新的 Nginx 服务器封锁文件,因为它有助于避免常见的错误,并将默认文件作为回归配置。

前提条件

要遵循本教程,您将需要:

一个Ubuntu 18.04服务器设置通过以下 初始服务器设置为Ubuntu 18.04 教程,包括一个 sudo非根用户和一个防火墙.

  • 一个完全注册的域名. 本教程将使用 your_domain 在整个过程中。您可以购买一个域名在 Namecheap,获得一个免费在 Freenom,或使用域注册器的您的选择。
  • 两个下列的 DNS 记录设置为您的服务器. 您可以遵循 这个介绍到DigitalOcean DNS 有关如何添加它们的细节。
  • 一个记录与 your_domain指向

步骤1:安装Certbot

使用 Let's Encrypt 获得 SSL 证书的第一步是将 Certbot 软件安装到您的服务器上。

该Certbot项目建议大多数用户通过snap安装软件,这是Canonical(Ubuntu背后的公司)开发的包管理器,现在可以在许多Linux发行版上使用:

1sudo snap install --classic certbot

您的输出将显示当前版本的 Certbot 和成功的安装:

1[secondary_label Output]
2
3certbot 1.21.0 from Certbot Project (certbot-eff✓) installed

接下来,创建一个符号链接到新安装的 /snap/bin/certbot 可执行的 /usr/bin/ 目录. 这将确保 certbot 命令可以在您的服务器上正确运行。 要做到这一点,运行下面的 ln 命令. 这包含 -s 旗帜,将创建一个符号或 soft 链接,而不是 hard link:

1sudo ln -s /snap/bin/certbot /usr/bin/certbot

Certbot 现在已经准备好使用,但为了为 Nginx 配置 SSL,您需要验证 Nginx 的某些配置。

步骤 2 — 确认 Nginx 的配置

Certbot 需要能够在您的 Nginx 配置中找到正确的服务器块,以便能够自动配置 SSL。

如果您遵循了 Nginx 安装教程中推荐的 服务器块设置步骤,则您将有服务器块为您的域名在 /etc/nginx/sites-available/your_domain,并且已设置适当的 server_name 指令。

要检查,请使用nano或您最喜欢的文本编辑器打开您的域名的服务器封锁文件:

1sudo nano /etc/nginx/sites-available/your_domain

查找现有的 server_name 行. 它应该看起来如下:

1[label  /etc/nginx/sites-available/your_domain]
2...
3server_name your_domain www.your_domain;
4...

如果是,请退出编辑器并转到下一步。

如果不是,更新以匹配,然后保存文件并离开编辑器. 如果你使用nano,你可以通过按CTRL + X然后YENTER来做到这一点。

现在检查您的配置编辑的语法:

1sudo nginx -t

如果您收到错误,请重新打开服务器封锁文件并检查任何字符或缺少的字符。一旦配置文件的语法是正确的,重新加载 Nginx 来加载新的配置:

1sudo systemctl reload nginx

Certbot 现在可以找到正确的服务器块并更新它。

接下来,您将更新防火墙以允许HTTPS流量。

步骤 3 – 通过防火墙允许 HTTPS

如果您已启用ufw防火墙,根据前提指南的建议,您将需要调整设置以允许HTTPS流量。

您可以通过运行以下操作来检查当前设置:

1sudo ufw status

您应该收到这样的输出,表示只有HTTP流量被允许到Web服务器:

1[secondary_label Output]
2Status: active
3
4To Action From
5--                         ------      ----
6OpenSSH ALLOW Anywhere                  
7Nginx HTTP ALLOW Anywhere                  
8OpenSSH (v6)               ALLOW Anywhere (v6)             
9Nginx HTTP (v6)            ALLOW Anywhere (v6)

若要允许额外的 HTTPS 流量,请允许 Nginx 完整配置文件并删除多余的 Nginx HTTP 配置文件许可:

1sudo ufw allow 'Nginx Full'
2sudo ufw delete allow 'Nginx HTTP'

现在,当您运行ufw status命令时,它将反映这些新规则:

1sudo ufw status
1[secondary_label Output]
2Status: active
3
4To Action From
5--                         ------      ----
6OpenSSH ALLOW Anywhere
7Nginx Full ALLOW Anywhere
8OpenSSH (v6)               ALLOW Anywhere (v6)
9Nginx Full (v6)            ALLOW Anywhere (v6)

接下来,您将运行Certbot并获取您的证书。

第4步:获取SSL证书

Certbot 提供了通过插件获取 SSL 证书的各种方式。 Nginx 插件将负责重新配置 Nginx 并在必要时重新加载配置。

1sudo certbot --nginx -d your_domain -d your_domain

这将运行certbot--nginx插件,使用-d来指定您希望证书有效的名称。

如果这是您第一次运行certbot,您将被要求输入电子邮件地址并同意服务条款。这样做后,certbot将与 Let’s Encrypt 服务器进行通信,以要求您的域名获得证书。

 1[secondary_label Output]
 2
 3Successfully received certificate.
 4Certificate is saved at: /etc/letsencrypt/live/your_domain/fullchain.pem
 5Key is saved at:         /etc/letsencrypt/live/your_domain/privkey.pem
 6This certificate expires on 2022-01-27.
 7These files will be updated when the certificate renews.
 8Certbot has set up a scheduled task to automatically renew this certificate in the background.
 9
10Deploying certificate
11Successfully deployed certificate for your_domain to /etc/nginx/sites-enabled/your_domain
12Successfully deployed certificate for www.your_domain to /etc/nginx/sites-enabled/your_domain
13Congratulations! You have successfully enabled HTTPS on https://your_domain and https://www.your_domain
14
15- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
16If you like Certbot, please consider supporting our work by:
17 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
18 * Donating to EFF:                    https://eff.org/donate-le
19- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

您的证书被下载、安装和加载。 尝试使用https://重新加载您的网站,并注意您的浏览器的安全指标。 它应该表明该网站是正确保护的,通常用绿色锁定图标。 如果您使用 SSL Labs Server Test测试您的服务器,它将获得 A等级。

现在你已经获得了SSL证书,最后一步是测试更新过程。

步骤 5 – 验证 Certbot 自动更新

Let's Encrypt 的证书仅有效 90 天,旨在鼓励用户自动更新证书过程. 您安装的 certbot 包通过将更新脚本添加到 `/etc/cron.d 来处理此问题。

要测试更新过程,您可以使用certbot进行干跑:

1sudo certbot renew --dry-run

如果你没有收到错误,你已经设置好了。当需要时,Certbot会更新你的证书并重新加载 Nginx 以获取更改。如果自动更新过程有时失败,Let’s Encrypt 将向你指定的电子邮件发送消息,通知你证书即将到期。

结论

在本教程中,您安装了 Let’s Encrypt 客户端certbot,下载了您的域的 SSL 证书,配置了 Nginx 以使用这些证书,并设置了自动证书更新。

Published At
Categories with 技术
comments powered by Disqus