获取SSL证书
理论上,我们自己也可以手动制作一个 SSL 安全证书,但是我们自己签发的安全证书浏览器信任,所以我们需要被信任的证书授权中心( CA )签发的安全证书。而一般的 SSL 安全证书签发服务都需要付费,且价格昂贵,不过为了加快推广 https 的普及, EEF 电子前哨基金会、 Mozilla 基金会和美国密歇根大学成立了一个公益组织叫 ISRG ( Internet Security Research Group ),这个组织从 2015 年开始推出了 Let’s Encrypt 免费证书。这个免费证书不仅免费,而且还相当好用,所以我们就可以利用 Let’s Encrypt 提供的免费证书部署 https 了。
1,安装snap
sudo apt-get install snapd
什么是snap?
一个包含应用程序代码和快照的squashFS文件系统。包含特定元数据的snap.yaml文件。它有一个只读文件系统,安装后还有一个可写区域。
是独立的。它捆绑了所需的大多数库和运行时,并且可以在不影响系统其余部分的情况下进行更新和还原。
是通过安全机制限制在OS和其他应用程序中的,但可以根据用户控制的细粒度策略和OS默认值与其他snap交换内容和功能。
snap可以理解为一个新型的软件包管理器
2,安装certbot
sudo snap install --classic certbot
sudo snap set certbot trust-plugin-with-root=ok
snap安装下的certbot
如果出瑞
Command 'certbot' not found,
可以使用/snap/bin/certbot
3,使用certbot创建证书
sudo /snap/bin/certbot certonly -d example.com --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
按提示输入相关信息 直接出现,如下图,让你添加dns信息,不要按回车
这时到你的dns服务器
按照提示增加对应的txt记录
添加完成后,等待1分钟到2分钟,回到命令行,按回车。
如果出现dns验证失败,说明dns记录还未生效,这时可以调整dns的ttl值,重新执行
sudo /snap/bin/certbot certonly -d example.com --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
然后更新dns里的记录,多试几次就可以成功
4,使用certbot插件自动添加dns记录
每次创建证书,都需要手动修改dns记录,有没有自动添加记录的功能
以cloundflare为例
先安装对应的插件
sudo snap install certbot-dns-cloudflare
创建对应的配置文件
mkdir -p ~/.secrets/certbot
chmod 0700 ~/.secrets
# Cloudflare API 秘钥
vim ~/.secrets/certbot/cloudflare.ini
/cloudflare.ini 内容
# Cloudflare API credentials used by Certbot
dns_cloudflare_email = cloudflare@example.com
dns_cloudflare_api_key = xxxxxxxxxxxxxxxxxxx
apikey获取方法
登录cloudflare,在我的资料里创建dns区域编辑api key
使用刚创建的配置文件
snap/bin/certbot certonly -d 'example.com' --agree-tos --email 你的邮箱@example.com --server https://acme-v02.api.letsencrypt.org/directory --dns-cloudflare --dns-cloudflare-credentials /root/.secrets/certbot/cloudflare.ini --dns-cloudflare-propagation-seconds 100
如果出现以下错误
Error determining zone_id: 6003 Invalid request headers. Please confirm that you have supplied valid Cloudflare API credentials.
请使用全局api key
5,自动更新
使用crontab -e 编辑定时任务
0 4 * * * /snap/bin/certbot renew >> /var/log/le-renew.log