一、说明
2023年年底左右,免费的https证书的有效期由一年缩短为90天。因为是个人博客网站,加上也没有交互内容,所以用了免费的https证书。
开始的时候,就直接手动更新,但是90天的有效期,导致更新太频繁,就从网上找了下自动续订的方案。
另外这种方式,仅限于个人博客,商业公司还是要根据业务需要购买更安全的https证书。
二、https证书自动续订原理
以前https证书都是手动从申请,然后部署。如果要实现https证书的自动续订则通过Lets Encrypt来申请证书。 Let’s Encrypt 是一家免费、开放、自动化的公益性证书颁发机构 (CA), 由互联网安全研究组 (ISRG) 运作。
关于Let’s Encrypt 更多内容可以点击查看。
https证书的自动续订就是编写脚本去Let’s Encrypt申请证书、下载,然后自动部署。Let’s Encrypt支持的客户端。
这里主要使用Acme.sh来实现https的自动续订与部署。
三、acme.sh部署
1.安装acme.sh
curl https://get.acme.sh | sh -s email=my@example.com
acme.sh会安装到下面的目录
~/.acme.sh/
2.acme.sh验证方式
2.1 acme.sh验证支持的方式
acme.sh 实现了 acme 协议支持的所有验证协议。
不过常用的方式是HTTP请求和DNS验证。这里仅记录DNS验证,HTTP验证的内容可以看官网。
2.2 配置dns的key
acme.sh 目前支持超过一百家的 DNS API。
以 DNSPod.cn 为例,你需要先登录到 DNSPod.cn,拿到你的 DNSPod API Key 和 ID 并设置:
export DP_Id="1234"
export DP_Key="sADDsdasdgdsf"
2.3 签发证书
acme.sh --issue --dns dns_dp -d example.com -d *.example.com
2.4 证书复制
证书生成好以后,我们需要把证书复制给对应的 Apache、Nginx 或其他服务器去使用。
必须使用–install-cert 命令来把证书复制到目标文件,请勿直接使用 ~/.acme.sh/ 目录下的证书文件,这里面的文件都是内部使用,而且目录结构将来可能会变化。
Apache 示例:
acme.sh --install-cert -d example.com \
--cert-file /path/to/certfile/in/apache/cert.pem \
--key-file /path/to/keyfile/in/apache/key.pem \
--fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \
--reloadcmd "service apache2 force-reload"
Nginx 示例:
acme.sh --install-cert -d example.com \
--key-file /path/to/keyfile/in/nginx/key.pem \
--fullchain-file /path/to/fullchain/nginx/cert.pem \
--reloadcmd "service nginx reload"
Nginx 的配置项 ssl_certificate 需要使用 /etc/nginx/ssl/fullchain.cer ,而非 /etc/nginx/ssl/.cer ,否则 SSL Labs 的测试会报证书链问题(Chain issues Incomplete)。
默认情况下,证书每 60 天更新一次(可自定义)。更新证书后,Apache 或者 Nginx 服务会通过 reloadcmd 传递的命令自动重载配置。
注意:reloadcmd 非常重要。证书会自动申请续签,但是如果没有正确的 reloadcmd 命令,证书可能无法被重新应用到 Apache 或者 Nginx,因为配置没有被重载。
注意–reloadcmd里面的命令,需要根据服务具体的命令更改。比如nginx的启动命令是/usr/sbin/nginx ,则需要改为:–reloadcmd “service nginx reload”
2.5 查看已安装证书信息
acme.sh --info -d example.com
会输出如下内容:
DOMAIN_CONF=/root/.acme.sh/example.com/example.com.conf
Le_Domain=example.com
Le_Alt=no
Le_Webroot=dns_ali
Le_PreHook=
Le_PostHook=
Le_RenewHook=
Le_API=https://acme-v02.api.letsencrypt.org/directory
Le_Keylength=
Le_OrderFinalize=https://acme-v02.api.letsencrypt.org/acme/finalize/23xxxx150/781xxxx4310
Le_LinkOrder=https://acme-v02.api.letsencrypt.org/acme/order/233xxx150/781xxxx4310
Le_LinkCert=https://acme-v02.api.letsencrypt.org/acme/cert/04cbd28xxxxxx349ecaea8d07
Le_CertCreateTime=1649358725
Le_CertCreateTimeStr=Thu Apr 7 19:12:05 UTC 2022
Le_NextRenewTimeStr=Mon Jun 6 19:12:05 UTC 2022
Le_NextRenewTime=1654456325
Le_RealCertPath=
Le_RealCACertPath=
Le_RealKeyPath=/etc/acme/example.com/privkey.pem
Le_ReloadCmd=service nginx force-reload
Le_RealFullChainPath=/etc/acme/example.com/chain.pem
2.6 更新证书
目前证书每 60 天自动更新,你无需任何操作。
但是你也可以强制续签证书:
acme.sh --renew -d example.com --force
四、参考文章
1.ACME 客户端
https://letsencrypt.org/zh-cn/docs/client-options
2.爽了!一分钟轻松搞定 SSL 证书自动续期,解决免费证书每 3 个月失效问题
https://cloud.tencent.com/developer/article/2428634
3.免费申请Lets Encrypt通配符HTTPS证书
https://blog.csdn.net/cfm_gavin/article/details/104816442
4.acme.sh官网教程
4.1 acme.sh官网
https://github.com/acmesh-official/acme.sh
4.2 中文说明
https://docs.certcloud.cn/docs/installation/auto/acme/acmesh
5.acme.sh使用文档
https://docs.certcloud.cn/docs/installation/auto/acme/acmesh/