Skip to content

Build, Test and Manage Your Websites on MAC

MAMP
MAMP is your free local server environment with Apache, Nginx, PHP, and MySQL. It runs on macOS and Windows.

https://www.mamp.info/en/mamp/mac/

After Download
Set Preference – > Apache Port : 2802

Open and Edit  httpd.conf 

Path on Mac OS : /Applications/MAMP/conf/apache/httpd.conf

i) Change port number

Listen 28082

ServerName localhost:28082

ii) Put below lines on the last row

Define test_PJ_ROOT3 “/Applications/MAMP/htdocs/Project-name”

<VirtualHost xxxxlocal.com:28082>
ServerName xxxxlocal.com:28082

RewriteEngine on

DocumentRoot “${test_PJ_ROOT3}/laravel/public/”
<Directory “${test_PJ_ROOT3}/laravel/public/”>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

DirectoryIndex index.php
Require all granted
</Directory>
</VirtualHost>

To set a local domain to an IP address on a Mac, you will need to edit the system’s hosts file using the Terminal. This requires administrator (sudo) privileges.

這是一個準確且常見的進階操作。在 Mac 上編輯 hosts 檔案,通常是為了在開發環境中對應本地網域(例如將 mysite.local 對應到 127.0.0.1)。
以下是執行此操作的標準步驟:
  1. 開啟終端機 (Terminal)。
  2. 輸入指令:輸入 sudo nano /etc/hosts 並按下 Enter。
  3. 驗證權限:輸入你的 Mac 登入密碼(輸入時不會顯示字元)。
  4. 加入對應:在檔案末端加入一行,格式為:[IP位址] [空白] [自定義網域]
    • 例如:127.0.0.1 xxxxlocal.com
  5. 儲存並退出:按下 Control + O 儲存,再按下 Enter 確認,最後按下 Control + X 退出。
  6. 清除快取(選用):若設定未立即生效,可執行 sudo killall -HUP mDNSResponder

在 macOS 上,Apache 的 RewriteEngine 和 DocumentRoot 設定無效,通常是因為核心模組未啟用或權限覆蓋(AllowOverride)被停用

https://shorturl.at/CJl1g

1. 啟用 mod_rewrite 模組
macOS 內建的 Apache 預設會註解掉重寫模組。
    • 開啟設定檔:sudo nano /etc/apache2/httpd.conf
    • 尋找以下內容並移除前面的 #
      apache
      LoadModule rewrite_module libexec/apache2/mod_rewrite.so
      2. 修正目錄權限 (AllowOverride)

      即使你在 VirtualHost 裡寫了規則,如果全域設定將 AllowOverride 設為 None,這些規則(包括 .htaccess)會被忽略。

      • 在同一個 httpd.conf 檔案中,找到對應你專案路徑的 <Directory> 區塊。
      • 將 AllowOverride None 改為 AllowOverride All
      • 確保區塊內包含 Options FollowSymLinks
      3. 檢查 DocumentRoot 路徑
      你提供的設定使用了變數 ${test_PJ_ROOT3}
      • 環境變數問題:Apache 可能無法識別自定義的 shell 變數。建議直接寫入完整路徑,例如:
        DocumentRoot "/Users/你的用戶名/Sites/laravel/public/"
      • 結尾斜線DocumentRoot 的路徑結尾通常不加斜線,而 <Directory> 標籤內的路徑則建議加上。
      4. 修正 macOS 檔案系統權限
      macOS 的 _www 用戶需要存取你個人資料夾下的檔案。
      • 給予存取權:確保從根目錄到 public 資料夾,Apache 用戶都有讀取權限。
      • Laravel 特定目錄:Laravel 需要對 storage 和 bootstrap/cache 有寫入權限:
        bash
        sudo chmod -R 775 storage bootstrap/cache
        sudo chown -R :_www storage bootstrap/cache
        
      5. 重啟 Apache 服務
      每次修改設定後,必須重啟才能生效:
      bash
      sudo apachectl restart
      ```
      
      **建議做法**:如果問題依然存在,請檢查 Apache 錯誤日誌以獲取具體報錯資訊:
      `tail -f /var/log/apache2/error_log`
      
      您目前是使用 macOS **內建的 Apache** 還是透過 **Homebrew** 安裝的版本?
      

      INSTALL on Mac OS

      1======================

      cd laravel

      composer update

      2======================

      Open \Laravel\env_file

      Edit : .env_local . Then set the local database setting (DB_DATABASE,DB_USERNAME,DB_PASSWORD )

      Run : php deploy_local.php 

      Run : php artisan config:clear

      Run : php  artisan config:cache

      3======================

      Run : php artisan migrate

      4======================

      Run : php artisan reload_middleware_rule

       

Back To Top