Let'sEncrypt免费申请泛域名HTTPS证书

Let’sEncrypt 介绍

Let’s Encrypt: https://letsencrypt.org , 是一个免费的、自动化的、开放的证书颁发机构。它的全球SSL证书市场份额已超过50%,得到主流浏览器和厂商的认可与支持。

Let’s Encrypt 证书提供免费的申请,但没有高额的安全保险,不具备点对点的技术支持,而且申请过程比较复杂,适合具有一定的专业知识的个人站长申请。并且每次申请到的SSL证书有效期只有90天,但是可以通过脚本实现提前自动续约达到自动化永久免费使用的目的。

在2018年5月,Let’s Encrypt 发布了免费泛域名通配符 SSL 证书:[https://community.letsencrypt.org/t/acme-v2-and-wildcard-certificate-support-is-live/55579] ,在 HTTPS 全面普及的当下,越来越多的开发者选择了 Let’s Encrypt 证书。

可自动续订的方案

Let’s Encrypt 官方提供了一系列的申请方法文档,但流程比较复杂,为了简化申请流程,我们可以在Github上找到这个仓库:[https://github.com/acmesh-official/acme.sh]

acme.sh 的具体使用说明见:[https://github.com/acmesh-official/acme.sh/wiki/说明]

安装

1
curl  https://get.acme.sh | sh

或者

1
wget -O -  https://get.acme.sh | sh

申请泛域名证书

acme.sh 脚本默认安装在用户主目录

/root/.acme.sh/

申请证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
./acme.sh --issue  -d *.test.com --dns --yes-I-know-dns-manual-mode-enough-go-ahead-please

[Mon May 20 14:31:39 CST 2019] Creating domain key
[Mon May 20 14:31:39 CST 2019] The domain key is here: /root/.acme.sh/*.test.com/*.test.com.key
[Mon May 20 14:31:39 CST 2019] Single domain='*.test.com'
[Mon May 20 14:31:39 CST 2019] Getting domain auth token for each domain
[Mon May 20 14:31:41 CST 2019] Getting webroot for domain='*.test.com'
[Mon May 20 14:31:41 CST 2019] Add the following TXT record:
[Mon May 20 14:31:41 CST 2019] Domain: '_acme-challenge.test.com'
[Mon May 20 14:31:41 CST 2019] TXT value: '-CtUNqZ72M8E825dLzc4YRQyshby65Ut5jeGYZcMA'
[Mon May 20 14:31:41 CST 2019] Please be aware that you prepend _acme-challenge. before your domain
[Mon May 20 14:31:41 CST 2019] so the resulting subdomain will be: _acme-challenge.test.com
[Mon May 20 14:31:41 CST 2019] Please add the TXT records to the domains, and re-run with --renew.
[Mon May 20 14:31:41 CST 2019] Please check log file for more details: /root/.acme.sh/acme.sh.log

PS: 解析对应的 TXT 记录,等 TXT 记录生效后,使用如下命令验证域名证书

颁发证书

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
./acme.sh --renew -d *.test.com --dns  --yes-I-know-dns-manual-mode-enough-go-ahead-please
[Mon May 20 14:39:54 CST 2019] Renew: '*.test.com'
[Mon May 20 14:39:54 CST 2019] Single domain='*.test.com'
[Mon May 20 14:39:54 CST 2019] Getting domain auth token for each domain
[Mon May 20 14:39:54 CST 2019] Verifying: *.test.com
[Mon May 20 14:39:58 CST 2019] Success
[Mon May 20 14:39:58 CST 2019] Verify finished, start to sign.
[Mon May 20 14:39:58 CST 2019] Lets finalize the order, Le_OrderFinalize: https://acme-v02.api.letsencrypt.org/acme/finalize/57434696/460828648
[Mon May 20 14:40:00 CST 2019] Download cert, Le_LinkCert: https://acme-v02.api.letsencrypt.org/acme/cert/0394d4c6ac1022960d1b4873927b1749d551
[Mon May 20 14:40:00 CST 2019] Cert success.

[Mon May 20 14:40:00 CST 2019] Your cert is in /root/.acme.sh/*.test.com/*.test.com.cer
[Mon May 20 14:40:00 CST 2019] Your cert key is in /root/.acme.sh/*.test.com/*.test.com.key
[Mon May 20 14:40:00 CST 2019] The intermediate CA cert is in /root/.acme.sh/*.test.com/ca.cer
[Mon May 20 14:40:00 CST 2019] And the full chain certs is there: /root/.acme.sh/*.test.com/fullchain.cer
[Mon May 20 14:40:00 CST 2019] It seems that you are using dns manual mode. please take care: The dns manual mode can not renew automatically, you must issue it again manually. You'd better use the other modes instead.
[Mon May 20 14:40:00 CST 2019] Call hook error.

PS: 这里使用的是 --renew

这里会生成.cer密钥

1
2
3
4
5
6
ll \*.test.com/
total 28
-rw-r--r-- 1 root root 1648 May 20 14:40 ca.cer
-rw-r--r-- 1 root root 3551 May 20 14:40 fullchain.cer
-rw-r--r-- 1 root root 1903 May 20 14:40 *.test.com.cer
-rw-r--r-- 1 root root 1675 May 20 14:31 *.test.com.key

证书续期

./acme.sh --renew -d *.test.com

默认证书只有3个月时间,到期后自执行上面命令续期,也要添加自动任务,自动续期

Ⅰ:配合DNSPOD API免手动解析快速获取证书

一、域名解析
将需要申请证书的域名更改DNS服务器添加到DNSPOD进行解析。

二、创建API
登入 DNSPOD 后台,在"安全设置"中,添加创建API。保存好ID和Token。

三、配置API

1
2
3
4
5
6
7
vim /etc/profile

export DP_Id="ID数字"
export DP_Key="Token字符串"
ID和KEY分别对应获取的API的ID和Token

source /etc/profile

四、签发证书SSL

/root/.acme.sh/acme.sh --issue -d test.com -d *.test.com --dns dns_dp --debug

Ⅱ、阿里云云解析NDS免手动解析快速获取证书

一、登录阿里云控制台–>云解析DNS–>账户

生成云解析账号的api的key 和Secret

二、配置API,设置2个变量

1
2
3
/root/.acme.sh/dnsapi/dns_ali.sh
export Ali_Key="阿里云key"
export Ali_Secret="阿里云Secret"

三、签发证书SSL

acme.sh --issue --dns dns_ali --force -d domain.com -d *.domain.com --debug

证书文件都被自动存放在 /root/.acme.sh/ 对应的域名文件夹中.

注意, 默认生成的证书都放在安装目录下: ~/.acme.sh/.com, 请不要直接使用此目录下的文件。

例如: 不要直接让 nginx/apache 的配置文件使用这下面的文件. 这里面的文件都是内部使用, 而且目录结构可能会变化.

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

1
2
3
4
acme.sh  --installcert  -d  <domain>.com   \
--key-file /etc/nginx/ssl/<domain>.key \
--fullchain-file /etc/nginx/ssl/fullchain.cer \
--reloadcmd "service nginx force-reload"

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

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

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

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

通过网站申请(不需要实现自动续订)

如果不需要实现SSL域名证书自动续订,可以在下面的网站申请Let’s Encrypt免费泛域名SSL证书
地址:[https://www.sslforfree.com/]
申请泛域名证书填写示例:
*.xxx.com xxx.com