问题背景

在去年底,看到一则信息,免费HTTPS证书有效期从一年调整为了三个月。没多久,便在阿里云控制台上看到了这则变更的通知:《关于免费证书服务策略调整通知》,意思是证书颁发机构改变了策略,云厂商不得不跟随调整。恰逢网站HTTPS证书即将到期,便开始寻求免费证书+自动续期的方案。

方案介绍

众所周知,目前市面上有不少提供免费HTTPS证书的机构,以下是ChatGPT建议的几个:

目前有几个知名的免费 HTTPS 证书服务提供商,它们提供了免费的 SSL/TLS 证书:

  1. Let's Encrypt:Let's Encrypt 是一个由非营利组织 Electronic Frontier Foundation (EFF)、Mozilla、Akamai、Cisco 等公司共同支持的证书颁发机构。它提供了免费的 SSL/TLS 证书,并提供了简单的工具来自动化证书的获取和更新。
  2. Cloudflare:Cloudflare 是一家网络安全和性能公司,提供了免费的基本 SSL/TLS 证书。通过 Cloudflare CDN 服务,你可以免费获得他们的证书,同时还能获得其他网络安全和性能优化功能。
  3. ZeroSSL:ZeroSSL 提供了免费的 SSL/TLS 证书,同时还提供了简单易用的在线工具来创建和管理证书。它也支持自动化证书的获取和更新。
  4. SSL For Free:SSL For Free 提供了免费的 SSL/TLS 证书,并提供了简单的方法来生成证书。它也支持通配符证书和多域名证书。

这些服务都提供了方便、简单的方法来获得免费的 HTTPS 证书,使你的网站可以通过 HTTPS 进行安全加密通信。

其中,Let's Encrypt之前便有所耳闻,我们直接采用它。

前置条件

证书的申请、续期和自动化本身并不复杂,但因为官方插件不支持阿里云,所以额外增加了不少步骤。

1. 创建子账号

前往 https://ram.console.aliyun.com 申请阿里云子账号并授予AliyunDNSFullAccess权限,创建完成后记录AccessKeyAccessKey Secret

2. 修改DNS

将域名DNS设置为阿里云DNS,否则执行时将报错:

The domain name belongs to other users. Transfer the domain name to the current user and then try the binding and setting actions.

3. 安装 aliyun cli 工具

bashCopy code
  • 1
  • 2
  • 3
  • 4
wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz tar xzvf aliyun-cli-linux-latest-amd64.tgz cp aliyun /usr/local/bin rm aliyun
wget https://aliyuncli.alicdn.com/aliyun-cli-linux-latest-amd64.tgz tar xzvf aliyun-cli-linux-latest-amd64.tgz cp aliyun /usr/local/bin rm aliyun

安装完成后需要配置凭证信息,将第一步获取的AccessKeyAccessKey Secret,以及区域ID填入即可:

bashCopy code
  • 1
aliyun configure --mode AK --profile https_dns.ini
aliyun configure --mode AK --profile https_dns.ini

4. 安装 certbot-dns-aliyun 插件

bashCopy code
  • 1
  • 2
  • 3
  • 4
  • 5
wget https://cdn.jsdelivr.net/gh/justjavac/certbot-dns-aliyun@main/alidns.sh cp alidns.sh /usr/local/bin chmod +x /usr/local/bin/alidns.sh ln -s /usr/local/bin/alidns.sh /usr/local/bin/alidns rm alidns.sh
wget https://cdn.jsdelivr.net/gh/justjavac/certbot-dns-aliyun@main/alidns.sh cp alidns.sh /usr/local/bin chmod +x /usr/local/bin/alidns.sh ln -s /usr/local/bin/alidns.sh /usr/local/bin/alidns rm alidns.sh

至此,全部准备工作完成。

HTTPS证书申请和自动续期

1. 安装snapdcertbot

bashCopy code
  • 1
  • 2
  • 3
apt install snapd snap install --classic certbot snap set certbot trust-plugin-with-root=ok
apt install snapd snap install --classic certbot snap set certbot trust-plugin-with-root=ok

2. 申请证书

测试是否能正确申请:

bashCopy code
  • 1
certbot certonly -d *.ifuyun.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run
certbot certonly -d *.ifuyun.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run

正式申请时去掉--dry-run参数:

bashCopy code
  • 1
certbot certonly -d *.ifuyun.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"
certbot certonly -d *.ifuyun.com --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean"

3. Nginx配置

执行上述命令后,会在输出中显示证书保存的路径,将其配置到Nginx中:

nginxCopy code
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
server { ... ssl_certificate /etc/letsencrypt/live/ifuyun.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/ifuyun.com/privkey.pem; include /etc/letsencrypt/options-ssl-nginx.conf; ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; ... }
server { ... ssl_certificate /etc/letsencrypt/live/ifuyun.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/ifuyun.com/privkey.pem; include /etc/letsencrypt/options-ssl-nginx.conf; ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; ... }

4. 证书续期

bashCopy code
  • 1
certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run
certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --dry-run

如果以上命令没有错误,把--dry-run参数去掉。

5. 自动续期

添加定时任务crontab

bashCopy code
  • 1
crontab -e
crontab -e

输入:

iniCopy code
  • 1
1 1 * * 0 root certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --deploy-hook "nginx -s reload"
1 1 * * 0 root certbot renew --manual --preferred-challenges dns --manual-auth-hook "alidns" --manual-cleanup-hook "alidns clean" --deploy-hook "nginx -s reload"

上面脚本中的--deploy-hook "nginx -s reload"表示在续期成功后自动重启nginx

注:关于定时任务crontab的规则,可以访问:https://crontab.guru/

 

以上。[]~( ̄▽ ̄)~*