如何使用 Certbot 建立免費的 TLS/SSL 網域憑證並自動產生 PFX 憑證
source link: https://blog.miniasp.com/post/2021/05/09/Create-SSL-TLS-certificates-from-LetsEncrypt-using-Certbot-2
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
我之前有寫過一篇 如何使用 Certbot 命令列工具建立免費的 TLS/SSL 頂層網域憑證 文章,當時的情境是我的域名是 頂層網域 (naked domains)(Top-Level Domain)(TLD),必須用「手動」方式申請。今天這篇文章,我要示範如何「全自動」申請與更新憑證,並自動產生 Kestrel 所需的 *.pfx
(PKCS#12) 憑證。
首次建立憑證與設定自動更新排程 (www.your-domain.com.tw)
本篇文章將會以 Ubuntu 18.04.1 LTS 為主進行示範,但其實使用的方式大同小異。
-
使用 SSH 登入 Ubuntu 18.04.1 LTS 主機
-
先確認 snapd 已升級到最新版本
sudo snap install core; sudo snap refresh core
-
確認現有已安裝的 Certbot 已確實移除
sudo apt-get remove certbot
因為安裝 Certbot 的過程中會自動註冊一些排程工作,所以為了避免舊版本的 Certbot 影響到新的安裝,建議先移除舊版。
-
使用
snap
安裝certbot
套件sudo snap install --classic certbot
安裝過程中會自動安裝一個
snap.certbot.renew.service
系統服務,此服務專門用來自動更新憑證之用。你可以透過systemctl list-units --all --type=service --no-pager | grep certbot
查詢該服務,或透過service snap.certbot.renew status
查詢目前的執行狀態。剛安裝好的 certbot 並不會有服務執行。 -
到網站建立
/.well-known/acme-challenge/
資料夾並測試網址可以正常連接cd $WWWROOT/ mkdir -p .well-known/acme-challenge echo OK > .well-known/acme-challenge/abc curl https://www.your-domain.com.tw/.well-known/acme-challenge/abc rm -f .well-known/acme-challenge/abc
這裡請將
$WWWROOT
替換成你目前的網站根目錄。 -
sudo certbot certonly --webroot
注意:所有偵錯訊息都會放在
/var/log/letsencrypt/letsencrypt.log
檔案中。Saving debug log to /var/log/letsencrypt/letsencrypt.log Plugins selected: Authenticator webroot, Installer None Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [email protected] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server. Do you agree? - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: Y - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Would you be willing, once your first certificate is successfully issued, to share your email address with the Electronic Frontier Foundation, a founding partner of the Let's Encrypt project and the non-profit organization that develops Certbot? We'd like to send you email about our work encrypting the web, EFF news, campaigns, and ways to support digital freedom. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - (Y)es/(N)o: N Account registered. Please enter in your domain name(s) (comma and/or space separated) (Enter 'c' to cancel): www.your-domain.com.tw Requesting a certificate for www.your-domain.com.tw Performing the following challenges: http-01 challenge for www.your-domain.com.tw Input the webroot for www.your-domain.com.tw: (Enter 'c' to cancel): $WWWROOT/ Waiting for verification... Cleaning up challenges IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/www.your-domain.com.tw/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/www.your-domain.com.tw/privkey.pem Your certificate will expire on 2021-07-20. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
-
確認憑證輸出結果
憑證輸出路徑
/etc/letsencrypt/live/www.your-domain.com.tw/
主要 Certbot 設定檔
/etc/letsencrypt/renewal/www.your-domain.com.tw.conf
-
憑證輸出路徑下的憑證檔案
伺服器憑證
fullchain.pem
(此檔案將包括完整的憑證鏈)私密金鑰檔
privkey.pem
(此檔案是未加密的金鑰檔) -
修改 Apache 的 SSL 相關設定
檔案路徑:
/etc/apache2/sites-enabled/www.your-domain.com.tw.conf
SSLCertificateFile /etc/ssl/certs/www.your-domain.com.tw.crt SSLCertificateKeyFile /etc/ssl/private/www.your-domain.com.tw.key SSLCertificateChainFile /etc/ssl/certs/twca-chain.crt
SSLCertificateFile /etc/letsencrypt/live/www.your-domain.com.tw/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/www.your-domain.com.tw/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/www.your-domain.com.tw/chain.pem
-
重新啟動 Apache 服務
sudo service apache2 reload
-
驗證憑證自動更新 (Renew)
sudo certbot renew --webroot --dry-run
上述命令會自動設定一個系統排程,你可以透過
systemctl list-timers --no-pager
或systemctl list-unit-files --type timer
查詢到一個名為snap.certbot.renew.timer
的排程作業(UNIT)。 -
調整 Systemd 的 Timer 排程
請將 [Timer] 區段修改為 每天早上 4:30 執行一次:
vi /etc/systemd/system/timers.target.wants/snap.certbot.renew.timer
[Timer] Unit=snap.certbot.renew.service OnCalendar=*-*-* 04:30
關於定時的格式可以參考 systemd/Timers - ArchWiki 文件說明。
取得憑證後自動產生 PFX 憑證
-
產生 PFX (PKCS#12) 憑證檔
cd /etc/letsencrypt/live/www.your-domain.com.tw/ PASSWORD=SD9QT4bVq openssl pkcs12 -export -out your-domain.pfx -inkey "privkey.pem" -in "cert.pem" -certfile "fullchain.pem" -password "pass:$PASSWORD" cp your-domain.pfx $YourPfxLocation/your-domain.pfx
注意: 請將
$YourPfxLocation
替換為你的 PFX 放置路徑! -
重新啟動網站
service MyService reload
注意: 我有將我的 Kestrel 網站伺服器封裝成一個服務,假設服務名稱為
MyService
。如果網站可以正常瀏覽,才能確認你所產生的 PFX 檔案正確無誤,這時才能進行下一步。
-
建立一個
/etc/letsencrypt/renewal-hooks/post/certbot-post-hook.sh
檔案這個檔案會在每次成功 Renew 憑證後自動執行,如此一來你就可以將自動產生 PFX 與重新啟動網站伺服器的動作,寫在這個 Shell Script 裡面,這樣就能完成自動化了!
#!/bin/sh cd /etc/letsencrypt/live/www.your-domain.com.tw/ PASSWORD=SD9QT4bVq openssl pkcs12 -export -out your-domain.pfx -inkey "privkey.pem" -in "cert.pem" -certfile "fullchain.pem" -password "pass:$PASSWORD" cp your-domain.pfx $YourPfxLocation/your-domain.pfx service MyService status
變更執行權限
chmod 700 /etc/letsencrypt/renewal-hooks/post/certbot-post-hook.sh
-
驗證憑證自動更新 (Renew)
sudo certbot renew --webroot --dry-run
請記得加上
--dry-run
測試目前的腳本與設定可以正確的更新憑證並自動建立 PFX 憑證與重新啟動網站伺服器。
- 如何使用 Certbot 建立免費的 TLS/SSL 網域憑證並自動產生 PFX 憑證
我之前有寫過一篇 如何使用 Certbot 命令列工具建立免費的 TLS/SSL 頂層網域憑證 文章,當時的情境是我的域名是 頂層網域 (naked domains)(Top-Level Domain
- 如何使用 Certbot 命令列工具建立免費的 TLS/SSL 頂層網域憑證
許多有在玩 Angular 前端框架的人都知道,目前的 Angular 官網的正體中文版(https://angular.tw)都是由我負責維護。該網站我目前架設於 Azure App Service
- 如何使用 MakeCert 建立開發測試用途的自簽憑證 (Self-Signed Certificate)
無論我們開發網站或撰寫應用程式,都有可能會需要手動建立測試用的憑證,好讓我們的測試環境可以有效模擬像是 TLS/SSL 連線、發送 S/MIME 郵件、簽署應用程式、...等等。這篇文章我想來示範幾種
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK