首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

Apache + PHP 開發環境虛擬主機配置全攻略:從 403 到局域網訪問

     編輯:PHP 綜合知識

本地開發配置虛擬主機、解決權限問題、實現局域網多設備訪問是 PHP 開發者必備技能。本文記錄 WAMP 環境下的完整配置流程與坑點總結。

環境基線

  • WAMP 3.x (Apache 2.4 + PHP 7/8 + MySQL 5.7/8)
  • 項目目錄:D:/wamp/www/CrashServer
  • 目標域名:crash.com (本地開發用)

核心配置文件

1. httpd.conf 關鍵設置

# 1. 啟用虛擬主機配置
Include conf/extra/httpd-vhosts.conf

# 2. 默認目錄權限 (關鍵!)
<Directory "D:/wamp/www">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require local  # 僅本地訪問,局域網需改為 Require all granted
</Directory>

# 3. 啟用 rewrite 模塊
LoadModule rewrite_module modules/mod_rewrite.so

2. httpd-vhosts.conf 虛擬主機定義

# 必須聲明
NameVirtualHost *:80

# 1. 保留 localhost 對應默認目錄
<VirtualHost *:80>
    DocumentRoot "D:/wamp/www"
    ServerName localhost
    <Directory "D:/wamp/www">
        Options Indexes FollowSymLinks
        AllowOverride All
        Require local
    </Directory>
</VirtualHost>

# 2. 項目虛擬主機
<VirtualHost *:80>
    DocumentRoot "D:/wamp/www/CrashServer"
    ServerName crash.com
    ServerAlias www.crash.com
    
    # 目錄權限 (項目不在 www 下時必須顯式配置)
    <Directory "D:/wamp/www/CrashServer">
        Options Indexes FollowSymLinks
        AllowOverride All
        # Apache 2.4 語法
        Require all granted
        # Apache 2.2 舊語法 (兼容)
        Order Allow,Deny
        Allow from All
    </Directory>
    
    # 錯誤/訪問日誌分離 (推薦)
    ErrorLog "logs/crash.com-error.log"
    CustomLog "logs/crash.com-access.log" common
</VirtualHost>

3. hosts 文件綁定

# C:\Windows\System32\drivers\etc\hosts (需管理員權限編輯)
127.0.0.1    localhost
127.0.0.1    crash.com
127.0.0.1    www.crash.com

關鍵坑點總結

1. 項目目錄不在 DocumentRoot 下 → 403 Forbidden

# 錯誤配置
<VirtualHost *:80>
    DocumentRoot "E:/Projects/MyApp"
    ServerName myapp.local
</VirtualHost>
# 缺少  權限授予 → 403

# 正確配置
<VirtualHost *:80>
    DocumentRoot "E:/Projects/MyApp"
    ServerName myapp.local
    <Directory "E:/Projects/MyApp">
        Require all granted  # Apache 2.4 必須
    </Directory>
</VirtualHost>

2. Apache 2.4 vs 2.2 權限語法差異

版本語法示例
2.4+RequireRequire all granted / Require ip 192.168.1
2.2Order/Allow/DenyOrder Allow,Deny / Allow from all

建議雙寫兼容,避免版本遷移報錯。

3. 局域網手機訪問電腦站點

  1. 修改 httpd.conf 默認目錄權限:Require all granted
  2. Windows 防火牆放行 80 端口 (入站規則)
  3. 手機瀏覽器訪問 https://電腦局域網IP
  4. 若項目用虛擬域名,手機需修改 hosts 或使用 IP + Host Header

4. 端口衝突排查

# 檢查 80 端口佔用
netstat -ano | findstr :80
# PID 4 通常是 System (IIS/World Wide Web Publishing Service)
# 關閉 IIS 或修改 Apache 端口 Listen 8080

生產級配置模板

<VirtualHost *:80>
    ServerName dev.project.com
    DocumentRoot "/var/www/project/public"
    
    # 安全頭
    Header always set X-Content-Type-Options "nosniff"
    Header always set X-Frame-Options "SAMEORIGIN"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # PHP-FPM 代理 (生產環境推薦)
    # ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/project/public/$1
    
    <Directory "/var/www/project/public">
        Options -Indexes +FollowSymLinks
        AllowOverride All
        Require all granted
        
        # 前端路由支持 (Vue/React/Laravel)
        FallbackResource /index.php
    </Directory>
    
    # 靜態資源緩存
    <FilesMatch "\.(css|js|png|jpg|jpeg|gif|ico|svg|woff2?)$">
        ExpiresActive On
        ExpiresDefault "access plus 1 year"
        Header set Cache-Control "public, immutable"
    </FilesMatch>
    
    # 訪問/錯誤日誌
    ErrorLog ${APACHE_LOG_DIR}/project_error.log
    CustomLog ${APACHE_LOG_DIR}/project_access.log combined
</VirtualHost>

驗證清單

  • [ ] Apache 語法檢查:httpd -t (Syntax OK)
  • [ ] 重啟服務:WAMP 圖標左鍵 → Restart All Services
  • [ ] 瀏覽器訪問 https://crash.com 正常
  • [ ] 瀏覽器訪問 https://localhost 仍顯示 WAMP 首頁
  • [ ] 手機同 WiFi 訪問電腦 IP 正常
  • [ ] .htaccess 生效測試 (RewriteRule)
  • [ ] PHP 錯誤顯示正常 (開發環境)

標準化的虛擬主機配置流程,能讓開發環境「開箱即用」,避免環境差異導致的「我機器上能跑」問題。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved