了解一下根目錄和虛擬目錄的關系:
安裝過apache之后打開httpd.conf文件會發現DocumentRoot默認設置的應該是apache安裝目錄中的htdocs文件夾。然后你的網頁就可以放大這個htdocs文件夾中測試,例你在里面放了一個1.PHP,可以輸入http://127.0.0.1/1.php進行測試。這個DocumentRoot后面的路徑就是apache的根目錄。有時候我們不想將自己的網站放到這里,例如我想要放到F:/MyPHP中來運行,那么就可以修改DocumentRoot為F:/MyPHP,然后將那個1.php放入F:/MyPHP文件夾,同樣使用http://127.0.0.1/1.php可以正常訪問。
虛擬目錄是什么?顧名思義只是一個虛擬的目錄,和真實目錄是有差別的。先來看看使用真實目錄訪問,我們在剛才的F:/MyPHP中建立一個文件夾Test,然后在里面放入2.php,這個時候就可以通過http://127.0.0.1/Test/2.php訪問。但是有些時候你可能建立的文件夾和想要輸入的訪問地址有一個映射關系,而不是直接輸入Test文件夾名,這樣做的原因有很多其中就有一個是安全問題,因為那樣別人就會知道你的根目錄的文件夾。具體點,你在Test文件夾中放置了2.php,但是想要通過http://127.0.0.1/cmj/2.php訪問2.php而不是通過http://127.0.0.1/Test/2.php來訪問怎么辦呢?這個時候我們就需要虛擬目錄了,很明顯沒有cmj這個文件夾,但是又能夠像訪問一個正式的目錄一樣來訪問,就需要一種映射關系。
多個虛擬目錄
首先把Apache安裝到D:/Program Files/Apache2.2目錄下,端口號設置為8080,安裝完成后默認的網站根目錄為D:/Program Files/Apache2.2/htdocs,通常我們可以在htdocs下面建立個文件夾MySite,然后在瀏覽器輸入:http://localhost:8080/MySite 這樣就可以看到我們自己的站點了。然而有時我們想把站點放到其它目錄下面,這時就需要配置虛擬目錄了
比如我們在D盤建立如下文件夾D:/Code/WebSite,然后通過http://localhost:8080/DemoSite來訪問這個站點
打開httpd.conf文件,搜索<IfModule alias_module> 節點,然后在節點內輸入以下內容:
#下面是虛擬目錄聲明格式#Alias用來定義虛擬目錄及虛擬目錄路徑,其中虛擬目錄名稱用于URL訪問的路徑別名,可以和虛擬目錄名稱不同#<Directory/>節點用于定義目錄的訪問權限等##Alias 虛擬目錄名稱 虛擬目錄路徑#<Directory 虛擬目錄路徑># Options Indexes FollowSymLinks# AllowOverride All# Order allow,deny# Allow from all#</Directory>#下面是具體的示例,/DemoSite是目錄別名 "D:/Code/WebSite"是虛擬目錄的實際路徑Alias /DemoSite "D:/Code/WebSite"<Directory "D:/Code/WebSite"> Options Indexes FollowSymLinks AllowOverride All Order allow,deny Allow from all</Directory>
重啟Apache服務后,在瀏覽器輸入http://localhost:8080/DemoSite就可以正常訪問了
這里需要注意下目錄盡量使用"/",而不是使用"/",原因就是"/"代表轉義符有些情況下會導致莫名奇妙的錯誤,下面附上完整的<IfModule alias_module>節點以供參考
<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>
多主機頭綁定
(就是在一個端口上綁定多個域名,然后每個域名可以指向不同的目錄進行訪問,主機頭是IIS里面的說法),打開httpd.conf文件,在文件最后添加如下內容
#多主機頭配置無需放在特定的節點下面,一般直接在配置文件底部添加即可#NameVirtualHost addr[:port] 為一個基于域名的虛擬主機指定一個IP地址(和端口)#聲明主機頭必須加這條指令,否者主機頭配置不會生效#VirtualHost節點下面ServerName就是要綁定的域名,DocumentRoot表示此域名指向的目錄#本機測試的話請在hosts中進行域名綁定如 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服務,瀏覽器輸入www.mysite1.com:8080,就會自動定向到D:/Program Files/Apache2.2/htdocs站點了
輸入www.mysite2.com:8080就會自動定向到D:/Code/MySite站點,如此就可以實現在一個服務器上同時運行多個站點
新聞熱點
疑難解答