顯示廣告
隱藏 ✕
看板 KnucklesNote
作者 Knuckles (站長 那克斯)
標題 [CentOS7] Apache使用Certbot申請Let's Encrypt的SSL憑證
時間 2020-07-29 Wed. 05:22:11


[圖]


Let's Encrypt 是免費的網頁 SSL 憑證
每三個月要重新認證一次

使用 Certbot 可以簡化認證流程,也可以設定自動重新認證
參考官網說明 https://certbot.eff.org/instructions?ws=apache&os=centosrhel7&tab=standard

要先安裝 EPEL (Extra Packages for Enterprise Linux) 擴充資源庫
$ sudo yum install epel-release

安裝 apache 的 SSL 模組,重啟 apache
$ sudo yum install mod_ssl
$ sudo systemctl restart httpd


* 使用 yum 的安裝方法 (已不建議使用)
安裝 Certbot
$ sudo yum install certbot python2-certbot-apache
相依的套件中會安裝 apache 的 mod_ssl


使用通用套件管理器 snap 的安裝方法 (官網建議使用)

安裝 snapd,參考 snap 官網說明
$ sudo yum install snapd

設定 snapd 為開機執行
$ sudo systemctl enable --now snapd.socket

建立一個 /snap 軟連結
$ sudo ln -s /var/lib/snapd/snap /snap

確認 snap 已更新至最新版
$ sudo snap install core
$ sudo snap refresh core

出現以下錯誤訊息的話,過一會再執行就好
error: too early for operation, device not yet seeded or device model not acknowledged

若之前有用 yum 安裝過 certbot 的話,要先移除
$ sudo yum remove certbot

使用 snap 安裝 certbot
$ sudo snap install --classic certbot

建立一個軟連結確保 certbot 可執行
$ sudo ln -s /snap/bin/certbot /usr/bin/certbot



使用 Certbot 申請 Let's Encrypt 的 SSL 憑證
$ certbot certonly --apache -w /var/www/xxx/ -d xxx.disp.cc

其中:
certonly 代表只申請憑證,不要自動修改 http.conf
--apache 代表網頁伺服器是使用Apache
-w /var/www/xxx/ 改成自己網站的根目錄
-d xxx.disp.cc 改成自己網站的網域名,可以設定多個,例如 -d disp.cc -d www.disp.cc

執行後會要你輸入E-mail做為憑證到期通知,按c略過的話會停止認證
接著會要你同意使用條款,輸入 y 同意
接著問你是否要接收相關新聞的 E-mail,若不想要的話,可輸入 n 不同意

預設會使用 HTTP-01 Challenge 的方式做認證
執行時程式會在網站目錄下新增用來確認的檔案,例如
/var/www/xxx/.well-known/acme-challenge/xxxxx
然後使用 Let's Encrypt 的主機連至
http://xxx.disp.cc/.well-known/acme-challenge/xxxxx
看看有沒有這個檔案,有的話就通過了,通過後就會刪除用來確認的檔案

NOTE: 如果想要使用萬用字元憑證(*.disp.cc),要使用 DNS-01 Challenge 認證,可參考這篇 [Linode] NodeBalancer 使用 Let's Encrypt SSL憑證

認證成功的話會出現以下訊息:
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/xxx.disp.cc/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/xxx.disp.cc/privkey.pem
This certificate expires on 2020-10-26.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
產生的憑證檔放在 /etc/letsencrypt/live/xxx.disp.cc/
Certbot 已設定了排程自動更新憑證檔

修改 apache 的 virtual host 設定檔
$ sudo vim /etc/httpd/conf.d/vhost.conf

將原本 xxx.disp.cc 的設定,例如為
<VirtualHost *:80>
    ServerName    xxx.disp.cc
    DocumentRoot  /var/www/xxx
    # …
</VirtualHost>

複製一份,改成
<VirtualHost *:443>
    # 插入以下四行,xxx.disp.cc 改成自己的網域名
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/xxx.disp.cc/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/xxx.disp.cc/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/xxx.disp.cc/fullchain.pem

    # 在 *:80 的設定這邊也要再寫一遍
    ServerName    xxx.disp.cc
    DocumentRoot  /var/www/xxx
    # …
