这两天域名的证书又到期了,因为qiong呀一直都在用免费的,之前也使用过acme.sh来申请&更新证书,但是使用一段时间后不能更新了,没有整好,就换到了zerossl,但是今天发现不能再次免费续了,超过了免费次数3次。

还是因为qiong,所以寻找了一下其他方案,看网络还是acme.sh相关的居多,今天又折腾了下acme.sh脚本,成功了,记录之。

acme安装

github仓库地址:https://github.com/acmesh-official/acme.sh

根据README文档,可以有以下几种安装方式:

在线安装:

1
2
3
4
5
6
#使用curl
curl https://get.acme.sh | sh -s email=my@example.com

#或者使用wget
wget -O - https://get.acme.sh | sh -s email=my@example.com

通过拉取git源码:

1
2
3
git clone https://github.com/acmesh-official/acme.sh.git
cd ./acme.sh
./acme.sh --install -m my@example.com

手动安装(如果没有root权限,推荐这种):

1
2
3
4
5
6
7
8
#复制acme.sh到home
cp acme.sh ~/.acme.sh/

#.bashrc创建一个别名
alias acme.sh='~/.acme.sh/acme.sh'

#创建cront定时任务脚本
0 0 * * * "/home/user/.acme.sh"/acme.sh --cron --home "/home/user/.acme.sh" > /dev/null

这里使用了curl的安装方式,网络不是很稳定,试了几次成功了。

证书申请

根据wiki中的说明,从v3.0开始,默认CA服务商改为ZeroSSL,根据网上的教程操作了,在关联账号的时候就是不成功,超时不知道是网络问题,还是因为我的账号已经在web控制台申请了三次免费了;所以修改使用Letsencrypt.

修改默认CA Server:

1
acme.sh --set-default-ca --server letsencrypt

配置DNS API参数

这里使用了DNS API添加record方式来完成证书申请时的验证,有关DNS API可以参考官方wiki:

https://github.com/acmesh-official/acme.sh/wiki/dnsapi

主要是需要定义相关的token,这边使用的是cloudflare,需要的参数如下:

1
2
3
export CF_Token="xxxxxxxxxxxxxxxxx"
export CF_Account_ID="xxxxxxxxxxxxx"
export CF_Zone_ID="xxxxxxxxxxxxx"

申请成功后,相关的参数将会保存在domain/xxx.conf文件中.

申请证书

1
./acme.sh --issue --dns dns_cf -d example.com -d www.example.com

证书部署

关于证书部署可以参考官方wiki:

https://github.com/acmesh-official/acme.sh/wiki/%E8%AF%B4%E6%98%8E

https://github.com/acmesh-official/acme.sh/wiki

这里说一下的是,可以通过deploy_hook实现自定义部署,有关deploy_hook详细参考:

https://github.com/acmesh-official/acme.sh/wiki/deployhooks

~/.acme.sh/deploy目录下,存放自定义部署脚本,示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash

#mayapi

#Here is a sample custom api script.
#This file name is "myapi.sh"
#So, here must be a method myapi_deploy()
#Which will be called by acme.sh to deploy the cert
#returns 0 means success, otherwise error.

######## Public functions #####################

#domain keyfile certfile cafile fullchain
myapi_deploy() {
_cdomain="$1"
_ckey="$2"
_ccert="$3"
_cca="$4"
_cfullchain="$5"

echo "will update domain: $_cdomain cert...."

#update nginx cert
cat $_cfullchain >/opt/etc/cert/cer/nginx.cer
cat $_ckey >/opt/etc/cert/key/nginx.key

echo "update nginx cert OK."

return 0

}

执行部署脚本

1
acme.sh --deploy -d example.com --deploy-hook myapi