如何在 Ubuntu 20.04 上使用 Let's Encrypt 保护 Nginx

系统 Linux
在本教程中,您将使用 Certbot 在 Ubuntu 20.04 上为 Nginx 获取免费的 SSL 证书,并将您的证书设置为自动续订。

介绍

Let's Encrypt 是一个证书颁发机构 (CA),它提供了一种简单的方法来获取和安装免费的TLS/SSL 证书,从而在 Web 服务器上启用加密的 HTTPS。它通过提供软件客户端 Certbot 来简化流程,该客户端尝试自动执行大部分(如果不是全部)所需步骤。目前,获取和安装证书的整个过程在 Apache 和 Nginx 上都是完全自动化的。

在本教程中,您将使用 Certbot 在 Ubuntu 20.04 上为 Nginx 获取免费的 SSL 证书,并将您的证书设置为自动续订。

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

[[441191]]

先决条件

要学习本教程,您需要:

  •  设置一个 Ubuntu 20.04 服务器,包括启用 sudo 的非root用户和防火墙。
  •  一个注册的域名。本教程将example.com贯穿始终。
  •  为您的服务器设置的以下两个 DNS 记录。
  •      example.com指向您服务器的公共 IP 地址的 A 记录。
  •      指向您服务器的公共 IP 地址的 A 记录。www.example.com
  •  按照如何在 Ubuntu 20.04 上安装 Nginx 安装 Nginx。确保您的域有一个服务器块。本教程将用作示例。/etc/nginx/sites-available/example.com

步骤 1 - 安装 Certbot

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

安装 Certbot 和它的 Nginx 插件apt:

  • sudo apt install certbot python3-certbot-nginx

Certbot 现在可以使用了,但是为了让它自动为 Nginx 配置 SSL,我们需要验证 Nginx 的一些配置。

步骤 2 — 确认 Nginx 的配置

Certbot 需要能够server在您的 Nginx 配置中找到正确的块才能自动配置 SSL。具体来说,它通过查找server_name与您为其申请证书的域匹配的指令来实现这一点。

如果您按照Nginx 安装教程中的服务器块设置步骤进行操作,则您的域中应该有一个服务器块,并且已正确设置了指令。/etc/nginx/sites-available/example.comserver_name

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

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

找到现有的server_name行。它应该是这样的:

/etc/nginx/sites-available/example.com 

  1. ...  
  2. server_name example.com www.example.com;  
  3. ... 

如果是,请退出编辑器并继续下一步。

如果不是,请更新它以匹配。然后保存文件,退出编辑器,并验证配置编辑的语法:

  •  sudo nginx -t

如果出现错误,请重新打开服务器块文件并检查是否有任何拼写错误或缺失字符。配置文件的语法正确后,重新加载 Nginx 以加载新配置:

  • sudo systemctl reload nginx

Certbot 现在可以找到正确的server块并自动更新它。

接下来,让我们更新防火墙以允许 HTTPS 流量。

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

如果您ufw按照先决条件指南的建议启用了防火墙,则需要调整设置以允许 HTTPS 流量。幸运的是,Nginxufw在安装时注册了一些配置文件。

您可以通过键入以下内容查看当前设置:

  •  sudo ufw status

它可能看起来像这样,这意味着 Web 服务器只允许 HTTP 流量: 

  1. Status: active  
  2. To                         Action      From  
  3. --                         ------      ----  
  4. OpenSSH                    ALLOW       Anywhere                  
  5. Nginx HTTP                 ALLOW       Anywhere                
  6. OpenSSH (v6)               ALLOW       Anywhere (v6)          
  7. Nginx HTTP (v6)            ALLOW       Anywhere (v6)  

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

  •  sudo ufw allow 'Nginx Full'
  •  sudo ufw delete allow 'Nginx HTTP'

