【XAMPP】【Apache】用Certbot在XAMPP上安裝SSL憑證

ZeroSSL免費三個月3次的憑證都用完了


原本想說Let's Encrypt不是永久免費嗎?

後來查仔細看發現 ZeroSSL、SSL For Free、Let's Encrypt 三者是如圖中的關係。




Let's Encrypt 的Getting Started頁面也推薦使用Certbot。




Certbot客戶端工具


接著來使用Certbot申請憑證吧

一開始來到Certbot網頁





選擇好伺服器及OS後,
下載certbot-installer 並在Server上進行安裝




以系統管理員身分開啟CMD,確認是否可只執行certbot


> certbot --help




這邊照說明,我選擇關閉Apache
否則接下來會出現 Problem binding to port 80: [WinError 10013] 嘗試存取通訊端被拒絕,因為存取權限不足。


> certbot certonly --standalone




第一次使用會要求輸入信箱
> gjlmotea@gmail.com

是否同意條款
> Y

是否接收到訂閱訊息
> Y

輸入網域,多個網域以`,`區隔
> gjlmotea.com

(或者 > gjlmotea.com,*.gjlmotea.com
如果輸入 * 萬用字元(wildcard) 時出現"Client with the currently selected authenticator does not support any combination of challenges that will satisfy the CA. You may need to use an authenticator plugin that can do challenges over DNS." 的話,代表此間DNS供應商不支援(可以到DNS 加一條* records看看,如果還是不行就只能手動驗證
> certbot certonly --manual -d *.gjlmotea.com -d gjlmotea.com --preferred-challenges dns
並且到DNS設定新增 要你填寫的紀錄,Type選擇TXT

)



成功後會出現這段文字

Successfully received certificate.
Certificate is saved at: C:\Certbot\live\gjlmotea.com\fullchain.pem
Key is saved at:         C:\Certbot\live\gjlmotea.com\privkey.pem




如果出現這段訊息
Detail: no valid A records found for thu.today; no valid AAAA records found for thu.today
代表你得先將Domain DNS做設定,將Domain綁定到此台電腦IP






(如果出現以下訊息
Hint: The Certificate Authority failed to download the challenge files from the temporary standalone webserver started by Certbot on port 80. Ensure that the listed domains point to this machine and that it can accept inbound connections from the internet.

Some challenges have failed.
Ask for help or search for solutions at https://community.letsencrypt.org. See the logfile C:\Certbot\log\letsencrypt.log or re-run Certbot with -v for more details.

則代表須先設定網頁根目錄(在httpd.conf)
或者是防火牆的問題,可以先關閉防火牆試試看
否則也可以使用standalone以外的 第二種方式驗證,請在文章搜尋 webroot)



之後移動到 : C:\Certbot\live\gjlmotea.com 底下出現了好多檔案


閱讀README,上面寫著

This directory contains your keys and certificates.

`privkey.pem`  : the private key for your certificate. 憑證的私鑰
`chain.pem`    : used for OCSP stapling in Nginx >=1.3.7. 線上憑證狀態協定
`cert.pem`     : will break many server configurations, and should not be used
                 without reading further documentation (see link below). 伺服器憑證,建議閱讀文檔再使用
`fullchain.pem`: the certificate file used in most server software. 順帶一提 fullchain = cert.pem + chain.pem
使用中間證書(Intermediate Certificate)就須使用fullchain.pem,以驗證完整的證書鏈

WARNING: DO NOT MOVE OR RENAME THESE FILES!
         Certbot expects these files to remain in this location in order
         to function properly!

We recommend not moving these files. For more information, see the Certbot
User Guide at https://certbot.eff.org/docs/using.html#where-are-my-certificates.


強烈建議我們不要移動這些憑證的位置,
並且這些憑證是以捷徑(連結)形式存在,會自動連結到最新的憑證,以達到自動更新憑證的目的


設定XAMPP Apache SSL

接著開啟XAMPP的Apache SSL設定



找到 SSLCertificateFile、SSLCertificateKeyFile、SSLCertificateChainFile 三個參數,
並設定為CertBot產出的憑證路徑

以下是我做的修改

#   Server Certificate:
#   Point SSLCertificateFile "conf/ssl.crt/server.crt"
#   the certificate is encrypted, then you will be prompted for a
#   pass phrase.  Note that a kill -HUP will prompt again.  Keep
#   in mind that if you have both an RSA and a DSA certificate you
#   can configure both in parallel (to also allow the use of DSA
#   ciphers, etc.)
#   Some ECC cipher suites (http://www.ietf.org/rfc/rfc4492.txt)
#   require an ECC certificate which can also be configured in
#   parallel.
SSLCertificateFile "C:\Certbot\live\gjlmotea.com\cert.pem"

#   Server Private Key:
#   If the key is not combined with the certificate, use this
#   directive to point at the key file.  Keep in mind that if
#   you've both a RSA and a DSA private key you can configure
#   both in parallel (to also allow the use of DSA ciphers, etc.)
#   ECC keys, when in use, can also be configured in parallel
SSLCertificateKeyFile "C:\Certbot\live\gjlmotea.com\privkey.pem"

#   Server Certificate Chain:
#   Point SSLCertificateChainFile at a file containing the
#   concatenation of PEM encoded CA certificates which form the
#   certificate chain for the server certificate. Alternatively
#   the referenced file can be the same as SSLCertificateFile "conf/ssl.crt/server.crt"
#   certificate for convenience.
SSLCertificateChainFile "C:\Certbot\live\gjlmotea.com\chain.pem"




順帶一提
最後一行的SSLCertificateChainFile,用fulllchain或chain.pem都行
SSLCertificateChainFile "C:\Certbot\live\gjlmotea.com\chain.pem"
SSLCertificateChainFile "C:\Certbot\live\gjlmotea.com\fullchain.pem"
若遺漏掉 SSLCertificateChainFile 這行的話,因為缺少完整的證書鏈
雖然連線上、網頁瀏覽等等都沒什麼問題
但是在前端程式碼,透過proxy串接來進行的話,就會出現 UNABLE_TO_VERIFY_LEAF_SIGNATURE 的錯誤
甚至有些防火牆會識別為不安全網站,即便有SSL憑證
此時就是需要將憑證添加到chain中



最後重啟Apache伺服器

Ctrl+F5強制重新網頁
就能看到最新的憑證拉







手動更新憑證


一樣將XAMPP中的Apache服務先關閉後,用系統管理員身分開啟CMD執行以下這行

(更新全數域名)
> certbot renew --force-renewal --no-random-sleep-on-renew

(只更新單一域名)
> certbot renew --cert-name gjlmotea.com

(一開始沒加 --force-renewal --no-random-sleep-on-renew 參數,renew 時都會卡住不動)


成功時回傳結果




再啟動Apache服務、強制重整網站之後,就可以看到更新後的憑證



列出所有憑證


certbot certificates


移除憑證

若哪天不再續約Domain了(Domain不再是自己的了),刪除用不到的憑證

> certbot delete --cert-name gjlmotea.com


自動更新憑證


使用Windows內建的工作排程器
安排每隔一段時間就 certbot renew 一次





不同的驗證方式

若使用以上方式(standalone) 的話,更新時需要關閉Apache服務,也就是須停機


因為驗證,分為兩種驗證方式
1. HTTP-01 驗證
2. DNS-01 驗證

standalone 允許在沒有其他Web伺服器(Apache, Nginx)的情況下來處理驗證。走的是HTTP-01 驗證,在驗證時會使用臨時的Web服務器、佔用一個80 port,待驗證完畢後即關閉此臨時服務器。
所以在驗證時,我們需要關閉自己的網頁伺服器,否則會與現有服務器產生衝突。

webroot 則是另一種HTTP-01 驗證,但是不走80 port
原理是certbot會將驗證的文件放到網站的根目錄中,讓 Let's Encrypt 能夠通過 HTTP 請求訪問這些文件來進行驗證。在驗證過程中會寫入一個文件到 .well-known/acme-challenge/ 目錄,在驗證完畢後就會刪除、不會干擾現有網站流量,也不需停止 Apache伺服器。

webroot用法如下
-w 後面接網站根目錄
-d 後面接域名、子域名

> certbot certonly --webroot -w C:\\www\\gjlmotea -d gjlmotea.com -d www.gjlmotea.com

如果一個域名底下的子域名 掛在不同位置/路徑
可以這樣輸入

> certbot certonly --webroot -w C:\\www\\gjlmotea\\website -d gjlmotea.com -d www.gjlmotea.com -w C:\\www\\gjlmotea\\wordpress -d blog.gjlmotea.com

(注意httpd.conf ServerAlias 設定這行,是要像這樣 ServerAlias gjlmotea.com www.gjlmotea.com)

(用這個方法的話,第一次設定時,會需要分兩次執行伺服器
第一次需要先設定好httpd.conf,當伺服器跑起來、等webroot驗證好,拿到ssl證書之後
第二次才能設定httpd-ssl.conf,讓伺服器能跑https)

(待研究)
重載設定,無需重啟Apache服務
用XAMPP內建的Shell開啟
> httpd -k graceful





如果連接域名時,出現 ACME client standalone challenge solver 的錯誤,

用 > certbot certonly --standalone來更新該域名憑證



不過要先到Certbot路徑底下,把 archive、live、renewal 三個資料夾內 該域名的資料夾清除
不然名稱重複,就會出現0001 0002等,申請到新的key的檔案位置就對不上囉








沒有留言:

張貼留言