申请Let’s Encrypt 通配符证书

很多个人站长都是用的Let’s Encrypt的SSL证书,因为只有90天有效期,我嫌太麻烦一直没用过,因为网站服务器可以通过脚本自动更新证书,可CDN那边得手动去更换。个人建议这么用:CDN上面用国内BAT三家提供的有效期一年的免费证书,网站服务器上用Let’s Encrypt的证书。最近Let’s Encrypt上线了通配符证书,试着申请了下,隧记录下过程。

使用 acme.sh 获取 Let’s Encrypt 证书

acme.sh是国人写的一个脚本,能很方面的获取、更新证书。
登录服务器后,直接运行

 curl https://get.acme.sh | sh 

It is recommended to install socat first
出现了红色的提醒:
It is recommended to install socat first
We use socat for standalone server if you use standalone mode.
If you don't use standalone mode, just ignore this warning.
大概意思就是叫我最好安装 socat
那我就装呗

yum install socat -y

注意,这个socat是在epel源里面的:
socat
若刚刚的yum install socat -y 安装报错,我们就安装epel源后

yum install epel-release -y

再执行yum install socat -y 来安装socat。
所以,正确执行命令的顺序如下:

yum install epel-release -y
yum install socat -y
curl https://get.acme.sh | sh

此时,我们输入 bash 打开一个新的bash,acme就可以使用了。
看看大致的使用方法

[root@hqidi-com ~]# acme.sh --help
https://github.com/Neilpang/acme.sh
v2.7.8
Usage: acme.sh  command ...[parameters]....
Commands:
  --help, -h               Show this help message.
  --version, -v            Show version info.
  --install                Install acme.sh to your system.
  --uninstall              Uninstall acme.sh, and uninstall the cron job.
  --upgrade                Upgrade acme.sh to the latest code from https://github.com/Neilpang/acme.sh.
  --issue                  Issue a cert.
  --signcsr                Issue a cert from an existing csr.
  --deploy                 Deploy the cert to your server.
  --install-cert           Install the issued cert to apache/nginx or any other server.
  --renew, -r              Renew a cert.
  --renew-all              Renew all the certs.
  --revoke                 Revoke a cert.
  --remove                 Remove the cert from list of certs known to acme.sh.
  --list                   List all the certs.
  --showcsr                Show the content of a csr.
  --install-cronjob        Install the cron job to renew certs, you don't need to call this. The 'install' command can automatically install the cron job.
  --uninstall-cronjob      Uninstall the cron job. The 'uninstall' command can do this automatically.
  --cron                   Run cron job to renew all the certs.
  --toPkcs                 Export the certificate and key to a pfx file.
  --toPkcs8                Convert to pkcs8 format.
  --update-account         Update account info.
  --register-account       Register account key.
  --deactivate-account     Deactivate the account.
  --create-account-key     Create an account private key, professional use.
  --create-domain-key      Create an domain private key, professional use.
  --createCSR, -ccsr       Create CSR , professional use.
  --deactivate             Deactivate the domain authz, professional use.
  

获取DNS控制API

想通过脚本acme.sh获取通配符证书得验证DNS的TXT记录,自己添加TXT记录太过繁琐,所以我选择使用API的方式来添加TXT记录。
若你用的阿里云的解析,请打开https://ak-console.aliyun.com/#/accesskey 获取API。若你使用的腾讯云的解析,请跟着一步一步来。
➤打开 https://www.dnspod.cn/console/user/security 用腾讯云的帐号登录(dnspod早年被腾讯收购)
➤点击创建API Token按钮,新建一个API,建好后得到一个ID和Token
dnspod_API
➤把刚刚得到的ID和Token告诉acme.sh

[root@hqidi-com ~]# export DP_Id="50847"
[root@hqidi-com ~]# export DP_Key="6cd08fb87956mmmmmmmmmmmmmmmmmmmm"

签发通配符证书

acme.sh --issue --dns dns_dp -d diyilo.com -d www.diyilo.com -d *.diyilo.com 

注意事项:第一个 -d 后面不可以直接写 *.xx.com ,一定要写个普通单域名,第二个 -d 后面可以写泛域名。

签发通配符证书
最终,我们在/root/.acme.sh/diyilo.com/目录里面得到证书相关的所有文件。
➤ca.cer 这个是Let’s Encrypt的根证书,这个文件内容跟fullchain.cer文件的最下面一部分相同。
➤diyilo.com.cer 这是我们申请到的域名通配符证书,这个文件的内容跟fullchain.cer文件的最上面一部分相同。若你的网站没有android用户的话,可以用这个证书。只不过Android用户访问会报错。当前网站证书不可信且证书链长度为1
证书链影响Android
➤diyilo.com.key 证书密钥
➤fullchain.cer 虽然文件名叫完整的证书链,但我们就用这个文件做证书,就不会有上面的Android报错。

通配符证书效果

通配符证书效果
查看证书,显示效果稍有不同
查看SSL证书

安全提醒:
我们在DNSPOD的域名控制台能看到域名解析记录:
域名解析记录
因为我们给了脚本acme.sh一个API Token,那个API是对于整个腾讯云帐号的API Token,权限大到可以为所欲为,所以千万别泄漏了那个API Token。

原创文章,转载请注明: 转载自笛声

本文链接地址: 申请Let’s Encrypt 通配符证书

12 条评论

  • dige 2018年3月22日 回复 作者

    其他DNS服务商API操作:https://github.com/Neilpang/acme.sh/tree/master/dnsapi

  • 西枫里博客 2018年3月22日 回复

    没申请let'encrypt 一直用的亚洲诚信。一年有效期,还不错

  • 姜辰 2018年3月24日 回复

    全通配符= = 、如果不是三个月续期,我也想用。233

    • 姬长信 2018年3月30日 回复

      大作家这个可以用脚本续费

      • dige 2018年3月30日 回复 作者

        我知道,这个弄好后,会添加一条crontab计划任务。

  • 老何 2018年3月26日 回复

    看得我一脸懵逼。

  • Loekman 2018年4月4日 回复

    俺是虚机,从freessl.org申请

  • 里维斯社 2018年4月5日 回复

    这个证书用起来还是蛮方便的,自动续签

  • 广州网站建设 2018年6月8日 回复

    其实就是证书

  • dige 2018年7月2日 回复

    若域名在阿里云:
    export Ali_Key="abcdefrdsfswje"
    export Ali_Secret="jlsdflanljkljlfdsaklkjflsa"

  • 淮少 2021年11月5日 回复

    老哥,你的termianl配色很漂亮,请问是用iterm吗? 用的什么主题?

    • dige 2021年11月6日 回复 作者

      文中不是终端截图,prettify.js 实现的页面效果而已。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Copyright © 2015-2024 笛声博客 All Rights Reserved     浙ICP备15036123号-1