了解一下根目錄和虛擬目錄的關(guān)系:
安裝過apache之后打開httpd.conf文件會(huì)發(fā)現(xiàn)DocumentRoot默認(rèn)設(shè)置的應(yīng)該是apache安裝目錄中的htdocs文件夾。然后你的網(wǎng)頁就可以放大這個(gè)htdocs文件夾中測(cè)試,例你在里面放了一個(gè)1.PHP,可以輸入http://127.0.0.1/1.php進(jìn)行測(cè)試。這個(gè)DocumentRoot后面的路徑就是apache的根目錄。有時(shí)候我們不想將自己的網(wǎng)站放到這里,例如我想要放到F:/MyPHP中來運(yùn)行,那么就可以修改DocumentRoot為F:/MyPHP,然后將那個(gè)1.php放入F:/MyPHP文件夾,同樣使用http://127.0.0.1/1.php可以正常訪問。
虛擬目錄是什么?顧名思義只是一個(gè)虛擬的目錄,和真實(shí)目錄是有差別的。先來看看使用真實(shí)目錄訪問,我們?cè)趧偛诺腇:/MyPHP中建立一個(gè)文件夾Test,然后在里面放入2.php,這個(gè)時(shí)候就可以通過http://127.0.0.1/Test/2.php訪問。但是有些時(shí)候你可能建立的文件夾和想要輸入的訪問地址有一個(gè)映射關(guān)系,而不是直接輸入Test文件夾名,這樣做的原因有很多其中就有一個(gè)是安全問題,因?yàn)槟菢觿e人就會(huì)知道你的根目錄的文件夾。具體點(diǎn),你在Test文件夾中放置了2.php,但是想要通過http://127.0.0.1/cmj/2.php訪問2.php而不是通過http://127.0.0.1/Test/2.php來訪問怎么辦呢?這個(gè)時(shí)候我們就需要虛擬目錄了,很明顯沒有cmj這個(gè)文件夾,但是又能夠像訪問一個(gè)正式的目錄一樣來訪問,就需要一種映射關(guān)系。
多個(gè)虛擬目錄
首先把Apache安裝到D:/Program Files/Apache2.2目錄下,端口號(hào)設(shè)置為8080,安裝完成后默認(rèn)的網(wǎng)站根目錄為D:/Program Files/Apache2.2/htdocs,通常我們可以在htdocs下面建立個(gè)文件夾MySite,然后在瀏覽器輸入:http://localhost:8080/MySite 這樣就可以看到我們自己的站點(diǎn)了。然而有時(shí)我們想把站點(diǎn)放到其它目錄下面,這時(shí)就需要配置虛擬目錄了
比如我們?cè)贒盤建立如下文件夾D:/Code/WebSite,然后通過http://localhost:8080/DemoSite來訪問這個(gè)站點(diǎn)
打開httpd.conf文件,搜索<IfModule alias_module> 節(jié)點(diǎn),然后在節(jié)點(diǎn)內(nèi)輸入以下內(nèi)容:
#下面是虛擬目錄聲明格式#Alias用來定義虛擬目錄及虛擬目錄路徑,其中虛擬目錄名稱用于URL訪問的路徑別名,可以和虛擬目錄名稱不同#<Directory/>節(jié)點(diǎn)用于定義目錄的訪問權(quán)限等##Alias 虛擬目錄名稱 虛擬目錄路徑#<Directory 虛擬目錄路徑># Options Indexes FollowSymLinks# AllowOverride All# Order allow,deny# Allow from all#</Directory>#下面是具體的示例,/DemoSite是目錄別名 "D:/Code/WebSite"是虛擬目錄的實(shí)際路徑Alias /DemoSite "D:/Code/WebSite"<Directory "D:/Code/WebSite"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all</Directory>
重啟Apache服務(wù)后,在瀏覽器輸入http://localhost:8080/DemoSite就可以正常訪問了
這里需要注意下目錄盡量使用"/",而不是使用"/",原因就是"/"代表轉(zhuǎn)義符有些情況下會(huì)導(dǎo)致莫名奇妙的錯(cuò)誤,下面附上完整的<IfModule alias_module>節(jié)點(diǎn)以供參考
<IfModule alias_module> # # Redirect: Allows you to tell clients about documents that used to # exist in your server's namespace, but do not anymore. The client # will make a new request for the document at its new location. # Example: # Redirect permanent /foo http://localhost/bar # # Alias: Maps web paths into filesystem paths and is used to # access content that does not live under the DocumentRoot. # Example: # Alias /webpath /full/filesystem/path # # If you include a trailing / on /webpath then the server will # require it to be present in the URL. You will also likely # need to provide a <Directory> section to allow access to # the filesystem path. # # ScriptAlias: This controls which directories contain server scripts. # ScriptAliases are essentially the same as Aliases, except that # documents in the target directory are treated as applications and # run by the server when requested rather than as documents sent to the # client. The same rules about trailing "/" apply to ScriptAlias # directives as to Alias. # ScriptAlias /cgi-bin/ "D:/Program Files/Apache2.2/cgi-bin/" Alias /DemoSite "D:/Code/WebSite" <Directory "D:/Code/WebSite"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory></IfModule>
多主機(jī)頭綁定
(就是在一個(gè)端口上綁定多個(gè)域名,然后每個(gè)域名可以指向不同的目錄進(jìn)行訪問,主機(jī)頭是IIS里面的說法),打開httpd.conf文件,在文件最后添加如下內(nèi)容
#多主機(jī)頭配置無需放在特定的節(jié)點(diǎn)下面,一般直接在配置文件底部添加即可#NameVirtualHost addr[:port] 為一個(gè)基于域名的虛擬主機(jī)指定一個(gè)IP地址(和端口)#聲明主機(jī)頭必須加這條指令,否者主機(jī)頭配置不會(huì)生效#VirtualHost節(jié)點(diǎn)下面ServerName就是要綁定的域名,DocumentRoot表示此域名指向的目錄#本機(jī)測(cè)試的話請(qǐng)?jiān)趆osts中進(jìn)行域名綁定如 127.0.0.1 www.mysite1.comNameVirtualHost *:8080<VirtualHost *:8080> ServerName www.mysite1.com DocumentRoot "D:/Program Files/Apache2.2/htdocs"</VirtualHost><VirtualHost *:8080> ServerName www.mysite2.com DocumentRoot "D:/Code/MySite"</VirtualHost>
配置好后,重啟apache服務(wù),瀏覽器輸入www.mysite1.com:8080,就會(huì)自動(dòng)定向到D:/Program Files/Apache2.2/htdocs站點(diǎn)了
輸入www.mysite2.com:8080就會(huì)自動(dòng)定向到D:/Code/MySite站點(diǎn),如此就可以實(shí)現(xiàn)在一個(gè)服務(wù)器上同時(shí)運(yùn)行多個(gè)站點(diǎn)
新聞熱點(diǎn)
疑難解答
圖片精選