certbot生成并使用Let's Encrypt免费SSL证书(webroot方式)
前言
Let's Encrypt: 是一个由非营利性组织 互联网安全研究小组(ISRG)提供的免费、自动化和开放的证书颁发机构(CA). 官网地址:https://letsencrypt.org
certbot: Let's Encrypt 官方推荐我们使用生成证书的工具.官网地址: https://certbot.eff.org/
安装certbot
CentOs安装
yum -y install certbot
Ubuntu
apt-get update
//用于添加ppa源的小工具,ubuntu server默认没装
apt-get install software-properties-common
//把ppa源添加到source list中
add-apt-repository ppa:certbot/certbot
apt-get update
apt-get install certbot
//检查是否安装成功
certbot --version
生成证书
webroot方式: 通过访问网站目录中的验证并生成证书; 所以要确保域名能够正常访问.
生成证书命令:
certbot certonly --webroot -w 网站目录 -d 域名 [-w 网站目录 -d 域名]
证书存放目录: /etc/letsencrypt/live/域名/
nginx中使用证书
主要添加监听443端口, 以及指向证书目录,如果是docker安装的nginx环境需要把证书移动到数据卷映射目录
配置内容参考:
server {
listen 80;
# 监听443
listen 443 ssl;
server_name localhost;
root /usr/share/nginx/html/test;
location / {
index index.html index.htm index.php;
}
# 配置服务器证书
ssl_certificate /etc/letsencrypt/live/you.domain.com/fullchain.pem;
# 配置服务器私钥
ssl_certificate_key /etc/letsencrypt/live/you.domain.com/privkey.pem;
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
自动更新
把下面命令加入定时器,每月执行,可达到自动更新的效果
certbot renew
使用限制
- 申请的证书有效期只有90天
- 同一个顶级域名下的二级域名,一周做多申请 20 个
- 一个域名一周最多申请 5 次
- 1 小时最多允许失败 5 次
- 请求频率需要小于 20 次/s
- 一个 ip 3 小时内最多创建 10 个账户
- 一个账户最多同时存在 300 个 pending 的审核
更多限制可以去官网查看
补充: 申请通配符域名证书
执行命令
certbot-auto certonly -d *.example.com --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory
-d 申请证书的域名,如果是通配符域名输入 *.example.com
–manual 手动安装插件
–preferred-challenges dns 使用 DNS 方式校验域名所有权
–server,Let’s Encrypt ACME v2 版本使用的服务器不同于 v1 版本,需要显示指定
执行命令后根据需求设置选项, 不知道的都Y
即可, 最终停止在下面这里,不要回车, 这里要先去配置域名解析
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name
_acme-challenge.example.com with the following value:
dh-koijcvTKgH6hZ2368hKlnPpikGWS5U6T7JIpsKU8
Before continuing, verify the record is deployed.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue
设置域名解析
从上面获取到主机记录和记录值,到阿里云控制台 -> 域名 -> 解析设置(选择域名进入后) -> 添加记录; (添加记录后稍微几分钟, 让解析记录生效)
记录值: TXT
主机记录: _acme-challenge.example.com
记录值: dh-koijcvTKgH6hZ2368hKlnPpikGWS5U6T7JIpsKU8
设置如图所示:
然后回到刚才的命令行Press Enter to Continue
,回车后能看到证书生成成功了, 证书目录和配置nginx和上面的步骤相同.
参考
https://blog.csdn.net/weixin_45052750/article/details/108733647
https://www.jianshu.com/p/6ea81a7b768f
https://www.jianshu.com/p/43e74cddba45
https://www.chenxublog.com/2019/10/19/nginx-lets-encrypt-auto-renew.html