您现在的状态应如下所示:

  •  sudo ufw status 
  1. Status: active  
  2. To                         Action      From  
  3. --                         ------      ----  
  4. OpenSSH                    ALLOW       Anywhere  
  5. Nginx Full                 ALLOW       Anywhere  
  6. OpenSSH (v6)               ALLOW       Anywhere (v6)  
  7. Nginx Full (v6)            ALLOW       Anywhere (v6) 

接下来,让我们运行 Certbot 并获取我们的证书。

步骤 4 - 获取 SSL 证书

Certbot 提供了多种通过插件获取 SSL 证书的方式。Nginx 插件将负责重新配置 Nginx 并在必要时重新加载配置。要使用此插件,请键入以下内容:

  •  sudo certbot --nginx -d example.com -d www.example.com

这certbot与--nginx插件一起运行,-d用于指定我们希望证书有效的域名。

如果这是您第一次运行certbot,系统会提示您输入电子邮件地址并同意服务条款。执行此操作后,certbot将与 Let's Encrypt 服务器通信,然后运行质询以验证您是否控制要为其申请证书的域。

如果成功,certbot将询问您希望如何配置 HTTPS 设置。 

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

选择您的选择,然后点击ENTER。配置将被更新,Nginx 将重新加载以获取新设置。certbot将以一条消息结束,告诉您该过程已成功以及您的证书的存储位置: 

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

您的证书已下载、安装和加载。尝试使用重新加载您的网站https://并注意浏览器的安全指示器。它应该表明该站点已得到适当保护,通常带有锁定图标。如果您使用SSL Labs Server Test测试您的服务器,它将获得A级。

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

步骤 5 - 验证 Certbot 自动续订

Let's Encrypt 的证书有效期只有九十天。这是为了鼓励用户自动化他们的证书更新过程。certbot我们安装的软件包通过添加一个每天运行两次的 systemd 计时器来为我们处理这个问题,并自动更新任何在到期后三十天内的证书。

您可以使用以下命令查询计时器的状态systemctl:

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

要测试更新过程,您可以使用以下命令进行试运行certbot:

  •   sudo certbot renew --dry-run

如果您没有看到任何错误,则说明一切就绪。必要时,Certbot 将更新您的证书并重新加载 Nginx 以获取更改。如果自动续订过程失败,Let's Encrypt 将向您指定的电子邮件发送一条消息,在您的证书即将到期时向您发出警告。

结论

在本教程中,您安装了 Let's Encrypt 客户端certbot,为您的域下载了 SSL 证书,将 Nginx 配置为使用这些证书,并设置了自动证书续订。如果您对 Certbot的使用还有其他疑问,可以从官方文档开始。 

 

责任编辑:庞桂玉 来源: Linux公社
相关推荐

2021-12-15 11:46:46

HTTP2Nginx

2018-04-08 09:00:00

Let's Encry加密解密

2022-05-29 17:37:39

LinuxUbuntuPHP

2020-07-14 20:25:46

UbuntuLinux桌面应用

2020-08-14 07:00:00

RudderIT工具

2022-03-17 14:56:26

KubernetesMinikubeLinux

2021-09-18 08:00:00

Python系统开发

2023-05-27 16:27:25

2020-10-17 13:04:35

UbuntuDeepin桌面环境

2022-09-01 08:37:50

Ubuntu发行版

2015-12-31 10:35:53

HTTPS 证书HTTPS网络协议

2021-12-16 16:50:47

NginxSSLLinux

2016-11-03 20:06:53

UbuntuGrafanaDocker

2016-01-15 09:56:44

LinuxUbuntuGlances

2017-08-03 10:36:08

UbuntuCertbotNGINX

2021-07-20 08:00:00

集群Elasticsear工具

2018-02-25 09:48:36

LinuxUbuntu文件系统

2019-09-11 14:30:45

UbuntuLinuxR语言

2017-02-27 18:26:45

UbuntuADBAndroid

2018-02-25 11:03:00

LinuxUbuntuEncryptpad
点赞
收藏

51CTO技术栈公众号