之前需要用https证书时都是取腾讯云那边弄,一开始是免费一年,现在变成了免费90天,然后一个号大概有50次免费机会,用的时候也麻烦,需要去腾讯云配置、改ssl解析、下载证书、部署证书。所以考虑换个方式—Let’s Encrypt

前言

环境: Ubuntu 20; Nginx
官方指引: https://certbot.eff.org/instructions?ws=nginx&os=ubuntufocal&tab=wildcard
参考教程: https://blog.csdn.net/smallbirdnq/article/details/132214790

先安装snapd

官方是推荐用这个的,咱也不知道为什么,省事就跟着做吧

sudo apt update
sudo apt install snapd

//测试安装
$ sudo snap install hello-world
hello-world 6.4 from Canonical✓ installed
$ hello-world
Hello World!

安装 Certbot

在该机器的命令行上运行此命令以安装 Certbot。

sudo snap install --classic certbot

准备 Certbot 命令

在机器的命令行上执行以下指令,以确保 certbot 命令可以运行。

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

确认插件包含级别

在机器上的命令行上运行此命令,以承认已安装的插件将拥有与 Certbot 快照相同的 classic 容器。

sudo snap set certbot trust-plugin-with-root=ok

安装正确的 DNS 插件

运行以下命令,将替换为您的 DNS 提供商的名称。

sudo snap install certbot-dns-<PLUGIN>

我的是腾讯云的域名,腾讯云是用的DNSPOD域名提供商,所以

sudo snap install certbot-dns-dnspod

运行 Certbot

sudo certbot --nginx

运行后会显示你当前 Nginx 中已部署的一些域名,然后根据编号选择对应域名(全部域名也行),就可以对其进行证书部署了

自动续期

为了证书不被过期,需要定期更新证书有效期,可以执行命令

sudo certbot renew --dry-run

此命令会自动创建一个job执行,在证书过期之前自动更新有效期,job位置在其中以下一个位置:

  • /etc/crontab/
  • /etc/cron./
  • systemctl list-timers

我的机器看了下,是第三个生效的