在 Windows 環境下使用 AppServ 搭建 PHP 開發環境時,配置虛擬目錄(Virtual Host)能讓多個項目通過不同域名獨立訪問。本文詳細記錄基於 Apache 2.2 的配置全過程。
編輯文件前請備份原配置,文件路徑通常為:AppServ\Apache2.2\conf\httpd.conf
搜尋並取消註釋以下行:
LoadModule rewrite_module modules/mod_rewrite.so
搜尋並取消註釋:
Include conf/extra/httpd-vhosts.conf
同時修改根目錄權限,將 AllowOverride None 改為 AllowOverride All,允許 .htaccess 覆蓋設置:
<Directory />
Options FollowSymLinks ExecCGI Indexes
AllowOverride All
Order deny,allow
Deny from all
Satisfy all
</Directory>
根據項目需求修改 DocumentRoot 和對應的 Directory 路徑:
DocumentRoot "E:/AppServ/www/www/"
<Directory "E:/AppServ/www/www/">
經驗提示:此步驟在某些情況下非必需,視具體情況決定是否修改。
編輯 AppServ\Apache2.2\conf\extra\httpd-vhosts.conf,添加虛擬主機配置:
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.x
DocumentRoot "E:\AppServ\www\www"
ServerName localhostes
ErrorLog "logs/dummy-host2.x-error.log"
CustomLog "logs/dummy-host2.x-access.log" common
</VirtualHost>
每個項目添加一組 VirtualHost 塊,修改 DocumentRoot 和 ServerName 即可。
編輯 C:\WINDOWS\system32\drivers\etc\hosts,添加域名映射:
127.0.0.1 localhostes
# 可添加多個別名
127.0.0.1 localhostes local
https://localhostes 測試若覺得 localhostes 不夠酷,可嘗試配置更個性化的域名,如 myproject.dev,只需同步修改 vhosts.conf 和 hosts 文件即可。