momo's Blog.

使用acme.sh自动更新域名证书

字数统计: 1.2k阅读时长: 5 min
2022/06/16 Share

前言

域名更新到期续费和更换是一个比较麻烦的事情。 正好近期准备迁移一下博客, 也不在想在手动申请免费的证书和配置了。尝试一下使用 acme.sh 自动更新证书。

acme.sh 是一个实现 ACME 协议的客户端,能够向支持 ACME 协议的 CA 申请证书(如 Letsencrypt)至于 ACME 协议是什么?Automatic Certificate Management Environment 自动化证书管理环境,通过它我们可以实现证书的自动申请以及部署,可以大大的节省人员的管理及额外的配置工作。

安装acme.sh

安装 acme.sh 非常简单,只需要一个命令。

1
curl https://get.acme.sh | sh -s email=my@example.com

安装程序做了3个步骤

  1. 把 acme.sh 安装到你的 home 目录下:
    1
    ~/.acme.sh/

并创建 一个 bash 的 alias, 方便你的使用: alias acme.sh=~/.acme.sh/acme.sh

1
2
3
4
5
6
7
~# cat .bashrc
...
. "/root/.acme.sh/acme.sh.env"

~# cat /root/.acme.sh/acme.sh.env
export LE_WORKING_DIR="/root/.acme.sh"
alias acme.sh="/root/.acme.sh/acme.sh"
  1. 自动为你创建 cronjob, 每天 0:00 点自动检测所有的证书, 如果快过期了, 需要更新, 则会自动更新证书.
1
2
crontab -l
21 0 * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null

申请证书

证书的申请方式分为http的方式和手动dns的方式。

我们使用http的方式, acme.sh会在当前html目录下创建验证文件, 并且在验证后删除。

单域名申请方式

1
acme.sh  --issue  -d xmomo521.top  --webroot  /var/www/html/

多域名申请方式

1
acme.sh  --issue  -d xmomo521.top  -d www.xmomo521.top --webroot  /var/www/html/

申请完成以后,执行acme.sh --list 可查看证书

1
2
3
acme.sh --list
Main_Domain KeyLength SAN_Domains CA Created Renew
xmomo521.top "2048" no ZeroSSL.com 2022-06-16T06:19:34Z 2022-08-15T06:19:34Z

证书的有效期一般为90天, 证书的存放位置在 ~/.acme.sh/{your_domain}/, 不过一般不推荐自己去copy, 应该使用acme.sh 自身的命令去copy证书。

Copy证书文件

Nginx

1
2
3
4
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"

Apache

1
2
3
4
5
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"

上述的所有命令都会被 acme.sh 记录下来, 并且证书需要更新的时候自动更新。

but, 我的前端并没有使用Apache和NGINX,而是用的haproxy,并且假如说我的部署服务器不在这里,怎么办?

Deploy-hook

官方文档
我们可以使用 deploy-hook 来去copy其他类型的证书。并且官方已经为我们写好了大部分主流的服务。

1
2
3
4
ls ~/.acme.sh/deploy/
apache.sh cpanel_uapi.sh exim4.sh gitlab.sh kong.sh myapi.sh nginx.sh openstack.sh pureftpd.sh routeros.sh synology_dsm.sh vault_cli.sh
cleverreach.sh docker.sh fritzbox.sh haproxy.sh lighttpd.sh mydevil.sh openmediavault.sh panos.sh qiniu.sh ssh.sh truenas.sh vault.sh
consul.sh dovecot.sh gcore_cdn.sh keychain.sh mailcow.sh mysqld.sh opensshd.sh peplink.sh README.md strongswan.sh unifi.sh vsftpd.sh

如果自己自定义的需求比较高, 或者官方没有现成的脚本, 可以跳过这一步。

我们生成好证书以后, 需要将证书copy到Haproxy当中。

1
2
3
4
5
6
7
8
9
10
cat /opt/certs/renew-hook-haproxy.sh
#!/bin/bash

domain_name=$1

export DEPLOY_HAPROXY_PEM_PATH=/etc/ssl/private/
export DEPLOY_HAPROXY_RELOAD="/usr/bin/systemctl restart haproxy"

/root/.acme.sh/acme.sh --deploy -d $1 --deploy-hook haproxy
echo Done

上方脚本指定了输出目录和重启命令, 这样就可以手动COPY证书并且生效了.

自动更新和部署

不管怎么说, 我们始终是要自动的, 当然如果你执行过--install-cert 此时已经是自动的了,但是我们并没有执行过这样的命令。

此时我们就需要另一个Renew hook

在签发证书的时候执行

1
2
3
4
acme.sh --issue -d example.com  \
--pre-hook "echo this is pre hook that happens before attempting to issue a certificate." \
--post-hook "echo this is post hook that happens after attempting to issue a certificate." \
--renew-hook "echo this will be called when certs are successfully renewed." .......

e.g

我们把刚才的脚本指定在–renew-hook.
可以通过指定的方式, 配置配置证书前,后,和更新后的钩子函数。

1
2
acme.sh --issue -d xmomo521.top --webroot  /var/www/html/  \
--renew-hook "bash /opt/certs/renew-hook-haproxy.sh xmomo521.top"

使用 install-cert

1
2
3
4
5
acme.sh \
--install-cert \
-d xmomo521.top \
--reloadcmd \
"/opt/certs/renew-hook-haproxy.sh xmomo521.top"

如果你想自定义, 那么可以随意修改 renew-hook-haproxy.sh 脚本。。。。。

查看配置

1
2
3
4
5
6
7
8
acme.sh --info  -d xmomo521.top


Le_DeployHook=haproxy,
Le_Deploy_haproxy_pem_path=/etc/ssl/private/
Le_Deploy_haproxy_reload=/usr/bin/systemctl restart haproxy
Le_ReloadCmd=/opt/certs/renew-hook-haproxy.sh xmomo521.top
Le_RealFullChainPath=

看到 Le_ReloadCmd 等于我们的脚本就正确了.

更新证书

指定域名更新

1
acme.sh --renew -d example.com --force

更新所有

1
acme.sh --cron --home "/root/.acme.sh" --force

参考文档

  1. https://wiki.acme.sh
CATALOG
  1. 1. 前言
  2. 2. 安装acme.sh
  3. 3. 申请证书
    1. 3.1. 单域名申请方式
    2. 3.2. 多域名申请方式
  4. 4. Copy证书文件
    1. 4.1. Nginx
    2. 4.2. Apache
  5. 5. Deploy-hook
  6. 6. 自动更新和部署
    1. 6.1. 在签发证书的时候执行
    2. 6.2. 使用 install-cert
  7. 7. 查看配置
  8. 8. 更新证书
  9. 9. 参考文档