使用 acme.sh 向 Let's Encrypt 申请 https 证书并自动续期
由 Qiongpan Ke 于 2022-09-22 最后修改
概述
acme.sh 实现了 acme 协议,可以从一些 CA 机构(如:Let's Encrypt)申请网站的 https 证书,并通过安装时创建的 cronjob 定时任务在每天凌晨检查和更新即将过期的证书。
使用说明
参考官方中文文档《acme.sh 说明》。
安装
# 其中 email 最好与网站登录的邮箱一致,可以用 whois 先查一下。
curl https://get.acme.sh | sh -s email=example@example.com
# 如果使用非 root 用户安装,默认安装到 ~/.acme.sh/ 目录,并将命令行别名 acme.sh 添加到当前用户的启动脚本(如.bashrc),重新登录即可生效。
# alias acme.sh=~/.acme.sh/acme.sh
curl https://get.acme.sh | sh -s email=example@example.com
# 如果使用非 root 用户安装,默认安装到 ~/.acme.sh/ 目录,并将命令行别名 acme.sh 添加到当前用户的启动脚本(如.bashrc),重新登录即可生效。
# alias acme.sh=~/.acme.sh/acme.sh
生成证书
生成证书时需要验证您对 Web 服务器的管理权限,有两种方式:a) 在服务器根路径下添加用于验证的文件; b) 在该域名下添加一条 txt 类型的 DNS 解析记录。
文件验证方式
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 方式1: 指定放置验证文件的目录;
acme.sh --issue -d example.com -d www.example.com --webroot /srv/www/example.com/
# 方式2: 根据当前系统的 apache 配置查找放置验证文件的目录;
acme.sh --issue -d example.com --apache
# 方式3: 根据当前系统的 nginx 配置查找放置验证文件的目录;
acme.sh --issue -d example.com --nginx
# 方式4: 当没有任何 Web 服务器,可以模拟一个 Web 服务,以完成验证。
acme.sh --issue -d example.com --standalone
acme.sh --issue -d example.com -d www.example.com --webroot /srv/www/example.com/
# 方式2: 根据当前系统的 apache 配置查找放置验证文件的目录;
acme.sh --issue -d example.com --apache
# 方式3: 根据当前系统的 nginx 配置查找放置验证文件的目录;
acme.sh --issue -d example.com --nginx
# 方式4: 当没有任何 Web 服务器,可以模拟一个 Web 服务,以完成验证。
acme.sh --issue -d example.com --standalone
DNS验证方式
手动配置 DNS 解析记录的方式:
# 查看需要手动配置的 DNS 解析记录
acme.sh --issue --dns -d example.com --yes-I-know-dns-manual-mode-enough-go-ahead-please
# 向 CA 申请并生成 https 证书
acme.sh --renew -d example.com --yes-I-know-dns-manual-mode-enough-go-ahead-please
acme.sh --issue --dns -d example.com --yes-I-know-dns-manual-mode-enough-go-ahead-please
# 向 CA 申请并生成 https 证书
acme.sh --renew -d example.com --yes-I-know-dns-manual-mode-enough-go-ahead-please
自动配置 DNS 解析记录的方式(支持 cloudflare, dnspod, cloudxns, godaddy ):
# 在 dnspod 平台申请生成 DNSPod Token 的 ID 和 Token
export DP_Id="1234"
export DP_Key="sADDsdasdgdsf"
# 自动向 dnspod 添加 DNS 解析记录,并向 CA 申请生成 https 证书
acme.sh --issue --dns dns_dp -d example.com -d www.example.com
export DP_Id="1234"
export DP_Key="sADDsdasdgdsf"
# 自动向 dnspod 添加 DNS 解析记录,并向 CA 申请生成 https 证书
acme.sh --issue --dns dns_dp -d example.com -d www.example.com
安装证书
上面生成的证书默认是放置到 ~/.acme.sh/ 目录下,但 Web 服务器不应直接引用该目录下的证书,应该使用 install-cert 命令将证书安装到指定位置。
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"
--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 force-reload"
--key-file /path/to/keyfile/in/nginx/key.pem \
--fullchain-file /path/to/fullchain/nginx/cert.pem \
--reloadcmd "service nginx force-reload"
证书续期
通过 cronjob 创建的定时任务,在每次凌晨会进行证书过期检查,在过期前会完成证书更新续期。目前每 60 天会自动更新续期一次证书,无需任何人工干预。
如果不再需要证书续期,可以执行下面脚本移除对指定域名的证书的检查更新(但不会移除当前在用的证书文件):
acme.sh --remove -d example.com
升级
# 立即检查升级
acme.sh --upgrade
# 开启自动检查升级
acme.sh --upgrade --auto-upgrade
# 关闭自动检查升级
acme.sh --upgrade --auto-upgrade 0
acme.sh --upgrade
# 开启自动检查升级
acme.sh --upgrade --auto-upgrade
# 关闭自动检查升级
acme.sh --upgrade --auto-upgrade 0
使用 docker 运行 acme.sh
可参考官方文档《Run acme.sh in docker》。