一,
1.获取certbot-auto
# 下载
wget https://dl.eff.org/certbot-auto
# 设为可执行权限
chmod a+x certbot-auto
2.开始申请证书
客户在申请 Let’s Encrypt 证书的时候,需要校验域名的所有权,证明操作者有权利为该域名申请证书,目前支持三种验证方式:
dns-01:给域名添加一个 DNS TXT 记录。
http-01:在域名对应的 Web 服务器下放置一个 HTTP well-known URL 资源文件。
tls-sni-01:在域名对应的 Web 服务器下放置一个 HTTPS well-known URL 资源文件。
- 而申请通配符证书,只能使用 dns-01 的方式
介绍下相关参数:
certonly,表示安装模式,Certbot 有安装模式和验证模式两种类型的插件。
--manual 表示手动安装插件,Certbot 有很多插件,不同的插件都可以申请证书,用户可以根据需要自行选择
-d 为那些主机申请证书,如果是通配符,输入 *.xxx.com(你自己的域名)
--preferred-challenges dns-01,使用 DNS 方式校验域名所有权
--server,Let's Encrypt ACME v2 版本使用的服务器不同于 v1 版本,需要显示指定。
# 注xxx.com请根据自己的域名自行更改,通配符证书必须指定 --server 参数,并使用 acme v2 版本
./certbot-auto -d "*.xxx.com" --manual --preferred-challenges dns-01 certonly --server https://acme-v02.api.letsencrypt.org/directory
或者:dns不同
/home/certbot-auto certonly --preferred-challenges dns --manual -d xxx.com --server https://acme-v02.api.letsencrypt.org/directory
执行完这一步之后,会下载一些需要的依赖,稍等片刻之后,会提示输入邮箱,随便输入都行【该邮箱用于安全提醒以及续期提醒】
注意,申请通配符证书是要经过DNS认证的,按照提示,前往域名后台添加对应的DNS TXT记录。
添加之后,不要心急着按回车,先执行dig xxxx.xxx.com txt
或者dig -t txt xxxx.xxx.com 确认解析记录是否生效,生效之后再回去按回车确认
$ dig -t txt _acme-challenge.example.com @8.8.8.8
; <<>> DiG 9.10.6 <<>> -t txt _acme-challenge.example.com @8.8.8.8
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20013
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;_acme-challenge.chat186.com. IN TXT
;; ANSWER SECTION:
_acme-challenge.example.com. 1798 IN TXT "OGWxK87UxvB5iMp2tn-tfnB7r_kICS3oSRHyo0ouwzU"
;; Query time: 258 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Fri May 31 20:33:01 CST 2019
;; MSG SIZE rcvd: 112
- dig 命令安装:yum install bind-utils
到了这一步后,大功告成!!! 证书存放在/etc/letsencrypt/live/xxx.com/里面
校验证书信息,输入如下命令:
$ openssl x509 -in /etc/letsencrypt/live/example.com/cert.pem -noout -text
# 可以看到证书包含了 SAN 扩展,该扩展的值就是 *.example.com
...
Authority Information Access:
OCSP - URI:http://ocsp.int-x3.letsencrypt.org
CA Issuers - URI:http://cert.int-x3.letsencrypt.org/
X509v3 Subject Alternative Name:
DNS:*.example.com
...
证书续约:
要续期的话,执行certbot-auto renew
就可以了
$ certbot-auto renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not yet due for renewal
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following certs are not due for renewal yet:
/etc/letsencrypt/live/example.com/fullchain.pem expires on 2019-08-29 (skipped)
No renewals were attempted.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
由于还没到期所以被忽略掉了,以上的非交互式续约的前提是存在续期的账号凭证,默认第一次是保存在/etc/letsencrypt目录中,建议定期备份。
二,
注:这样的证书无法应用到主域名xxx.com
上,如需把主域名也增加到证书的覆盖范围,请在开始申请证书步骤的那个指令把主域名也加上,如下: 需要注意的是,这样的话需要修改两次解析记录
./certbot-auto -d "*.xxx.com" -d "xxx.com" --manual --preferred-challenges dns-01 certonly --server https://acme-v02.api.letsencrypt.org/directory