Linux中申请Let’s Encrypt通配符证书

  1. 使用的工具,使用certbot申请并使用acme-dns-client作为manual-auth-hook执行更新证书的验证工作。
  • 安装certbot:在不同的Linux发行版中可以使用apt install certbot, yum install epel-release 之后再yum install certbot以及 dnf install certbot
  • 安装certbot之后会有以下提示,自动证书更新服务没有启动,但是新版本的话当使用certbot申请证书后,certbot-renew.timer服务会自动开启

###Certbot auto renewal timer is not started by default.

###Run ‘systemctl start certbot-renew.timer’ to enable automatic renewals.

systemctl status certbot-renew.timer

  • 使用acme-dns-client注册需要申请证书的域名,每一个执行一次并按提示修改DNS记录,因为CAA记录验证属于有风险操作所以 {必选参数 –dangerous},例子如下
acme-dns-client register -d cn-it.org -s https://auth.acme-dns.io –dangerous

acme-dns-client register -d 51azure.com -s https://auth.acme-dns.io –dangerous

acme-dns-client register -d yuushatech.com -s https://auth.acme-dns.io –dangerous

acme-dns-client register -d maoutech.com -s https://auth.acme-dns.io –dangerous

acme-dns-client register -d 5i818.com -s https://auth.acme-dns.io –dangerous

acme-dns-client register -d 51aws.com -s https://auth.acme-dns.io –dangerous

每一次命令输入完都会有一个通过DNS验证域名所有权的过程,需要添加CNAME记录,如下

_acme-challenge.cn-it.org.     IN      CNAME   6185f842-efb2-4c65-9813-a0c410ea36ac.auth.acme-dns.io

  • 在验证完CNAME记录之后会提示验证CAA记录,需要在DNS中添加CAA记录

caa         @            letsencrypt.org 0 issue  (单域名证书验证记录)

caa         @            letsencrypt.org 0 issuewild (通配符证书验证记录)

阿里云DNS注意事项,DNS记录值中的 “letsencrypt.org” 一定要有””

  • 验证CNAME的过程中可能会有警告信息,说明这个服务器以前做过相同操作留有旧的账户信息。

查看 /etc/letsencrypt/accounts/  目录下可能有两个账号,删掉老的。

Do you wish to set up a CAA record with accounturi now? [y/N]: y

[i] Found a total of 2 ACME account(s) on this system:

 [Certbot] URI: https://acme-staging-v02.api.letsencrypt.org/acme/acct/177419874

  • 在所有域名验证完成后,申请证书。Challenges模式是 dns,auth-hook 是acme-dns-client。由于新的证书安全性需要,通配符证书必须还要配合根域域名证书才能信任,所以要同时申请。
certbot certonly \
  --manual \
  --preferred-challenges dns \
  --manual-auth-hook 'acme-dns-client' \
  -d '*.cn-it.org' \
  -d cn-it.org \
  -d '*.51azure.com' \
  -d 51azure.com \
  -d '*.yuushatech.com' \
  -d yuushatech.com \
  -d '*.maoutech.com' \
  -d maoutech.com \
  -d '*.5i818.com' \
  -d 5i818.com \
  -d '*.51aws.com' \
  -d 51aws.com
  • 如果之前的CNAME记录及CAA记录都完好可查的情况下,证书应该可以顺利签署。

使用命令 certbot renew –dry-run 可以测试证书是否可以正常renew,使用命令 systemctl status certbot-renew.timer 可以查看自动renew服务的状态

  • 关于配置文件及自动运行脚本

    配置文件位置 /etc/letsencrypt/renewal/cn-it.org.conf

    自动运行脚本 /etc/letsencrypt/renewal-hooks/post/pkcs12convert.sh

    脚本内容如下,每次自动更新证书后会自动运行renewal-hooks/post 中的脚本

    #!/bin/sh
    openssl pkcs12 -export -out /etc/letsencrypt/live/cn-it.org/cn-it.org.pfx -inkey /etc/letsencrypt/live/cn-it.org/privkey.pem -in /etc/letsencrypt/live/cn-it.org/cert.pem -certfile /etc/letsencrypt/live/cn-it.org/chain.pem -keypbe AES-256-CBC -certpbe AES-256-CBC -macalg sha256 -passout pass:XXXxxx***
    echo "pkcs#12 generated!"
    /bin/cp -f /etc/letsencrypt/live/cn-it.org/fullchain.pem /home/gex/v2fly/certs/fullchain.pem
    /bin/cp -f /etc/letsencrypt/live/cn-it.org/privkey.pem /home/gex/v2fly/certs/privkey.pem
    chmod 666 -R /home/gex/v2fly/certs/
    echo "cert copyed!"
    systemctl restart nginx
    echo "Nginx restarted"
    
    
    • 关于域名变化后的证书申请更新

    如果你是要覆盖现在的 cn-it.org 证书,并且以后继续让 certbot renew 使用这套配置,建议明确指定证书名:

    certbot certonly \
      --cert-name cn-it.org \
      --manual \
      --preferred-challenges dns \
      --manual-auth-hook 'acme-dns-client' \
      -d '*.cn-it.org' \
      -d cn-it.org \
      -d '*.51azure.com' \
      -d 51azure.com \
      -d '*.5i818.com' \
      -d 5i818.com \
      -d '*.51aws.com' \
      -d 51aws.com
    • 关于证书自动更新的服务(随时期不同有以下两种名字)

    systemctl status certbot-renew.timer
    systemctl status certbot.timer

    发表评论