利用 Let's Encrypt 來自動簽署並更新 SSL 憑證 (wildcard)

前言

本篇主要分享,如何利用 Let's Encryptcert bot 來自動簽署以及更新 SSL 憑證 (wildcard)
什麼是 wildcard? 比方說你的域名是 example.com, 那一個 wildcard 的憑證將會適用於以下的網站:

abc.example.com
aaa.example.com
whatever.example.com

wildcard 憑證不包含以下的域名, 但是照文章內的指令申請, 可以一併申請

example.com

總之不管前面是什麼,都可以適用。
要使用 Certbot 來取得 wildcard 的 certificate 需要安裝 DNS 提供商的插件,或者其他的手動插件,本篇文章採用 DNS 提供商為 Google 的情況




參考網頁

官網




環境

  • Server: NginX
  • OS: Ubuntu 18.04
  • DNS Provider: Google



確認你的 DNS Provider 是否支援

看你的 DNS Provider 有無被 Certbot 支援, 看這份清單


沒支援

如果你的 DNS provider 沒有被支援,在這停下。 參考這份文件,使用手動插件運行 Certbot

  • 執行以下程式碼
certbot certonly --manual -d *.example.com -d example.com --preferred-challenges dns
  • 執行後先別按下 Enter, 請看下一步
  • 到你的 DNS 服務商去新增相對應的 hostName 以及指向的 value, type 為 TXT
  • dig -t txt hostName 確認該 hostName 是否指向 上一步新增的 value
  • 回到 terminal 按下 enter
  • 大功告成

有支援

如果你的 DNS provider 有支援,順著以下的步驟繼續




SSH 到你的 Server

SSH 到你的 server, 該使用者須擁有 sudo 權限




加入 Certbot PPA

你將需要把 Certbot PPA 加入到 repository 清單,執行以下指令來加入:

sudo apt-get update; sudo apt-get install software-properties-common; sudo add-apt-repository universe; sudo add-apt-repository ppa:certbot/certbot; sudo apt-get update



安裝 Certbot

執行以下指令來安裝 Certbot

sudo apt-get install certbot python-certbot-nginx



安裝正確的 DNS 插件

執行以下指令以安裝插件,本教程中使用的 DNS 提供商為 Google

範例

sudo apt-get install python3-certbot-dns-\<PLUGIN\>

本教程情境

sudo apt-get install python3-certbot-dns-google



設定 credentials

你將需要設定 DNS credentials。
依循各 DNS 提供商文件中的 “Credentials” 區塊來建立或存取適當的 credential 設定檔
點擊這份文件 來尋找指定 DNS 提供商的 credential 教學。

設定 Google Credentials

需要哪些權限?

由 Certbot 的 Google 文件, 可以得知 Certbot 需要的權限如下:

dns.changes.create
dns.changes.get
dns.managedZones.list
dns.resourceRecordSets.create
dns.resourceRecordSets.delete
dns.resourceRecordSets.list
dns.resourceRecordSets.update

建立 role

Navigation menu > IAM & admin > role , 進到 role 頁面後點擊 CRETE ROLE, 然後點擊 ADD PERMISSION 按鈕。

在 filter 欄位中,逐一輸入上面的權限來將需求的權限加入

全部都加入之後,請在 title 以及其他欄位依照個人需求填入名稱,若覺得預設挺好的,也可以保留預設即可。

選擇 CREATE, 共有七項。


建立 Service Account

  1. 從左側選單點擊 Navigation menu > IAM & admin > Service accounts , 進到 Service accounts 頁面後,點擊上方 CREATE SERVICE ACCOUNT 按鈕。

  2. 依照個人喜好輸入 Service account name 以及 Service account description

  3. 選擇 role, 想當然爾,自然是輸入我們上一步建立的 role 啦! 然後點擊 CONTINUE

  4. 最後是建立一把 json key, 點擊 CREATE KEY

  5. 最後點擊 DONE, 結束這一回合




選擇你想要怎麼樣執行 Certbot, 是單純拿到 certificate 或要安裝

這邊官網有提供兩種方式,一種是全自動安裝,不只獲得憑證還幫你安裝,另外一種是只有安裝。
不過我看過 Google 的插件之後,發現好像只有獲得憑證的選項,因此下面的指令會是單純獲得憑證的方式,若有人知道怎麼同時安裝的,也可以提供哦!

下面指令為 Google 版的,其他版的請勿使用哦!

certbot certonly --dns-google --dns-google-credentials yourCredentailLocation -d *.example.com. -d example.com. -i nginx

上面的 yourCredentailLocation 為上面我的建立的 Google Service Account JSON Key, 假如我放在 /home/ray/serviceAccountKey.json, 那上面的指令將會如下:

certbot certonly --dns-google --dns-google-credentials /home/ray/serviceAccountKey.json -d *.example.com. -d example.com. -i nginx



拿到憑證之後,我們可以設定一個測試站來看 wildcard 憑證是否有效

  • 建立測試 site 檔案
    vim example.com
server {
server_name abc.example.com;
listen 80;
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
}

憑證的位置我們在上一步成功拿到憑證之後,輸出訊息會顯示憑證放置位置。 我們也可以把它放到任何我們想要的地方。

  • 測試 syntax

    nginx -t
  • 重啟 Nginx

    systemctl restart nginx.service
  • 到域名服務商去新增 abc.example.com 這筆 A record, 然後 IP 為你 Server 的位置

  • 給 DNS 一些時間來生效

  • 在瀏覽器輸入 https://abc.example.com 來拜訪測試站,若成功的話可以看到 Welcome to nginx!




測試自動更新

Certbot 套件附有 cron job 或是 systemd timer,可以在憑證過期前自動的更新你的憑證。 你將不需要再執行 Certbot, 除非你更動了你的設定。 你可以運行下面的指令來測試自動更新:

sudo certbot renew --dry-run

自動更新 certbot 的指令安裝在以下位置:

/etc/crontab/
/etc/cron.*/*
systemctl list-timers



確認 Certbot 正確的設定我們的憑證

拜訪你的網站,然後看左方鎖頭的圖案。
如果你想看更多資訊,試試看這個網站
故障排除請參考這份文件
若需要更多客製化的設定,請參考這份文件
若覺得 Certbot 不錯,也可以贊助




失敗了?

如果有看到錯誤訊息如下:
Encountered error deleting TXT record: <HttpError 412 when requesting https://dns.googleapis.com/dns/v1/projects/yourProject/managedZones/yourZoneNumber/changes?alt=json returned "Precondition not met for 'entity.change.deletions[0]'">

那檢查一下是不是之前有手動申請憑證,並且驗證完畢之後沒有把 TXT 檔刪掉

Netdata 學習筆記 Cloud Functions 初探 - 主控台

留言

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×