現在所有的網站慢慢都採用了 HTTPS 協議來避免駭客的攻擊,但是要想要使用 HTTPS 協議就必須使用 TLS 證書,這代表你必須先擁有一個網域(例如 techmoon.xyz),否則假如你是將網站安裝在本地主機或是 IP Server 上,就無法透過 Let’s Encrypt 這樣的 SSL 發證平台獲取 Localhost 的 SSL 證書。
但當我們在進行網站測試時,有時仍然會先在本地 Localhost 安裝一個 Beta 版本進行調教與測試,或是使用 IP 位址作為測試開發環境。此時若僅能使用 HTTP 進行測試的話,在正式上線時就常常會發生網站出現許多 Mixed Content 混合內容的問題。
這就是在 Localhost 上使用 HTTP 協議開發的缺點。
因此,國外的大神 Filippo Valsorda 開源了一款名為 mkcert 的工具,讓所有開發人員可以透過此工具快速的配置一個本地 Localhost 的 SSL 證書,使我們在本地 Localhost 開發時仍然可以使用 HTTPS 協議進行測試。
透過以下指令就能輕鬆在你的本地開發環境當中添加 SSL 證書:
$ mkcert -install
Created a new local CA at "/Users/filippo/Library/Application Support/mkcert" 💥
The local CA is now installed in the system trust store! ⚡️
The local CA is now installed in the Firefox trust store (requires restart)! 🦊
$ mkcert example.com "*.example.org" myapp.dev localhost 127.0.0.1 ::1
Using the local CA at "/Users/filippo/Library/Application Support/mkcert" ✨
Created a new certificate valid for the following names 📜
"example.com"
"*.example.org"
"myapp.dev"
"localhost"
"127.0.0.1"
"::1"
The certificate is at "./example.com+5.pem" and the key at "./example.com+5-key.pem" ✅
影片教學:
配置完成後就可以看到你的 Localhost 就擁有 HTTPS 協議:
目前 mkcert 支援 macOS, Linux, and Windows, and Firefox, Chrome and Java。
如果你有在 Localhost 或是 IP Server 環境中使用 HTTPS 協議的需求,可以前往原作者的 Github 進行安裝:https://github.com/FiloSottile/mkcert#installation
你也可以參考作者的原始文章了解更多資訊內容:https://blog.filippo.io/mkcert-valid-https-certificates-for-localhost/
相關文章:
- RunCloud 整合 GCP 主機,建立 WordPres 網站教學
- SiteGround 2019 年 2 月:3 折 70% off 優惠續約折扣,台灣 WordPress 推薦主機評測
- SiteGround 購買註冊教學 + WordPress 安裝教學 – 註冊購買與一鍵…
- 2019 SugarHosts 糖果主機評價:購買 8 個主機,使用超過 3 年的經驗與教學
- Hostinger 購買註冊教學 + WordPress 安裝教學 – 註冊購買與一鍵 WordPress 快速安裝教學
- WPX Hosting 主機評測 – VPS 規格 Shared Hosting 價格,是你…
- 什麼是 502 Bad Gateway Error 錯誤與如何在 WordPress 中修復這個問題
- HostGator 評價 – 優異的運行時間與支持一鍵安裝 WordPress,內含 4 折…
- SendPulse – 整合 Email 電子報寄送、SMS 簡訊發送、Web Push…
- ServerPilot + GCP VPS,輕鬆架設 WordPress 網站教學