</VirtualHost>

測試設定檔是否正確
$ sudo httpd -t

重載 Apache 設定
$ sudo systemctl reload httpd


防火牆設定,使用 firewalld 的話,開啟 https 服務
$ sudo firewall-cmd --zone=public --add-service=https --permanent

使用 iptables 的話,修改設定檔打開 443 port,例如
iptables -A INPUT -p TCP -i eth0 --dport 443  -j ACCEPT   # HTTPS


列出所有憑證及到期日
$ sudo certbot certificates
Found the following certs:
  Certificate Name: xxx.disp.cc
    Serial Number: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    Key Type: ECDSA
    Domains: xxx.disp.cc
    Expiry Date: 2020-10-26 19:24:03+00:00 (VALID: 89 days)
    Certificate Path: /etc/letsencrypt/live/xxx.disp.cc/fullchain.pem
    Private Key Path: /etc/letsencrypt/live/xxx.disp.cc/privkey.pem

測試能否更新憑證
$ sudo certbot renew --dry-run

Certbot 已自動設定定期執行,不用再寫至 crontab
$ sudo systemctl list-timers
NEXT                        LEFT    LAST PASSED UNIT                     ACTIVATES
Sat 2023-09-30 21:27:00 CST 6h left -    -      snap.certbot.renew.timer snap.certbot.renew.service

* 若沒有自動排程的話,再設定排程定期更新
每個月自動檢查一次憑證,有效期低於一個月才會更新憑證
$ sudo vim /etc/crontab

加上這行,每月某個時間檢查,例如每月 4 日的 3:12
12 3 4 * *   root  certbot renew


○ 刪除SSL憑證

如果某個網址不需要使用了要刪掉設定的話

如果還沒到期的話,要先撤銷網域 SSL 憑證
$ sudo certbot revoke --cert-path /etc/letsencrypt/live/xxx.disp.cc/cert.pem
撤銷後會再詢問是否要刪除憑證檔
若apache的設定還有用到這些憑證檔的話,刪除可能會產生問題
確定已經沒有使用到的話再刪除

只要刪除SSL憑證檔與相關設定的話,使用
$ sudo certbot delete --cert-name xxx.disp.cc


參考:
安裝Certbot工具,與Let's Encrypt申請SSL金鑰憑證,自動更新SSL憑證

相關文章:
[CentOS7] Apache 安裝與設定 - KnucklesNote板 - Disp BBS
[RockyLinux9] 網頁伺服器 Apache 安裝與設定 - KnucklesNote板 - Disp BBS
[Linode] NodeBalancer 使用 Let's Encrypt SSL憑證 - KnucklesNote板 - Disp BBS


錯誤修正記錄:

出現 SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed
或 TypeError: __str__ returned non-string (type Error)
→ 更新 openssl 看看
$ sudo yum update ca-certificates openssl

出現 ImportError: 'pyOpenSSL' module missing required functionality. Try upgrading to v0.14 or newer.
→ 參考 [Fix] ImportError: ‘pyOpenSSL’ module missing required functionality. Try upgrading to v0.14 or newer.
移除 python 的一些套件,改用 yum 安裝的
$ sudo pip uninstall requests
$ sudo yum reinstall python-requests

$ sudo pip uninstall six
$ sudo yum reinstall python-six

$ sudo pip uninstall urllib3
$ sudo yum reinstall python-urllib3

--
※ 作者: Knuckles 時間: 2020-07-29 05:22:11 (台灣)
※ 編輯: Knuckles 時間: 2023-12-26 02:55:25 (台灣)
※ 看板: KnucklesNote 文章推薦值: 0 目前人氣: 0 累積人氣: 528 
分享網址: 複製 已複製
r)回覆 e)編輯 d)刪除 M)收藏 ^x)轉錄 同主題: =)首篇 [)上篇 ])下篇