本地Apache綁定多域名

在本地開發的時候,經常要實現通過訪問不同的xxx.xxx.com來訪問本地多個不同網站而不是多個xxx.xxx.com/xxx/,在網上看了一大堆文章,都是copy的,無用的配置一大堆,而且會攔截原有域名,比如研究test,綁定www.test.com做本地測試,就訪問不了test官方網站來尋找資料,十分不便,但是採取local.test.com或其他xxx.test.com的方式,則即不影響域名和配置的直觀效果,又不影響test官網的訪問。

說配置:

打開c:\windows\system32\drivers\etc\hosts文件,編輯內容如下:

127.0.0.1       localhost
192.168.1.101    local.test.com

第一行本來就存在,不管;如果apache服務器網站只是你一個人本機訪問做測試,那麼第二行前面也可以寫成127.0.0.1,如果局域網也要訪問,比如公司同事,則可以寫成本機內網ip,可以通過本地運行cmd,敲入ipconfig命令來獲得,不過最好是設置了靜態ip。局域網用戶需要訪問local.test.com,同樣需要在他電腦的c:\windows\system32\drivers\etc\hosts裏面加入第二行。

打開Apache配置文件,一般在Apache安裝目錄下/config/httpd.conf,編輯前先做備份。

查找<Directory />,找到如下內容並修改爲,不然可能會因權限問題而出現錯誤頁面:

<Directory />
    Options FollowSymLinks ExecCGI Indexes
    AllowOverride None
   #Order deny,allow
    #Deny from all
   Order allow,deny
    Allow from all
    Satisfy all
</Directory>

接着在最後加入以下內容:

<VirtualHost *:80>
DocumentRoot F:/App/www/test/
ServerName local.test.com
# Other directives here
</VirtualHost>

其中F:/App/www/爲Apache根目錄,F:/App/www/test/爲根目錄下方test所有源碼的目錄;ServerName爲你想要訪問的域名,如果你想用www.xxx.com的方式訪問,只需要同時編輯c:\windows\system32\drivers\etc\hosts裏面的域名和這個域名就可以實現,但是會攔截這個域名的DNS解析。

最終配置如下:

c:\windows\system32\drivers\etc\hosts

127.0.0.1       localhost
192.168.1.101    local.test.com

 

/apache/conf/httpd.conf

<Directory />
    Options FollowSymLinks ExecCGI Indexes
    AllowOverride None
   Order allow,deny
    Allow from all
    Satisfy all
</Directory>

 

<VirtualHost *:80>
DocumentRoot F:/App/www/test/
ServerName local.test.com
# Other directives here
</VirtualHost>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章