Apache 配置 Laravel


建立時間: 2024年1月3日 14:45
更新時間: 2024年1月3日 14:52

說明

分享 Apache 如何將自訂網域對應到指定的 Laravel 專案。

Windows hosts

Windows hosts 用來自訂 IP 位置對應到的網域名稱,在本機開發可以將 localhost 對應到多個主機(網域),然後再到 httpd-vhost 配置 Laravel 要對應到哪個主機。

C:\Windows\System32\drivers\etc\hosts

127.0.0.1 laravel.local

主機名稱注意事項

  • 不可以用 .dev 結尾的名稱,例如: laravel.dev

httpd

C:\www\Apache24\conf\httpd.conf

# 引用 vhosts 配置檔
Include conf/extra/httpd-vhosts.conf
# 重寫 URL,.htaccess 會用到
LoadModule rewrite_module modules/mod_rewrite.so

httpd-vhosts

這是一個 Laravel 配置範例,用來將虛擬主機對應到 Laravel 專案,也就是將 Laravel 的 public 資料夾當作網站的根目錄。

C:\www\Apache24\conf\extra\httpd-vhosts.conf

<VirtualHost laravel.local:80>
    <Directory "path/to/laravel-project/public">
        AllowOverride All
        Require all granted
    </Directory>
    DocumentRoot "path/to/laravel-project/public"
    ServerAdmin webmaster@laravel
    ServerName laravel.local
</VirtualHost>
  • Directory: 定義虛擬主機相關聯的目錄的設定。
  • AllowOverride: 什麼指令的類型允許在 .htaccess 檔案。
    • All: .htaccess 檔案中允許任何具有 .htaccess 上下文的指令。
  • Require all granted: 設定允許所有用戶訪問這個目錄。
  • DocumentRoot: 虛擬主機的根目錄。
  • ServerAdmin: 伺服器管理員郵件地址。
  • ServerName: 虛擬主機的名稱。
  • laravel.local: 自訂的主機名稱,需要到 C:\Windows\System32\drivers\etc\hosts 配置。

參考

觀看次數: 459
apacheconfigurationhostlaravelvhostvirturalvirturalhost配置
按讚追蹤 Enjoy 軟體 Facebook 粉絲專頁
每週分享資訊技術

一杯咖啡的力量,勝過千言萬語的感謝。

支持我一杯咖啡,讓我繼續創作優質內容,與您分享更多知識與樂趣!