- 論壇徽章:
- 0
|
配置PHP虛擬目錄
----------
摘要
----------
本文極為簡要的介紹了如何在Windows平臺(tái)下安裝Apache2.2.x并配置虛擬目錄. 以作為快速安裝的參考. 詳細(xì)的設(shè)置最好的文檔是官方的說明文檔見下方的鏈接, 在安裝配置的時(shí)候仔細(xì)的查閱文檔是很好的解決辦法.
1. Apache HTTP Server Version 2.2 英文文檔
2. Apache HTTP Server Version 2.2 中文文檔
----------
準(zhǔn)備條件
----------
到 http://httpd.apache.org/ 下載相應(yīng)版本的Apache. (注意: 2.0與2.2版本的Apache對應(yīng)的Module版本頁不同, 互相不兼容, 因此需要根據(jù)應(yīng)用程序來下載相應(yīng)版本的Apache). 如果不需要使用ssl, 則要下載 Apache 2.x.x-win32-x86-no_ssl.msi, 如果需要使用, 則需要下載Apache 2.x.x-win32-x86-openssl-x.x.x.msi
----------
安裝
----------
雙擊安裝程序, 它將引導(dǎo)你完成整個(gè)安裝過程。請確保你輸入正確的服務(wù)器URL(如果你的服務(wù)器沒有DNS名稱,你可以直接輸入IP地址)。建議將Apache安裝成" for all Users,on Port 80,as a service" Apache將自動(dòng)安裝一個(gè)系統(tǒng)服務(wù)以自動(dòng)啟動(dòng)。注意:如果你已經(jīng)有一個(gè)IIS或者其它的程序使用80端口,安裝可能會(huì)失敗。如果是這樣,你可以到程序目錄下的Apache Group\Apache2\conf目錄中找到文件httpd.conf,將其中的Listen 80改成一個(gè)不用的端口,比如Listen 81。然后重新運(yùn)行這個(gè)安裝程序,這次應(yīng)該不會(huì)再有問題了。
安裝結(jié)束之后在瀏覽器中鍵入 http://loccalhost,如果呈現(xiàn)了一個(gè)已經(jīng)配置好的網(wǎng)站,那么Apache就安裝成功了。 如果你將Apache安裝成一個(gè)服務(wù),它將以本地system帳戶來運(yùn)行。為Apache創(chuàng)建一個(gè)單獨(dú)的用戶來運(yùn)行它將會(huì)更安全一些。
打開Apache的安裝目錄, 找到文件 /conf/httpd.conf, 打開它進(jìn)行配置的編輯. 配置文件中對于各項(xiàng)都有比較詳細(xì)的描述, 在配置結(jié)束之后可以運(yùn)行開始菜單中Apache安裝文件夾中的Test Configuration工具來檢驗(yàn)配置文件是否正確. 如果配置不正確會(huì)給出簡要的說明. 如果在配置結(jié)束之后通過了Test Configuration工具, 但是仍然不能夠順利運(yùn)行Apache, 可以到[Apache安裝目錄]/logs/ 這個(gè)目錄中查看記錄, 以找出問題所在.
----------
配置 httpd.conf文件
----------
# 設(shè)置文檔主目錄, 后面的就是磁盤的目錄
DocumentRoot "D:/Programs/Apache2.2/htdocs"
# 添加虛擬目錄, 下面的設(shè)置在瀏覽器中輸入 http://localhost/webpath/ 的時(shí)候機(jī)會(huì)訪問服務(wù)器的"D:/Programs/WebPath"目錄
Alias /webpath/ "D:/Programs/WebPath"
# 當(dāng)/webPath 后面不加"/"的話, 在瀏覽器中可以輸入 http://localhost/webpath Apache會(huì)自動(dòng)的在后面添加"/"
Alias /webpath "D:/Programs/WebPath"
# URL 重定向
Redirect permanent /foo/ http://www.example.com/bar/
# 設(shè)定虛擬主機(jī): 請參考 http://lamp.linux.gov.cn/Apache/ApacheMenu/vhosts/index.html
在WIN下安裝APACHE配置虛擬目錄和UNIN下基本是一樣的
就是修改httpd.conf
1:單個(gè)IP對應(yīng)單個(gè)玉米
例如:www.phpunion.com對應(yīng)192.168.1.1
www.feelone.net對應(yīng)192.168.1.2
在httpd.conf中加入如下指令
<VirtualHost 192.168.1.1> #對應(yīng)玉米所指向的IP
DocumentRoot "c:/web/php" #www.phpunion.com要指向的目錄,注意一定要有""
ServerName www.phpunion.com #所要綁的玉米
</VirtualHost>
<VirtualHost 192.168.1.2> #對應(yīng)玉米所指向的IP
DocumentRoot "c:/web/feel" #www.feelone.net要指向的目錄,注意一定要有""
ServerName www.feelone.net #所要綁的玉米
</VirtualHost>
剩下的可以繼續(xù)綁!
2:單個(gè)IP對應(yīng)多個(gè)玉米
NameVirtualHost 192.168.1.1 #記得一定要有著句啊!
<VirtualHost 192.168.1.1> #對應(yīng)玉米所指向的IP
DocumentRoot "c:/web/php" #www.phpunion.com要指向的目錄,注意一定要有""
ServerName www.phpunion.com #所要綁的玉米
</VirtualHost>
<VirtualHost 192.168.1.1> #對應(yīng)玉米所指向的IP
DocumentRoot "c:/web/feel" #www.feelone.net要指向的目錄,注意一定要有""
ServerName www.feelone.net #所要綁的玉米
</VirtualHost>
這樣就綁上對應(yīng)的目錄了,如果沒有NameVirtualHost 192.168.1.1 這個(gè)指令的話,所有的玉米就都指到php目錄了.
然后重新啟動(dòng)apache,就可以看到所指的玉米對應(yīng)相應(yīng)的虛擬目錄!
Example:
<VirtualHost 10.1.2.3>
ServerAdmin webmaster@host.example.com
DocumentRoot /www/docs/host.example.com
ServerName host.example.com
ErrorLog logs/host.example.com-error_log
TransferLog logs/host.example.com-access_log
</VirtualHost>
今天網(wǎng)站建設(shè)技術(shù)部的PHP程序員教了我一個(gè)在本地實(shí)現(xiàn)虛擬一個(gè)域名來訪問本機(jī)電腦的數(shù)據(jù)的方法,首先說說我本地的環(huán)境是:WIN2003+xampp。其實(shí)能實(shí)現(xiàn)效果就行,直接用xampp的最好方法就是省事,不用一個(gè)一個(gè)軟件安裝!然后下面一步一步實(shí)現(xiàn)虛擬一個(gè)www.123.com來訪問本地網(wǎng)站程序。
1.關(guān)閉xampp的apache,然后找到C:\xampp\apache\conf\extra\httpd-vhosts.conf文件:
在##NameVirtualHost *:80 下面加入以下代碼,其中的IP為本地電腦的IP,可以進(jìn)入CMD使用ipconfig查詢到本機(jī)IP
NameVirtualHost 192.168.1.101:80
然后把以下代碼復(fù)制一份
##<VirtualHost *:80>
##ServerAdmin postmaster@dummy-host2.localhost
##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
##</VirtualHost>
修改成以下代碼
<VirtualHost *:80>
##ServerAdmin postmaster@dummy-host2.localhost
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
</VirtualHost>
其中DocumentRoot 是本機(jī)網(wǎng)站制作程序的路徑,ServerName及ServerAlias 就是要虛擬的域名網(wǎng)址,這個(gè)隨便你高興了
2.找到C:\xampp\apache\conf\httpd.conf文件,找到以下代碼:
DocumentRoot "C:/xampp/htdocs"
<Directory "C:/xampp/htdocs">
把上面的兩個(gè)網(wǎng)站目錄的路徑修改成上面設(shè)置的路徑
DocumentRoot "E:/php"
<Directory "E:/php">
因?yàn)樵O(shè)置了虛擬的域名網(wǎng)址,所以路徑不用再加上網(wǎng)站的文件夾名稱了
3.然后打開C:\WINDOWS\system32\drivers\etc\hosts 文件,
在127.0.0.1 localhost下添加你要添加的虛擬域名網(wǎng)址
127.0.0.1 localhost
192.168.1.101 www.123.com
呵呵,修改這三個(gè)文件之后就OK了,現(xiàn)在可以重新啟動(dòng)xampp的apache了,然后在IE里輸入www.123.com就能訪問本地的網(wǎng)站程序了,這樣測試的好處是能把本機(jī)虛擬成服務(wù)器,有些東東很方便就能調(diào)試!當(dāng)然,用localhost也是還可以訪問了,這個(gè)功能用不到的話也沒必要這么搞,我是因?yàn)橛袝r(shí)用到Magento程序,使用域名會(huì)更好,不會(huì)有一些莫名其妙的問題。當(dāng)然了,如果你有好幾個(gè)網(wǎng)站的話,每次都要修改一下C:\xampp \apache\conf\extra\httpd-vhosts.conf對應(yīng)的DocumentRoot "E:/php/ecshop2.72",因?yàn)槲业木W(wǎng)頁設(shè)計(jì)的PHP程序都是放在E盤下的PHP文件夾,然后每個(gè)網(wǎng)站就一個(gè)文件夾,這樣每次只需要修改一個(gè)文件就OK了,看個(gè)人愛好了,共享出來讓大家玩玩,嘿嘿!
詳細(xì)配置:http://httpd.apache.org/docs/2.2/vhosts/
中文配置:http://www.zzxj.net/web/manual/A ... /rewrite_guide.html
Apache虛擬主機(jī)的配置
虛擬主機(jī)的配置
基于IP地址的虛擬主機(jī)配置
Listen 80
DocumentRoot /www/example1
ServerName www.example1.com
DocumentRoot /www/example2
ServerName www.example2.org
基于IP和多端口的虛擬主機(jī)配置
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
單個(gè)IP地址的服務(wù)器上基于域名的虛擬主機(jī)配置:
# 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
在多個(gè)IP地址的服務(wù)器上配置基于域名的虛擬主機(jī):
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 …
在不同的端口上運(yùn)行不同的站點(diǎn):
基于多端口的服務(wù)器上配置基于域名的虛擬主機(jī)。
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的混合虛擬主機(jī)的配置:
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
自己的設(shè)置
hosts
127.0.0.1 localhost
127.0.0.1 fantsi
httpd.conf- Alias /fantsi/ "E:/xampp/htdocs/fantsi"
- NameVirtualHost 127.0.0.1
- <VirtualHost 127.0.0.1 >
- DocumentRoot "E:/xampp/htdocs"
- ServerName localhost
- ServerAlias localhost
- </VirtualHost>
- <VirtualHost 127.0.0.1 >
- DocumentRoot "E:/xampp/htdocs/fantsi"
- ServerName fantsi
- ServerAlias fantsi
- </VirtualHost>
復(fù)制代碼 |
|