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