- HTTPS:為避免連網過程中所傳輸的資料被攔截、竊取,會使用 HTTPS 其應用 TLS (或 SSL) 來加密 HTTP 的請求與回應。萬一資料被攔截時看不到文字,只是一堆看似隨機的字元。
- 憑證:TLS 是一種公開金鑰加密的技術:使用一對金鑰,即「公開金鑰」和「私密金鑰」,其中公開金鑰透過網頁伺服器的 SSL 憑證與用戶端裝置共享。當用戶端與伺服器端連線時,這兩個裝置使用公開金鑰和私密金鑰協商一個新的金鑰(稱為工作階段金鑰),用以加密它們之間的所有通訊。
- 申請憑證:有數種獲得方式,但因安全考量,能選擇的越來越少,
- 本機憑證:因客戶端瀏覽器無法驗證憑證的真實性,需在客戶端設定,多已不支援。
- 憑證服務:多數是付費服務,而免費服務中的 Let's Encrypt 是比較獲得認可。
- 網域名稱:在申請憑證過程中需要網站的「網域名稱」,有很多免費網域名稱可註冊使用。若是公司行號也可付費註冊一個特別的網域名稱。
1. 網域名稱
申裝寬頻固網者除了日常連網外,多會申請一個免費的固定網路位址,方便由外部連回主機。此外,您可花錢註冊或至免費的網域名稱服務註冊一個網域名稱 (註:筆者以為免費網域名稱服務有些疑慮而不想用),若只是為個人或小眾使用而花錢去註冊一個網域名稱有些麻煩。筆者使用中華電信也申請一個免費固定的「網路位址」,想用免費「網域名稱」而不想去註冊。既然有固定「網路位址」,想說「網際網路提供商 (ISP)」應該有對應的「網域名稱服務」,最好是免費的。中華電信的固網就有,用 nslookup 就可以查得。
[SiB@Server ~]S nslookup 125-xx-yy-zz.hinet-ip.hinet.net其中「125.xx.yy.zz」是自 HiNet 申請的免費「網路位址」,而「125-xx-yy-zz.hinet-ip.hinet.net」則是 HiNet 預設給「網路位址」「125.xx.yy.zz」的「網域名稱」。這個「網域名稱」是待會申請憑證時要用的。
Server: 127.0.0.53
Address: 127.0.0.53#53
Name: 125-xx-yy-zz.hinet-ip.hinet.net
Address: 125.xx.yy.zz
2. 從 Let's Encrypt 申請憑證
以下操作都需先切換成 root 身份,請參考 "在 Linux 中,切換為 root 身分以操作、設定系統" 一文。詳細的文件請讀者參考 Let's Encrypt,本文以幾個命令列指令來簡單說明這個申請步驟。 首先,要安裝一些必要的軟體套件,- httpd:Apache HTTP 伺服器
- mod_md:使用 ACME 為 Apache HTTP 伺服器設定憑證
- certbot:免費、自動憑證的認證工具.
[root@Server ~]# dnf install httpd mod_md certbot有關 Apache HTTP 伺服器的設定,請另外參考其它安裝說明。接著,開始向 Let's Encrypt 申請憑證。
[root@Server ~]# certbot certonly -d 125-xx-yy-zz.hinet-ip.hinet.net -m myEmail@gmail.com --webroot -w /var/www/html -v其中 certbot 的各個參數的說明如下:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Certificate is due for renewal, auto-renewing...
Renewing an existing certificate for 125-xx-yy-zz.hinet-ip.hinet.net
Performing the following challenges:
http-01 challenge for 125-xx-yy-zz.hinet-ip.hinet.net
Using the webroot path /var/www/html for all unmatched domains.
Waiting for verification...
Cleaning up challenges
Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/125-xx-yy-zz.hinet-ip.hinet.net/fullchain.pem
Key is saved at: /etc/letsencrypt/live/125-xx-yy-zz.hinet-ip.hinet.net/privkey.pem
This certificate expires on 2024-11-16.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
certonly | :只取得憑證而不安裝 |
-d 125-xx-yy-zz.hinet-ip.hinet.net | :指定「網域名稱」,不可用「網路位址」 |
-m myEmail@gmail.com | :指定 email 帳號以供通知之用 |
--webroot | :透過將檔案存放在 webroot 以取得憑證 |
-w /var/www/html | :指定 webroot 的目錄 |
-v | :設定輸出的詳細程度 |
3. 設定、更新 Apache 憑證:
a. 設定 Apache 使用 mod-md
在目錄 /etc/httpd/conf.modules.d/ 加入設定檔 01-md.conf 內容為[root@Server ~]# echo LoadModule md_module modules/mod_md.so > /etc/httpd/conf.modules.d/01-md.conf第一個指令是加入設定使得 Apache 啟動時自動載入 mod_md 模組。而第二個指令則是驗證設定的結果。
[root@Server ~]# cat /etc/httpd/conf.modules.d/01-md.conf
LoadModule md_module modules/mod_md.so
b. 設定 Web 伺服器以 ACME 更新憑證
修改 /etc/httpd/conf/httpd.conf 以透過 ACME 自動更新,ServerAdmin myEmail@gmail.com設定時要注意原有 httpd.conf 中是否已有預設值,若有要先註解掉再加入新設定。
ServerName 125-xx-yy-zz.hinet-ip.hinet.net
#Add 5 lines for Let's Encrypt
Protocols http/1.1 acme-tls/1
MDCAChallenges tls-alpn-01
MDCertificateAgreement accepted
MDomain 125-xx-yy-zz.hinet-ip.hinet.net
MDPrivateKeys RSA 4096
c. 傳輸埠 (Port) 的使用注意
HTTPS 預設的傳輸埠是 443,而透過 ACME 更新也使用 443,如果讀者並未更改預設傳輸埠,那麼整個設定就完成。筆者有使用防火牆更改連入的傳輸埠,並關閉 443 埠而造成不能『自動更新』,當發現無法連線或收到『逾期提醒信』時,先在防火牆開啟 443 埠,再重啟 Apache 網頁伺服器。[root@Server ~]# systemctl restart httpd.service底下是手動更新前後的憑證檢視, 憑證的到期日是“2025年4月28日”即將到期, 用上述指令重啟網頁伺服器強制更新憑證後,再查詢的結果如下: 更新後的到期日為“2025年7月24日”,三個月的保鮮期限從更新日起算。
後記:
- 本文整理歷經多年的設定修改,部份設定方法可能有重複、冗餘,請讀者見諒並指正。
已測試版本:
- Fedora: 40﹢Apache 2.4
參考資料:
- Let's Encrypt.
- Let's Encrypt, "Browser error : net::err_cert_common_name_invalid" Jun, 2022 [Apr. 26, 2025].
- Medium, "不用80 Port及DNS獲取Let’s Encrypt SSL認證," Nov. 7 2022 [Apr. 26, 2025].
- Let's Encrypt, "Automatic Let’s Encrypt certificates in Apache with mod_md," Oct. 10 2021 [Apr. 26, 2025].
- GitHub Gist, "jogu/apache.conf," Sep. 22 2022 [Apr. 26, 2025].
- Fraser's IdM Blog, "ACME for Apache httpd with mod_md," May 7 2020 [Apr. 26, 2025].
沒有留言:
張貼留言
感謝你耐心看完本文,歡迎留下任何指正、建議,筆者會儘快回應。(English is also welcome.)