利用Certbot自动签发及续签Let's Encrypt免费SSL证书(宝塔nginx)
由于目前免费SSL证书)有效期通常只有90天,在到期前需要手动申请、部署证书较为麻烦,甚至会因为未及时续签证书导致网址无法访问。
故而需要一款自动化处理方法,能实现自动签发、自动续签,使用Certbot结合Let's Encrypt就可以实现该需求。
什么是Certbot
Certbot是一款由Mozilla维护的开源项目,同时也是由Electronic Frontier Foundation (EFF) 创建的,它主要用于自动化获取和安装SSL/TLS证书,特别是与Let's Encrypt服务结合使用时,使得网站的HTTPS化变得简单且免费。
什么是Let's Encrypt
Let's Encrypt是一个由非营利性组织互联网安全研究小组(ISRG)提供的免费、自动化和开放的证书颁发机构(CA),旨在以自动化流程消除手动创建和安装证书的复杂流程,并推广使万维网服务器的加密连接无所不在,为安全网站提供免费的SSL/TLS证书。
服务器环境说明
1、Linux CentOS 7.x 64位
2、已安装nginx,示例中nginx是通过 yum 的方式进行安装
3、以下示例代码中的 yourdomain 代表实际需要申请证书的域名,具体实际时请替换为正式域名
4、由于示例中Certbot是采用http-01方式进行验证,所以yourdomain必须解析至服务器,以便能通过验证
安装Certbot
yum -y install certbot签发证书
在签发过程中需要保证域名已完成解析,中途会要求输入email地址并选择是否同意相关协议。
前提条件:需先完成nginx的站点conf配置,再进行证书的签发
# certbot certonly --webroot -w 网站目录 -d 域名 [-w 网站目录 -d 域名]
certbot certonly --webroot -w /www/wwwroot/test.xlyw.top -d test.xlyw.top
# 输入邮箱
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
(Enter 'c' to cancel): 1361183856@qq.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# y 同意相关协议
# 申请成功
successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/test.xlyw.top/fullchain.pem
Key is saved at: /etc/letsencrypt/live/test.xlyw.top/privkey.pem
This certificate expires on 2025-01-29.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
# 证书目录:/etc/letsencrypt/live/域名/nginx中使用证书
参考nginx部署证书配置
注意:证书目录配置为/etc/letsencrypt/live/域名/ 后续的证书自动更新才有效
自动更新
把下面命令加入定时器,每月执行,可达到自动更新的效果
certbot renew
# 加入计划任务
* * * */3 * certbot renew使用限制
申请的证书有效期只有90天
同一个顶级域名下的二级域名,一周做多申请 20 个
一个域名一周最多申请 5 次
1 小时最多允许失败 5 次
请求频率需要小于 20 次/s
一个 ip 3 小时内最多创建 10 个账户
一个账户最多同时存在 300 个 pending 的审核
更多限制可以去官网查看