使用Certbot工具從Let’s Encrypt獲取免費SSL證書

一、簡介

Let’s Encrypt 官方文檔:https://letsencrypt.org/zh-cn/

爲了在您的網站上啓用 HTTPS,您需要從證書頒發機構(CA)獲取證書(一種文件)。Let’s Encrypt 是一個證書頒發機構(CA)。

限制:

  • 每個註冊域名的證書數量(每週 50 張)
  • 一張證書中最多包含 100 個域名
  • 證書有效期爲 90 天,到期手動續費

Certbot 官方文檔:https://certbot.eff.org/docs/

Certbot是一種免費的開放源代碼軟件工具,可用於在手動管理的網站上自動使用Let’s Encrypt證書來啓用HTTPS。Certbot也是Let’s Encrypt官方推薦使用的一個客戶端。

二、Certbot使用

github下載地址:https://github.com/certbot/certbot/releases

2.1 安裝

# 下載
yum install -y wget 
wget https://github.com/certbot/certbot/archive/v1.5.0.tar.gz

# 解壓
tar -zxvf v1.5.0.tar.gz

# 查看幫助信息
cd certbot-1.5.0/
./certbot-auto --help

顯示

Usage: certbot-auto [OPTIONS]
A self-updating wrapper script for the Certbot ACME client. When run, updates
to both this script and certbot will be downloaded and installed. After
ensuring you have the latest versions installed, certbot will be invoked with
all arguments you have provided.

Help for certbot itself cannot be provided until it is installed.

  --debug                                   attempt experimental installation
  -h, --help                                print this help
  -n, --non-interactive, --noninteractive   run without asking for user input
  --no-bootstrap                            do not install OS dependencies
  --no-permissions-check                    do not warn about file system permissions
  --no-self-upgrade                         do not download updates
  --os-packages-only                        install OS dependencies and exit
  --install-only                            install certbot, upgrade if needed, and exit
  -v, --verbose                             provide more output
  -q, --quiet                               provide only update/error output;
                                            implies --non-interactive

All arguments are accepted and forwarded to the Certbot client when run.

2.2 獲取證書

先將域名解析到certbot服務器上。

證書獲取方式1:通過訪問80端口方式驗證

# 停止nginx
systemctl stop nginx

# 獲取證書, 
## --standalone 參數:使用內置web server
## --email 參數:管理員郵箱,證書到期前會發郵件到此郵箱提醒
## -d 參數:要綁定的域名,同一域的不同子域都要輸入.
./certbot-auto certonly --standalone --email [email protected] -d www.liuli.host
#啓動nginx
systemctl start nginx

證書獲取方式2:通過臨時目錄驗證

#--webroot 參數:指定使用臨時目錄的方式
## -w 參數:指定後面-d 域名所在的根目錄, 如果一次申請多個域的, 可以附加更多 -w...-d... 這段.
./certbot-auto certonly --webroot --email [email protected] -w /usr/share/nginx/html -d blog.liuli.host

完成上面的操作即可獲得 SSL 證書, 保存在 /etc/letsencrypt/live/根域名/ 目錄下會有四個文件

  • cert.pem : Apache服務器端證書
  • chain.pem :Apache根證書和中繼證書
  • fullchain.pem : Nginx所需要ssl_certificate文件
  • privkey.pem : 安全證書KEY文件

例如要配置nginx服務器,

#設置非安全連接永久跳轉到安全連接
server{
    listen 80;
    server_name www.liuli.host;
    #告訴瀏覽器有效期內只准用 https 訪問
    add_header Strict-Transport-Security max-age=15768000;
    #永久重定向到 https 站點
    return 301 https://$server_name$request_uri;
}
server {
   #啓用 https, 使用 http/2 協議, nginx 1.9.11 啓用 http/2 會有bug, 已在 1.9.12 版本中修復.
   listen 443 ssl http2;
   server_name www.liuli.host;
   #首頁
   index  index.php index.html index.htm;
   #網站根目錄
   root   /usr/share/nginx/html;
   #告訴瀏覽器當前頁面禁止被frame
   add_header X-Frame-Options DENY;
   #告訴瀏覽器不要猜測mime類型
   add_header X-Content-Type-Options nosniff;

   #證書路徑
   ssl_certificate /etc/letsencrypt/live/www.liuli.host/fullchain.pem;
   #私鑰路徑
   ssl_certificate_key /etc/letsencrypt/live/www.liuli.host/privkey.pem;
   #安全鏈接可選的加密協議
   ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
   #可選的加密算法,順序很重要,越靠前的優先級越高.
   ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
   #在 SSLv3 或 TLSv1 握手過程一般使用客戶端的首選算法,如果啓用下面的配置,則會使用服務器端的首選算法.
   ssl_prefer_server_ciphers on;
   #儲存SSL會話的緩存類型和大小
   ssl_session_cache shared:SSL:10m;
   #緩存有效期
   ssl_session_timeout 60m;

    location / {
        try_files $uri $uri/ /index.php?$args;  #修改內容
    }
}

2.3 續期證書

證書的有效期是3個月,你可以在證書過期前的30天內,進行續期,也可以進行腳本自動續期。
方式1:命令行手動更新

./certbot-auto renew

方式2:定時腳本

#!/usr/bin/env bash
# Auth: liuli
# Version: v1.0, 2020/7/7
# Sys: CentOS 7.8
# Features: 更新域名證書有效期
# Prepare:系統內已安裝certbot客戶端

if ! /root/certbot/certbot-1.5.0/certbot-auto renew > /var/log/letsencrypt/renew.log 2>&1 ; then
    echo Automated renewal failed:
    cat /var/log/letsencrypt/renew.log
    exit 1
fi

# 需要重啓nginx證書才能生效
systemctl restart nginx

添加定時任務,每月28號23點執行腳本

0 23 28 * * /bin/sh /root/certbotrenew.sh
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章