acme.sh,SSL证书申请工具

前言

我发现 certbot 安装还是过于麻烦,使用 snap 又不轻量,不使用 snap 而使用 nginx 容器出问题,今天使用 acme.sh 申请 ssl 之后,发现很方便,这篇教程来教如何使用 acme.sh

相比于 certbot 不同的地方在于 使用 nginx/apaceh 时候不会修改原有的配置

  • 好处不用担心配置被搞坏
  • 缺点是需要手动配置 ssl 证书的位置。

项目地址:https://github.com/acmesh-official/acme.sh

安装 acme.sh

国外服务器

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

国内服务器

1
mkdir .acme.sh && cd .acme.sh && vim acme.sh

填入 acme.sh 脚本内容

1
chmod +x acme.sh
1
2
echo 'alias acme.sh=~/.acme.sh/acme.sh' >> ~/.bashrc
source ~/.bashrc

修改 SSL 证书默认的 CA 机构

默认证书的 ca 机构是 Let’s Encrypt

在申请证书之前,可以通过如下命令修改 ca 机构

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

生成证书

HTTP 方式

apache

nginx

先在 nginx 配置文件夹新增 conf 文件,填好基础的模板,比如:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:80;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

修改为自己的信息

再使用 acme.sh 申请

1
acme.sh --issue -d example.com --nginx

no web server

如果你还没有运行任何 web 服务, 80 端口是空闲的, 那么 acme.sh 还能假装自己是一个 webserver, 临时听在80 端口, 完成验证:

1
acme.sh --issue -d example.com --standalone

更高级的用法请参考: https://github.com/Neilpang/acme.sh/wiki/How-to-issue-a-cert

DNS 方式

手动 DNS 方式

手动在域名上添加一条 txt 解析记录, 验证域名所有权.

好处:你不需要任何服务器, 不需要任何公网 ip, 只需要 dns 的解析记录即可完成验证

缺点:如果不同时配置 Automatic DNS API,使用这种方式 acme.sh 将无法自动更新证书,每次都需要手动再次重新解析验证域名所有权。

1
2
acme.sh --issue --dns -d example.com \
 --yes-I-know-dns-manual-mode-enough-go-ahead-please

然后, acme.sh 会生成相应的解析记录显示出来, 你只需要在你的域名管理面板中添加这条 txt 记录即可.

等待解析完成之后, 重新生成证书:

1
2
acme.sh --renew -d example.com \
  --yes-I-know-dns-manual-mode-enough-go-ahead-please

注意第二次这里用的是 --renew

自动 DNS 方式

DNS 方式的真正强大之处在于可以使用域名解析商提供的 api 自动添加 txt 记录完成验证.

acme.sh 目前支持 cloudflare, dnspod, cloudxns, godaddy 以及 ovh 等数十种解析商的自动集成.

以 dnspod 为例, 你需要先登录到 dnspod 账号, 生成你的 api id 和 api key。然后

设置 dnspod id

1
export DP_Id="1234"

设置 dnspod token

1
export DP_Key="sADDsdasdgdsf"

申请证书

1
acme.sh --issue --dns dns_dp -d example.com -d www.example.com

证书就会自动生成了. 这里给出的 api id 和 api key 会被自动记录下来, 将来你在使用 dnspod api 的时候, 就不需要再次指定了. 直接生成就好了

1
acme.sh --issue -d example.com.com --dns  dns_dp

更详细的 api 用法: https://github.com/Neilpang/acme.sh/blob/master/dnsapi/README.md

安装 SSL 证书

前面证书生成以后, 接下来需要把证书 copy 到真正需要用它的地方.

注意, 默认生成的证书都放在安装目录下: ~/.acme.sh/, 请不要直接使用此目录下的文件, 例如: 不要直接让 nginx/apache 的配置文件使用这下面的文件. 这里面的文件都是内部使用, 而且目录结构可能会变化.

正确的使用方法是使用 --install-cert 命令,并指定目标位置, 然后证书文件会被 copy 到相应的位置, 例如:

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"

Nginx

1
2
3
4
acme.sh --install-cert -d example.com \
--key-file       /etc/nginx/ssl/example.com/key.pem  \
--fullchain-file /etc/nginx/ssl/example.com/cert.pem \
--reloadcmd     "service nginx force-reload"

