Let’s encrypt 提供期限为三个月的免费SSL证书,到期之后需要renew,官方还提供自动renew的工具certbot。
这里记录下使用Let’s Encrypt生成泛域名的通用证书。
一、获取certbot 客户端
2021/4/16更新,certbot-auto不再支持所有的操作系统,所以使用之前的下载方法,可能会报系统不再支持,所以提供官方最新的下载方法。
Skipping bootstrap because certbot-auto is deprecated on this system.
Your system is not supported by certbot-auto anymore.
Certbot cannot be installed.
Please visit https://certbot.eff.org/ to check for other alternatives.
之前的获取方法
# 下载 Certbot 客户端
$ wget -c https://dl.eff.org/certbot-auto -P /usr/local/bin/
# 设为可执行权限
$ chmod a+x /usr/local/bin/certbot-auto
$ certbot-auto --version
certbot 0.34.2
可以正常下载,但是在使用的过程中,会报
Skipping bootstrap because certbot-auto is deprecated on this system.
Your system is not supported by certbot-auto anymore.
所以这种方法我们暂时弃用,采用官方最新的方法。
官方最新安装方法
(1)、安装epel 和 snapd:
# 先安装epel
yum install epel-release
# 安装snapd
yum install snapd
(2)、启用snapd.socket:
systemctl enable --now snapd.socket
(3)、创建/var/lib/snapd/snap和/snap之间的链接:
ln -s /var/lib/snapd/snap /snap
(4)、重启系统:
重启之后,记得重启自己的nginx,php,mysql等服务
reboot
(5)、更新snap至最新版本:
snap install core
snap refresh core
(6)、卸载之前的certbot:
# 卸载之前的certbot
yum remove certbot
# 删除certbot相关文件:
rm /usr/local/bin/certbot-auto
# 删除certbot附加软件包:
rm -rf /opt/eff.org/certbot
(7)、通过snap安装certbot:
snap install --classic certbot
(8)、创建/snap/bin/certbot软链接:
ln -s /snap/bin/certbot /usr/bin/certbot
二、开始申请证书
假设域名为 *.example.com
certbot certonly -d *.example.com --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
certonly,表示安装模式,Certbot 有安装模式和验证模式两种类型的插件。
-d 为那些主机申请证书,如果是通配符,输入 *.example.com
–manual 表示手动安装插件,Certbot 有很多插件,不同的插件都可以申请证书,用户可以根据需要自行选择
–preferred-challenges dns,使用 DNS 方式校验域名所有权
–server,Let’s Encrypt ACME v2 版本使用的服务器不同于 v1 版本,需要显示指定。
执行完以上命令之后,就是命令行的输出,根据提示输入相应内容:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): fate1028@163.com #用于安全以及续约通知的邮箱
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A #是否同意相关协议条款
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N #是否订阅相关的邮件
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.
Are you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y #询问是否对域名和机器(IP)进行绑定
以上确认后继续,以下提示要求配置 DNS TXT 记录,从而校验域名所有权,也就是判断证书申请者是否有域名的所有权。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:
OGWxK87UxvB5iMp2tn-tfnB7r_kICS3oSRHyo0ouwzU
Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue ## 这里切记自己验证生效后再回车执行
上面输出要求给 _acme-challenge.example.com 配置一条 TXT 记录,在没有确认 TXT 记录生效之前不要回车执行。请自行登录域名管理系统,在域名解析里面配置的TXT记录.
注意的是,域名为 *.example.com时,可以直接添加如上的解析记录,但是如果你还想生成二级范域名例如 *.test.example.com的时候,你需要在完成 *.example.com 一切的域名配置之后,再次执行第二步申请 *.test.example.com的证书,值得注意的是,这时候再添加主机记录时,主机记录不要填_acme-challenge,改为 *.test 即可
如图
确认解析生效后,回车执行,输出如下信息
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/example.com/privkey.pem
Your cert will expire on 2019-08-29. To obtain a new or tweaked
version of this certificate in the future, simply run certbot-auto
again. To non-interactively renew *all* of your certificates, run
"certbot-auto renew"
- Your account credentials have been saved in your Certbot
configuration directory at /etc/letsencrypt. You should make a
secure backup of this folder now. This configuration directory will
also contain certificates and private keys obtained by Certbot so
making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
此时我们的证书已经申请成功了,证书都保存在如下目录中:
/etc/letsencrypt/live/example.com/
到此,我们就演示了如何在 Let’s Encrypt 申请免费的通配符证书。
三、Nginx配置
ssl_certificate /etc/letsencrypt/live/ example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ example.com/privkey.pem;
配置完记得重启nginx
四、续约
上个环节最后一步最重要事项中提示Let’s encrypt 的免费证书默认有效期为 90 天,到期后如果要非交互式续期所有证书,可以执行如下命令:
$ 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目录中,建议定期备份