由 Qiongpan Ke 于 2022-09-22 最后修改
显示最后作者
author | version | line-number | content |
---|---|---|---|
1 | = 概述 = | ||
2 | |||
3 | [[acme.sh>>https://github.com/acmesh-official/acme.sh]] 实现了 acme 协议,可以从一些 CA 机构(如:[[Let's Encrypt>>https://letsencrypt.org/]])申请网站的 https 证书,并通过安装时创建的 cronjob 定时任务在每天凌晨检查和更新即将过期的证书。 | ||
4 | |||
5 | = 使用说明 = | ||
6 | |||
7 | 参考官方中文文档《[[acme.sh 说明>>https://github.com/acmesh-official/acme.sh/wiki/%E8%AF%B4%E6%98%8E]]》。 | ||
8 | |||
9 | == 安装 == | ||
10 | |||
11 | {{code language="sh"}} | ||
12 | # 其中 email 最好与网站登录的邮箱一致,可以用 whois 先查一下。 | ||
13 | curl https://get.acme.sh | sh -s email=example@example.com | ||
14 | # 如果使用非 root 用户安装,默认安装到 ~/.acme.sh/ 目录,并将命令行别名 acme.sh 添加到当前用户的启动脚本(如.bashrc),重新登录即可生效。 | ||
15 | # alias acme.sh=~/.acme.sh/acme.sh | ||
16 | {{/code}} | ||
17 | |||
18 | == 生成证书 == | ||
19 | |||
20 | 生成证书时需要验证您对 Web 服务器的管理权限,有两种方式:a) 在服务器根路径下添加用于验证的文件; b) 在该域名下添加一条 txt 类型的 DNS 解析记录。 | ||
21 | |||
22 | === 文件验证方式 === | ||
23 | |||
24 | {{code language="sh" layout="LINENUMBERS"}} | ||
25 | # 方式1: 指定放置验证文件的目录; | ||
26 | acme.sh --issue -d example.com -d www.example.com --webroot /srv/www/example.com/ | ||
27 | # 方式2: 根据当前系统的 apache 配置查找放置验证文件的目录; | ||
28 | acme.sh --issue -d example.com --apache | ||
29 | # 方式3: 根据当前系统的 nginx 配置查找放置验证文件的目录; | ||
30 | acme.sh --issue -d example.com --nginx | ||
31 | # 方式4: 当没有任何 Web 服务器,可以模拟一个 Web 服务,以完成验证。 | ||
32 | acme.sh --issue -d example.com --standalone | ||
33 | {{/code}} | ||
34 | |||
35 | === DNS验证方式 === | ||
36 | |||
37 | 手动配置 DNS 解析记录的方式: | ||
38 | |||
39 | {{code language="sh"}} | ||
40 | # 查看需要手动配置的 DNS 解析记录 | ||
41 | acme.sh --issue --dns -d example.com --yes-I-know-dns-manual-mode-enough-go-ahead-please | ||
42 | # 向 CA 申请并生成 https 证书 | ||
43 | acme.sh --renew -d example.com --yes-I-know-dns-manual-mode-enough-go-ahead-please | ||
44 | {{/code}} | ||
45 | |||
46 | 自动配置 DNS 解析记录的方式(支持 cloudflare, [[dnspod>>https://console.dnspod.cn/account/token/token]], cloudxns, godaddy ): | ||
47 | |||
48 | {{code language="sh"}} | ||
49 | # 在 dnspod 平台申请生成 DNSPod Token 的 ID 和 Token | ||
50 | export DP_Id="1234" | ||
51 | export DP_Key="sADDsdasdgdsf" | ||
52 | # 自动向 dnspod 添加 DNS 解析记录,并向 CA 申请生成 https 证书 | ||
53 | acme.sh --issue --dns dns_dp -d example.com -d www.example.com | ||
54 | {{/code}} | ||
55 | |||
56 | == 安装证书 == | ||
57 | |||
58 | 上面生成的证书默认是放置到 ~~/.acme.sh/ 目录下,但 Web 服务器不应直接引用该目录下的证书,应该使用 install-cert 命令将证书安装到指定位置。 | ||
59 | |||
60 | Apache: | ||
61 | |||
62 | {{code language="sh"}} | ||
63 | acme.sh --install-cert -d example.com \ | ||
64 | --cert-file /path/to/certfile/in/apache/cert.pem \ | ||
65 | --key-file /path/to/keyfile/in/apache/key.pem \ | ||
66 | --fullchain-file /path/to/fullchain/certfile/apache/fullchain.pem \ | ||
67 | --reloadcmd "service apache2 force-reload" | ||
68 | {{/code}} | ||
69 | |||
70 | Nginx: | ||
71 | |||
72 | {{code language="sh"}} | ||
73 | acme.sh --install-cert -d example.com \ | ||
74 | --key-file /path/to/keyfile/in/nginx/key.pem \ | ||
75 | --fullchain-file /path/to/fullchain/nginx/cert.pem \ | ||
76 | --reloadcmd "service nginx force-reload" | ||
77 | {{/code}} | ||
78 | |||
79 | == 证书续期 == | ||
80 | |||
81 | 通过 cronjob 创建的定时任务,在每次凌晨会进行证书过期检查,在过期前会完成证书更新续期。目前每 **60** 天会自动更新续期一次证书,无需任何人工干预。 | ||
82 | |||
83 | 如果不再需要证书续期,可以执行下面脚本移除对指定域名的证书的检查更新(但不会移除当前在用的证书文件): | ||
84 | |||
85 | {{code language="sh"}} | ||
86 | acme.sh --remove -d example.com | ||
87 | {{/code}} | ||
88 | |||
89 | == 升级 == | ||
90 | |||
91 | {{code language="sh"}} | ||
92 | # 立即检查升级 | ||
93 | acme.sh --upgrade | ||
94 | # 开启自动检查升级 | ||
95 | acme.sh --upgrade --auto-upgrade | ||
96 | # 关闭自动检查升级 | ||
97 | acme.sh --upgrade --auto-upgrade 0 | ||
98 | {{/code}} | ||
99 | |||
100 | = 使用 docker 运行 acme.sh = | ||
101 | |||
102 | 可参考官方文档《[[Run acme.sh in docker>>https://github.com/acmesh-official/acme.sh/wiki/Run-acme.sh-in-docker]]》。 |