利用 xampp 架設 Laravel 和 SSL、Let's encrypted

[TOC]

xampp 安裝教學

xampp 安裝教學的部分參考 挨踢路人甲

Laravel 安裝

選擇專案資料夾

1
2
3
4
//安裝
composer create-project laravel/laravel chatbot
//產生金鑰
php artisan key:generate

DNS 申請

使用免費的 DNS 服務 Freenom, 參考電腦王阿達 - 架站的前哨站,免費的 freenom 網域申請

主要就是輸入自己喜歡的 domainName 註冊完畢,然後設定 A (IPv4) 紀錄,指向哪一個 IP,再讓子彈飛一會,DNS 擴散完畢,就可以對應到你的主機了。

網站可以檢查 DNS 散播情況

AAAA 是指 IPv6

CNAME 表示 domainName 的其他別名, 例如 www.neilsiao.tk 指向 neilsiao.tk

Let’s encrypted 申請

由於環境不是 Unix like 的環境,所以使用 sslforfree.com 取得憑證,下載後有三個檔案 ca_bundle.crt、certificate.crt、private.key,將檔案放置到 apache/conf/ssl 相關位置,修改 httpd-vhosts.conf 設定如下,接著為了驗證網站所有者是妳本人,到 Laravel 專案的 public 資料夾建立 /well-known/pki-validation/{sslforfree 的 txt 檔案},搭配 sslforfree 驗證按鈕,就會去驗證你確實是網站的主人,完成 SSL 設定。

1
2
3
4
SSLEngine on
SSLCertificateFile "D:/xampp/apache/conf/ssl.crt/certificate.crt"
SSLCertificateKeyFile "D:/xampp/apache/conf/ssl.key/private.key"
SSLCertificateChainFile "D:/xampp/apache/conf/ssl.crt/ca_bundle.crt"

sslforfree.com 本身也是使用 let’s encrypted 的服務,

參考教學 XAMPP 使用 Let’s Encrypt SSL 開啟加密連線—完整教學

Apache 設定檔

參考教學 Laravel 5.4 On Apache:在 Apache 架 Laravel 網站

透過 xampp 的控制面板,可以找到 Apache config 按鈕,可以找到相關路徑,主要的設定檔有 httpd.conf、httpd-vhosts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// httpd.conf 確保有 Include vhost
# Virtual hosts
Include conf/extra/httpd-vhosts.conf

// httpd-vhosts.conf
<VirtualHost *:80>
#ServerAdmin webmaster@dummy-host.example.com
ServerName neilsiao.tk
ServerAlias www.neilsiao.tk
# 80 port 的流量導到 443 https
Redirect 301 / https://neilsiao.tk
</VirtualHost>

<VirtualHost *:443>
#ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "D:/Interview/chatbot/public"
ServerName neilsiao.tk
ServerAlias www.neilsiao.tk
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common

#Laravel 專案位置
<Directory D:/Interview/chatbot/public>
AllowOverride all
Require all granted # 不限制 IP 連入網站
</Directory>

# SSL 設定
SSLEngine on
SSLCertificateFile "D:/xampp/apache/conf/ssl.crt/certificate.crt"
SSLCertificateKeyFile "D:/xampp/apache/conf/ssl.key/private.key"
SSLCertificateChainFile "D:/xampp/apache/conf/ssl.crt/ca_bundle.crt"
</VirtualHost>

Apache 從 2.2 換至 2.4 httpd.conf 的調整筆記 (windows 環境)

總結

  1. xampp 安裝 Apache, MariaDB, PHP
  2. Freenom 購買 Domain, 設定 A、CNAME 參照 IP 與 www 的名稱
  3. 安裝 Laravel 專案,php artisan key:generate
  4. 申請 SSL 憑證,至 xampp 的 httpd-vhosts 設定憑證的三個檔案,在 Laravel public 新增 /well-known/pki-validation/{sslforfree.txt}檔案
  5. 訪問 domain name , OK