FILEEM

POWER OF DREAM

使用certbot自动申请续期SSL证书(Ubuntu)

《使用certbot自动申请续期SSL证书(Ubuntu)》

介绍

我们通过Certbot自动化完成SSL证书注册和配置文件修改步骤,从而在Web服务器上启用加密的HTTPS。目前,获取和安装证书的整个过程在Apache和Nginx上都是完全自动化的。
在本教程中,您将使用Certbot在Ubuntu 16.04上获取Nginx的Let’s Encrypt免费SSL证书,并将证书设置为自动续订。

先决条件

要学习本教程,您需要:
Ubuntu 16.04服务器
域名,本教程将example.com贯穿始终。
example.com指向服务器的公共IP地址的A记录。
在Ubuntu 16.04上安装Nginx。

第1步 – 安装Certbot

使用Let’s Encrypt获取SSL证书的第一步是在服务器上安装Certbot软件。
首先,添加存储库。

sudo add-apt-repository ppa:certbot/certbot

按ENTER接受。然后,更新包列表以获取新存储库的包信息。

sudo apt-get update

最后,apt-get安装Certbot的Nginx软件包。

sudo apt-get install python-certbot-nginx

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

第2步 – 设置Nginx

Certbot可以自动为Nginx配置SSL,但它需要能够server在配置中找到正确的块。它通过查找server_name与您请求证书的域匹配的指令来完成此操作。
如果您刚开始使用新的Nginx安装,则可以更新默认配置文件。用它nano或你喜欢的文本编辑器打开它。

sudo nano /etc/nginx/sites-available/default

找到现有server_name行并_使用您的域名替换下划线:
/etc/nginx的/网站可用/默认

server_name example.com www.example.com;

保存文件并退出编辑器。
然后,验证配置编辑的语法。

sudo nginx -t

如果您收到任何错误,请重新打开该文件并检查拼写错误,然后再次测试。
配置的语法正确后,重新加载Nginx以加载新配置。
sudo systemctl reload nginx
Certbot现在可以找到正确的server块并更新它。接下来,我们将更新防火墙以允许HTTPS流量。

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

如果ufw启用了防火墙,则必须按照先决条件指南的建议,调整设置以允许HTTPS流量。幸运的是,Nginx ufw在安装时注册了一些配置文件。
您可以键入以下内容来查看当前设置:

sudo ufw status

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

Output
Status: active
To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere<br />
Nginx HTTP                 ALLOW       Anywhere<br />
OpenSSH (v6)               ALLOW       Anywhere (v6)<br />
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

为了进一步允许HTTPS流量,我们可以允许Nginx Full配置文件,然后删除冗余Nginx HTTP配置文件限额:

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

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

sudo ufw status
Output
Status: active
To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx Full                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
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的加密服务器通信,然后运行质询以验证您是否控制了您要为其申请证书的域。
如果成功,certbot将询问您希望如何配置HTTPS设置。

Output
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this


change by editing your web server's configuration.

Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

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

Output
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
   /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will
   expire on 2017-10-23. To obtain a new or tweaked version of this
   certificate in the future, simply run certbot again with the
   "certonly" option. To non-interactively renew <em>all</em> of your
   certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

您的证书已下载,安装和加载。尝试使用重新加载您的网站并注意浏览器的安全指示器。它应该表明该网站已正确保护,通常带有绿色锁定图标。如果使用SSL Labs Server Test测试服务器,它将获得A级。
让我们通过测试更新过程来完成。

第5步 – 验证Certbot自动续订

我们的加密证书只有90天有效。这是为了鼓励用户自动执行证书续订过程。certbot我们安装的软件包通过systemd计时器每天运行两次“certbot renew”来为我们解决这个问题。在非系统发行版上,此功能由放置在其中的脚本提供/etc/cron.d。此任务每天运行两次,并将续订任何在到期后30天内的证书。
要测试更新过程,您可以执行以下操作certbot:

sudo certbot renew --dry-run

如果您没有看到任何错误,那么您已经完成了设置。必要时,Certbot将续订您的证书并重新加载Nginx以获取更改。如果自动续订过程失败,我们的加密将向您指定的电子邮件发送一条消息,并在您的证书即将过期时发出警告。

结论

在本教程中,您安装了Let’s Encrypt客户端certbot,为您的域下载了SSL证书,配置了Nginx以使用这些证书,并设置了自动证书续订。如果您对使用Certbot有其他疑问,他们的文档是一个很好的起点。

点赞