如何在 Debian 10 上使用 Let's Encrypt 加密 Nginx

介绍

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

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

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

前提条件

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

  • 一个 Debian 10 服务器,通过以下 Debian 10 初始服务器设置教程设置,以及一个sudo非根用户和防火墙。
  • 一个完全注册的域名. 您可以在 Namecheap上购买域名,在 Freenom上免费获取一个域名,或使用您选择的域名注册器。
  • 以下两个 DNS 记录为您的服务器设置。 您可以遵循 DigitalOcean DNS 的此介绍以获取如何添加域名的详细信息。
  • 一个记录与 your_domain 指向您的服务器的公共 IP 地址。

步骤1:安装Certbot

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

从 Debian 存储库中安装 python3-certbot-nginx 套件将使我们能够安装和使用 Cerbot 的 nginx 插件。与 Python 3 和 Python3-certbot-nginx 套件一起工作将增加我们的安装的寿命:Python 2 将于 2020 年 1 月 deprecated,因此我们的安装确保了与 Python 3 的兼容性。

在安装「python3-certbot-nginx」包之前,请更新您的包列表:

1sudo apt update

接下来,安装python3-certbot-nginx包的依赖,其中包括python3-acme,python3-certbot,python3-mock,python3-openssl,python3-pkg-resources,python3-pyparsingpython3-zope.interface包:

1sudo apt install python3-acme python3-certbot python3-mock python3-openssl python3-pkg-resources python3-pyparsing python3-zope.interface

最后,安装python3-certbot-nginx包:

1sudo apt install python3-certbot-nginx

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...

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

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

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 Full 配置文件并删除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 your_domain -d www.your_domain

这将运行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/your_domain/fullchain.pem
 5   Your key file has been saved at:
 6   /etc/letsencrypt/live/your_domain/privkey.pem
 7   Your cert will expire on 2019-10-08. 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 - Your account credentials have been saved in your Certbot
12   configuration directory at /etc/letsencrypt. You should make a
13   secure backup of this folder now. This configuration directory will
14   also contain certificates and private keys obtained by Certbot so
15   making regular backups of this folder is ideal.
16 - If you like Certbot, please consider supporting our work by:
17
18   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
19   Donating to EFF:                    https://eff.org/donate-le

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

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

步骤 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