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

介绍

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

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

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

<$>[info] 讓 DigitalOcean 擔心管理 Nginx 和 Let's Encrypt. DigitalOcean App Platform將讓您在幾分鐘內直接從 GitHub 部署。

前提条件

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

一个Ubuntu 20.04 服务器设置通过以下 初始服务器设置为Ubuntu 20.04]教程,包括一个 sudo-enabled非root用户和防火墙。

  • 注册域名. 本教程将使用example.com在整个过程中。您可以从 Namecheap购买域名,获得一个免费的 Freenom,或使用您选择的域名注册器。
  • 以下两个 DNS 记录为您的服务器设置。 如果您正在使用 DigitalOcean,请参阅我们的 DNS 文档详细信息如何添加它们。
  • 一个记录与 example.com 指向您的

步骤1:安装Certbot

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

安装 Certbot 并使用apt的 Nginx 插件:

1sudo apt install certbot python3-certbot-nginx

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

步骤 2 — 确认 Nginx 的配置

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

如果您遵循了 Nginx 安装教程中的 [服务器块设置步骤](https://andsky.com/tech/tutorials/how-to-install-nginx-on-ubuntu-20-04#step-5-%E2%80%93-setting-up-server-blocks-(recommended),则您应该在 /etc/nginx/sites-available/example.com’ 对您的域名设有服务器块,并且已设置适当的 server_name` 指令。

要检查,请使用nano或您最喜欢的文本编辑器打开您的域的配置文件:

1sudo nano /etc/nginx/sites-available/example.com

查找现有的server_name行,它应该是这样的:

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

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

如果不是,则更新以匹配,然后保存文件,离开编辑器,并验证配置编辑的语法:

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'

你的状态现在应该是这样的:

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 example.com -d www.example.com

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

如果这是你第一次运行certbot,你将被要求输入电子邮件地址并同意服务条款. 这样做后,certbot将与 Let’s Encrypt 服务器进行通信,然后运行一个挑战,以验证你控制的域,你正在请求证书。

如果成功,certbot 会问你要如何配置你的 HTTPS 设置。

1[secondary_label Output]
2Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
3- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
41: No redirect - Make no further changes to the webserver configuration.
52: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
6new sites, or if you're confident your site works on HTTPS. You can undo this
7change by editing your web server's configuration.
8- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

选择你的选择,然后点击ENTER。配置将被更新,Nginx将重新加载以获取新的设置。

 1[secondary_label Output]
 2IMPORTANT NOTES:
 3 - Congratulations! Your certificate and chain have been saved at:
 4   /etc/letsencrypt/live/example.com/fullchain.pem
 5   Your key file has been saved at:
 6   /etc/letsencrypt/live/example.com/privkey.pem
 7   Your cert will expire on 2020-08-18. To obtain a new or tweaked
 8   version of this certificate in the future, simply run certbot again
 9   with the "certonly" option. To non-interactively renew *all* of
10   your certificates, run "certbot renew"
11 - If you like Certbot, please consider supporting our work by:
12
13   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
14   Donating to EFF:                    https://eff.org/donate-le

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

让我们通过测试更新过程来结束。

步骤 5 – 验证 Certbot 自动更新

Let’s Encrypt 的证书仅有效 90 天,这是为了鼓励用户自动更新证书的过程,我们安装的certbot包通过添加一个 systemd 计时器来为我们提供此功能,该计时器每天运行两次,并在到期三十天内自动更新任何证书。

您可以用systemctl查询计时器的状态:

1sudo systemctl status certbot.timer
1[secondary_label Output]
2 certbot.timer - Run certbot twice daily
3     Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled)
4     Active: active (waiting) since Mon 2020-05-04 20:04:36 UTC; 2 weeks 1 days ago
5    Trigger: Thu 2020-05-21 05:22:32 UTC; 9h left
6   Triggers:  certbot.service

要测试更新过程,您可以使用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