配置PHP虛擬目錄
----------
摘要
----------
本文極為簡要的介紹了如何在Windows平台下安裝Apache2.2.x並配置虛擬目錄. 以作為快速安裝的參考. 詳細的設定最好的文檔是官方的說明文檔見下方的連結, 在安裝配置的時候仔細的查閱文檔是很好的解決辦法.
1. Apache HTTP Server Version 2.2 英文文檔
2. Apache HTTP Server Version 2.2 中文文檔
----------
準備條件
----------
到 http://httpd.apache.org/ 下載相應版本的Apache. (注意: 2.0與2.2版本的Apache對應的Module版本頁不同, 互相不相容, 因此需要根據應用程式來下載相應版本的Apache). 如果不需要使用ssl, 則要下載 Apache 2.x.x-win32-x86-no_ssl.msi, 如果需要使用, 則需要下載Apache 2.x.x-win32-x86-openssl-x.x.x.msi
----------
安裝
----------
雙擊安裝程式, 它將引導你完成整個安裝過程。請確保你輸入正確的伺服器URL(如果你的伺服器沒有DNS名稱,你可以直接輸入IP地址)。建議將Apache安裝成" for all Users,on Port 80,as a service" Apache將自動安裝一個系統服務以自動啟動。注意:如果你已經有一個IIS或者其它的程式使用80連接埠,安裝可能會失敗。如果是這樣,你可以到程式目錄下的Apache Group\Apache2\conf目錄中找到檔案httpd.conf,將其中的Listen 80改成一個不用的連接埠,比如Listen 81。然後重新運行這個安裝程式,這次應該不會再有問題了。
安裝結束之後在瀏覽器中鍵入 http://loccalhost,如果呈現了一個已經配置好的網站,那麼Apache就安裝成功了。 如果你將Apache安裝成一個服務,它將以本地system帳戶來運行。為Apache建立一個單獨的使用者來運行它將會更安全一些。
開啟Apache的安裝目錄, 找到檔案 /conf/httpd.conf, 開啟它進行配置的編輯. 設定檔中對於各項都有比較詳細的描述, 在配置結束之後可以運行開始菜單中Apache安裝資料夾中的Test Configuration工具來檢驗設定檔是否正確. 如果配置不正確會給出簡要的說明. 如果在配置結束之後通過了Test Configuration工具, 但是仍然不能夠順利運行Apache, 可以到[Apache安裝目錄]/logs/ 這個目錄中查看記錄, 以找出問題所在.
----------
配置 httpd.conf檔案
----------
# 設定文檔主目錄, 後面的就是磁碟的目錄
DocumentRoot "D:/Programs/Apache2.2/htdocs"
# 添加虛擬目錄, 下面的設定在瀏覽器中輸入 http://localhost/webpath/ 的時候機會訪問伺服器的"D:/Programs/WebPath"目錄
Alias /webpath/ "D:/Programs/WebPath"
# 當/webPath 後面不加"/"的話, 在瀏覽器中可以輸入 http://localhost/webpath Apache會自動的在後面添加"/"
Alias /webpath "D:/Programs/WebPath"
# URL 重新導向
Redirect permanent /foo/ http://www.example.com/bar/
# 設定虛擬機器主機: 請參考 http://lamp.linux.gov.cn/Apache/ApacheMenu/vhosts/index.html
?
在WIN下安裝APACHE配置虛擬目錄和UNIN下基本是一樣的
就是修改httpd.conf
1:單個IP對應單個玉米
例如:www.phpunion.com對應192.168.1.1
www.feelone.net對應192.168.1.2
在httpd.conf中加入如下指令
#對應玉米所指向的IP
DocumentRoot "c:/web/php" #www.phpunion.com要指向的目錄,注意一定要有""
ServerName www.phpunion.com #所要綁的玉米
?
#對應玉米所指向的IP
DocumentRoot "c:/web/feel" #www.feelone.net要指向的目錄,注意一定要有""
ServerName www.feelone.net #所要綁的玉米
剩下的可以繼續綁!
?
2:單個IP對應多個玉米
?
NameVirtualHost 192.168.1.1 #記得一定要有著句啊!
#對應玉米所指向的IP
DocumentRoot "c:/web/php" #www.phpunion.com要指向的目錄,注意一定要有""
ServerName www.phpunion.com #所要綁的玉米
#對應玉米所指向的IP
DocumentRoot "c:/web/feel" #www.feelone.net要指向的目錄,注意一定要有""
ServerName www.feelone.net #所要綁的玉米
這樣就綁上對應的目錄了,如果沒有NameVirtualHost 192.168.1.1 這個指令的話,所有的玉米就都指到php目錄了.
然後重新啟動apache,就可以看到所指的玉米對應相應的虛擬目錄!
?
Example:
ServerAdmin [email protected]
DocumentRoot /www/docs/host.example.com
ServerName host.example.com
ErrorLog logs/host.example.com-error_log
TransferLog logs/host.example.com-access_log
?
?
?
?
?
今天網站建設技術部的PHP程式員教了我一個在本地實現虛擬一個網域名稱來訪問本機電腦的資料的方法,首先說說我本地的環境是:WIN2003+xampp。其實能實現效果就行,直接用xampp的最好方法就是省事,不用一個一個軟體安裝!然後下面一步一步實現虛擬一個www.123.com來訪問以磁碟為基礎的網站程式。
1.關閉xampp的apache,然後找到C:\xampp\apache\conf\extra\httpd-vhosts.conf檔案:
在##NameVirtualHost *:80 下面加入以下代碼,其中的IP為本地電腦的IP,可以進入CMD使用ipconfig查詢到本機IP
NameVirtualHost 192.168.1.101:80
然後把以下代碼複製一份
##
##ServerAdmin [email protected]
##DocumentRoot "C:/xampp/htdocs/dummy-host2.localhost"
##ServerName dummy-host2.localhost
##ServerAlias www.dummy-host2.localhost
##ErrorLog "logs/dummy-host2.localhost-error.log"
##CustomLog "logs/dummy-host2.localhost-access.log" combined
##
修改成以下代碼
##ServerAdmin [email protected]
DocumentRoot "E:/php/ecshop2.72"
ServerName www.123.com
ServerAlias www.123.com
##ErrorLog "logs/dummy-host2.localhost-error.log"
##CustomLog "logs/dummy-host2.localhost-access.log" combined
其中DocumentRoot 是本機網站製作程式的路徑,ServerName及ServerAlias 就是要虛擬網域名稱網址,這個隨便你高興了
2.找到C:\xampp\apache\conf\httpd.conf檔案,找到以下代碼:
DocumentRoot "C:/xampp/htdocs"
把上面的兩個網站目錄的路徑修改成上面設定的路徑
DocumentRoot "E:/php"
因為設定了虛擬網域名稱網址,所以路徑不用再加上網站的檔案夾名稱了
3.然後開啟C:\WINDOWS\system32\drivers\etc\hosts 檔案,
在127.0.0.1 localhost下添加你要添加的虛擬網域名稱網址
127.0.0.1 localhost
192.168.1.101 www.123.com
呵呵,修改這三個檔案之後就OK了,現在可以重新啟動xampp的apache了,然後在IE裡輸入www.123.com就能訪問本地的網站程式了,這樣測試的好處是能把本機虛擬成伺服器,有些東東很方便就能調試!當然,用localhost也是還可以訪問了,這個功能用不到的話也沒必要這麼搞,我是因為有時用到Magento程式,使用網域名稱會更好,不會有一些莫名其妙的問題。當然了,如果你有好幾個網站的話,每次都要修改一下C:\xampp \apache\conf\extra\httpd-vhosts.conf對應的DocumentRoot "E:/php/ecshop2.72",因為我的網頁設計的PHP程式都是放在E盤下的PHP檔案夾,然後每個網站就一個檔案夾,這樣每次只需要修改一個檔案就OK了,看個人愛好了,共用出來讓大家玩玩,嘿嘿!
?
?
詳細配置:http://httpd.apache.org/docs/2.2/vhosts/
中文配置:http://www.zzxj.net/web/manual/ApacheMenu/rewrite/rewrite_guide.html
?
?
?
?
Apache虛擬機器主機的配置
?
虛擬機器主機的配置
?
基於IP地址的虛擬機器主機配置
Listen 80
DocumentRoot /www/example1
ServerName www.example1.com
DocumentRoot /www/example2
ServerName www.example2.org
?
?
?
基於IP和多連接埠的虛擬機器主機配置
Listen 172.20.30.40:80
Listen 172.20.30.40:8080
Listen 172.20.30.50:80
Listen 172.20.30.50:8080
?
DocumentRoot /www/example1-80
ServerName www.example1.com
DocumentRoot /www/example1-8080
ServerName www.example1.com
DocumentRoot /www/example2-80
ServerName www.example1.org
DocumentRoot /www/example2-8080
ServerName www.example2.org
?
單個IP地址的伺服器上基於網域名稱的虛擬機器主機配置:
# Ensure that Apache listens on port 80
Listen 80
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
DocumentRoot /www/example1
ServerName www.example1.com
ServerAlias example1.com. *.example1.com
# Other directives here
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here
?
在多個IP地址的伺服器上配置基於網域名稱的虛擬機器主機:
Listen 80
# This is the “main” server running on 172.20.30.40
ServerName server.domain.com
DocumentRoot /www/mainserver
# This is the other address
NameVirtualHost 172.20.30.50
DocumentRoot /www/example1
ServerName www.example1.com
# Other directives here …
DocumentRoot /www/example2
ServerName www.example2.org
# Other directives here …
?
在不同的連接埠上運行不同的網站:
基於多連接埠的伺服器上配置基於網域名稱的虛擬機器主機。
Listen 80
Listen 8080
NameVirtualHost 172.20.30.40:80
NameVirtualHost 172.20.30.40:8080
ServerName www.example1.com
DocumentRoot /www/domain-80
ServerName www.example1.com
DocumentRoot /www/domain-8080
ServerName www.example2.org
DocumentRoot /www/otherdomain-80
ServerName www.example2.org
DocumentRoot /www/otherdomain-8080
?
基於網域名稱和基於IP的混合虛擬機器主機的配置:
Listen 80
NameVirtualHost 172.20.30.40
DocumentRoot /www/example1
ServerName www.example1.com
DocumentRoot /www/example2
ServerName www.example2.org
DocumentRoot /www/example3
ServerName www.example3.net
?
?
?
自己的設定
?
hosts
?
?
?
127.0.0.1 localhost
127.0.0.1 fantsi
?
?
?
httpd.conf
?
?
?
Alias /fantsi/ "E:/xampp/htdocs/fantsi"
NameVirtualHost 127.0.0.1
DocumentRoot "E:/xampp/htdocs"
ServerName localhost
ServerAlias localhost
DocumentRoot "E:/xampp/htdocs/fantsi"
ServerName fantsi
ServerAlias fantsi