Let's Encrypt之acme.sh
项目地址
https://github.com/Neilpang/acme.sh
介绍
Let’s Encrypt 是一个证书颁发机构(CA)。是由互联网安全研究小组(ISRG,Internet Security Research Group)主导并开发的一个新型数字证书认证机构(CA,Certificate Authority)
acme.sh实现了 acme 协议, 可以从 letsencrypt 生成免费的证书.
acme.sh纯用Shell(Unix外壳)语言编写的ACME协议客户端。支持ACME v1/ACME v2和ACME v2通配符证书
安装
- 普通安装
Check this project: https://github.com/Neilpang/get.acme.shor1
curl https://get.acme.sh | sh
1
wget -O - https://get.acme.sh | sh
- 高级安装(可选)
1
2
3
4
5
6
7
8
9
10$ git clone https://github.com/Neilpang/acme.sh.git
$ cd acme.sh
$ ./acme.sh --install \
--home ~/myacme \
--config-home ~/myacme/data \
--cert-home ~/mycerts \
--accountemail "my@example.com" \
--accountkey ~/myaccount.key \
--accountconf ~/myaccount.conf \
--useragent "this is my client."签发证书
acme.sh 实现了 acme 协议支持的所有验证协议.
更多范例请访问: https://github.com/Neilpang/acme.sh/wiki/How-to-issue-a-cert
下面是常用的两种
http方式
Webroot mode
Example 1: Single domain.(单域名)
1 | acme.sh --issue -d example.com -w /home/wwwroot/example.com |
or:
1 | acme.sh --issue -d example.com -w /home/username/public_html |
or:
1 | acme.sh --issue -d example.com -w /var/www/html |
Example 2: Multiple domains in the same cert.(多域名)
1 | acme.sh --issue -d example.com -d www.example.com -d cp.example.com -w /home/wwwroot/example.com |
只需要指定域名, 并指定域名所在的网站根目录. acme.sh 会全自动的生成验证文件, 并放到网站的根目录, 然后自动完成验证. 最后会聪明的删除验证文件. 整个过程没有任何副作用.
Nginx/apache mode
如果你用的 apache/nginx服务器, acme.sh 还可以智能的从配置中自动完成验证, 你不需要指定网站根目录:
Apache example:
1 | acme.sh --install-cert -d example.com \ |
Nginx example:
1 | acme.sh --install-cert -d example.com \ |
dns 方式
See: https://github.com/Neilpang/acme.sh/wiki/dns-manual-mode first.
在域名上添加一条 txt 解析记录, 验证域名所有权.
这种方式的好处是, 你不需要任何服务器, 不需要任何公网 ip, 只需要 dns 的解析记录即可完成验证. 坏处是,如果不同时配置 Automatic DNS API,使用这种方式 acme.sh 将无法自动更新证书,每次都需要手动再次重新解析验证域名所有权。
- 手动You should get an output like below:
1
acme.sh --issue --dns -d example.com -d www.example.com -d cp.example.com
1
2
3
4
5
6
7
8
9Add the following txt record:
Domain:_acme-challenge.example.com
Txt value:9ihDbjYfTExAYeDs4DBUeuTo18KBzwvTEjUnSwd32-c
Add the following txt record:
Domain:_acme-challenge.www.example.com
Txt value:9ihDbjxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Please add those txt records to the domains. Waiting for the dns to take effect.
等待解析完成之后, 重新生成证书:
1 | acme.sh --renew -d example.com |
Ok, it’s done.
- 自动
dns 方式的真正强大之处在于可以使用域名解析商提供的 api 自动添加 txt 记录完成验证.
acme.sh目前支持 cloudflare, dnspod, cloudxns, godaddy 以及 ovh 等数十种解析商的自动集成.
以 dnspod 为例, 你需要先登录到 dnspod 账号, 生成你的 api id 和 api key, 都是免费的. 然后:
1 | $ export DP_Id="1234" |
证书就会自动生成了. 这里给出的 api id 和 api key 会被自动记录下来, 将来你在使用 dnspod api 的时候, 就不需要再次指定了. 直接生成就好了:
1 | $ acme.sh --issue -d mydomain2.com --dns dns_dp |
复制证书
生成证书后,你可能希望将证书安装/复制到Apache/Nginx或其他服务器。你必须使用此命令将证书复制到目标文件,请勿使用〜/ .acme.sh /文件夹中的证书文件,这些文件仅供内部使用,将来文件夹结构可能会更改。
Apache example:
1 | $ acme.sh --install-cert -d example.com \ |
Nginx example:
1 | $ acme.sh --install-cert -d example.com \ |
默认情况下,证书将每60天更新一次(可配置)。证书更新后,将通过以下命令自动重新加载Apache / Nginx服务:service apache2 force-reload或service nginx force-reload。
请注意:reloadcmd非常重要。该证书可以自动更新,但是,如果没有正确的“ reloadcmd”,该证书可能无法刷新到您的服务器(如nginx或apache),那么您的网站将无法在60天内显示更新的证书。
发行通配符证书.
很简单,只需提供一个通配符域作为-d参数。
1 | $ acme.sh --issue -d example.com -d '*.example.com' --dns dns_cf |
其他
- 如何续订证书
你不需要手动续订证书。所有证书将每60天自动更新一次。
但是,你也可以强制续订证书:
1 | $ acme.sh --renew -d example.com --force |
- 更新 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 |