1. 程式人生 > 實用技巧 >certbot生成並使用Let's Encrypt免費SSL證書(webroot方式)

certbot生成並使用Let's Encrypt免費SSL證書(webroot方式)

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 的稽核

更多限制可以去官網檢視

參考

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