/etc/nginx/ssl/example.com/key.pem > /etc/nginx/ssl/example.com/cert.pem 可自由指定位置,请保证/etc/nginx/ssl/example.com路径存在

一个小提醒, 这里用的是 service nginx force-reload, 不是 service nginx reload, 据测试, reload 并不会重新加载证书, 所以用的 force-reload

Nginx 的配置 ssl_certificate 使用 /etc/nginx/ssl/fullchain.cert ,而非 /etc/nginx/ssl/<domain>.cert ,否则 SSL Labs 的测试会报 Chain issues Incomplete 错误。

--install-cert命令可以携带很多参数, 来指定目标文件. 并且可以指定 reloadcmd, 当证书更新以后, reloadcmd 会被自动调用,让服务器生效.

详细参数请参考: https://github.com/Neilpang/acme.sh#3-install-the-issued-cert-to-apachenginx-etc

值得注意的是, 这里指定的所有参数都会被自动记录下来, 并在将来证书自动更新以后, 被再次自动调用.

查看已安装证书信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

更新证书

目前证书在 60 天以后会自动更新, 你无需任何操作. 今后有可能会缩短这个时间, 不过都是自动的, 你不用关心.

请确保 cronjob 正确安装, 看起来是类似这样的:

1
2
3
crontab  -l

56 * * * * "/root/.acme.sh"/acme.sh --cron --home "/root/.acme.sh" > /dev/null

关于修改 ReloadCmd

目前修改ReloadCmd没有专门的命令,可以通过重新安装证书来实现修改reloadCmd的目的。 此外,安装证书后,相关信息是保存在~/.acme.sh/example.com/example.conf文件下的,内容就是acme.sh --info -d example.com输出的信息,不过ReloadCmd在文件中使用了 Base64 编码。理论上可以通过直接修改该文件来修改ReloadCmd,且修改时,无需 Base64 编码,直接写命令原文acme.sh也可以识别。 不过,example.conf文件的位置和内容格式以后可能会改变!example.conf一直都是内部使用, 后面有可能会改为用 sqlite 或者 mysql 格式存储. 所以一般不建议自己修改。

更新 acme.sh

目前由于 acme 协议和 letsencrypt CA 都在频繁的更新, 因此 acme.sh 也经常更新以保持同步.

升级 acme.sh 到最新版 :

1
acme.sh --upgrade

如果你不想手动升级, 可以开启自动升级:

1
acme.sh --upgrade --auto-upgrade

之后, acme.sh 就会自动保持更新了.

你也可以随时关闭自动更新:

1
acme.sh --upgrade --auto-upgrade  0

出错怎么办:

如果出错, 请添加 debug log:

1
acme.sh --issue  .....  --debug

或者:

1
acme.sh --issue  .....  --debug  2

请参考: https://github.com/Neilpang/acme.sh/wiki/How-to-debug-acme.sh

在 DNS 验证模式下如果 debug 中出现诸如"timed out"等字样可能是因为 GFW 拦截了相应请求,需要添加 http(s) proxy 环境变量。(请按照自己实际设定修改)

1
export http_proxy="socks5h://localhost:1081" && export https_proxy="socks5h://localhost:1081"

如果是使用 docker 则完整示例配置如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
docker run --rm  -it  \
  -v "/etc/acme":/acme.sh  \
  -e "CF_Token=[填入自己的信息]" \
  -e "CF_Account_ID=[填入自己的信息]" \
  -e "CF_Zone_ID=[填入自己的信息]" \
  -e http_proxy="socks5h://[代理A]:1234" \
  -e https_proxy="socks5h://[代理A]:1234" \
  --network container:[代理A]\
  neilpang/acme.sh \
  --issue -d example.com --dns dns_cf --debug

上述例子中使用 cloudflare 的 DNS 来签发证书,并通过把 acme.sh 链接到容器[代理 A],来转发 curl 请求(请按照自己实际设定修改)

最后, 本文并非完全的使用说明, 还有很多高级的功能, 更高级的用法请参看其他 wiki 页面.

https://github.com/Neilpang/acme.sh/wiki

一键脚本(适合初次申请)

暂时未完成…

本脚本为了简化 acme.sh 申请证书过程,会修改配置,如已有的配置,请勿使用

1
vim acme-autossl.sh
网站总访客数:Loading
网站总访问量:Loading
使用 Hugo 构建
主题 StackJimmy 设计
-->