Apache VirtualHost的配置

來源:互聯網
上載者:User

標籤:man   directory   tail   tor   follow   this   cal   container   hosts   

自從電腦更換為mac後, 一直沒有時間去配置php的環境。導致每次要更改php代碼的時候, 都是本地更改,然後直接推送到伺服器上運行 這樣的開發與測試及其耗時且繁瑣, 所以早上特地決定弄好mac下的php開發環境,畢竟磨刀不誤砍柴工,一勞永逸嘛。

下載按照好mamp後,選擇web伺服器為apache。更改設定檔httpd.conf,添加相應的載入moudle。開啟虛擬配置。httpd-vhost.conf,因為當前的項目存在兩個獨立的項目,分別是front.xxx.local.admin.xxx.local

httpd-vhost.conf配置如下:

## Virtual Hosts## If you want to maintain multiple domains/hostnames on your# machine you can setup VirtualHost containers for them. Most configurations# use only name-based virtual hosts so the server doesn‘t need to worry about# IP addresses. This is indicated by the asterisks in the directives below.## Please see the documentation at # <URL:http://httpd.apache.org/docs/2.2/vhosts/># for further details before you try to setup virtual hosts.## You may use the command line option ‘-S‘ to verify your virtual host# configuration.## Use name-based virtual hosting.##NameVirtualHost *:80## VirtualHost example:# Almost any Apache directive may go into a VirtualHost container.# The first VirtualHost section is used for all requests that do not# match a ServerName or ServerAlias in any <VirtualHost> block.#<VirtualHost *:80>    DocumentRoot "/Users/fly_popc/Desktop/code/front/web"     ServerName front.xxx.local    ServerAlias front.xxx.local    <Directory "/Users/fly_popc/Desktop/code/front/web">        RewriteEngine on              RewriteCond %{REQUEST_FILENAME} !-f          RewriteCond %{REQUEST_FILENAME} !-d          RewriteRule . index.php     </Directory></VirtualHost><VirtualHost *:80>    DocumentRoot "/Users/fly_popc/Desktop/code/admin/web"     ServerName admin.xxx.local    ServerAlias admin.xxx.local    <Directory "/Users/fly_popc/Desktop/code/admin/web">        RewriteEngine on              RewriteCond %{REQUEST_FILENAME} !-f          RewriteCond %{REQUEST_FILENAME} !-d          RewriteRule . index.php     </Directory></VirtualHost>

重啟apache後,環境運行正常。

但是...

當訪問後台項目的時候,卻一直路由到了第一個地址上, 也就是apache配置多個網域名稱指向的虛擬機器主機訪問總是指向第一個虛擬機器主機。查詢了好幾個文章後,依然不能解決問題。最後終於瞭解到有個配置沒有開啟,那就是:

## Use name-based virtual hosting.#NameVirtualHost *:80

本著追本溯源的精神, 查詢了下這個配置的意思,得到的原因如下:

 NameVirtualHost 如果沒有這個,<VirtualHost>標籤就沒什麼作用。基於網域名稱訪問的情況,若是基於IP訪問的,以第一個指定IP的VirtualHost為準,每個IP可以單獨指定)一個NameVirtualHost 可以對用多個<VirtualHost>,每個<VirtualHost>必須有自己的NameVirtualHostNameVirutalHost *:80制定這個主機的IP和連接埠,如果伺服器上有多個IP,就可以制定某個IP的某個連接埠是哪個主機。(新版的Apache已經去除了NameVirtualHost 這個配置,因為確實沒什麼用,參數在VirtualHost中都已經指明了)。

至此開啟瀏覽器,分別訪問http://front.xxx.local 和http://admin.xxx.local 都能各自訪問到對應的頁面,大功告成!下面記錄下整個流程.

1.先要在httpd.conf(這個是Apache 總的設定檔)中,將虛擬路徑的注釋去掉。
#Include etc/extra/httpd-vhosts.conf
使httpd-vhosts.conf檔案起作用,或者直接在httpd.conf中寫配置也可以,但不建議這麼做。
相關的配置有:Listen NameVirtualHost <VirtualHost>

2.Listen要監聽的連接埠,多個連接埠,要寫多個Listen;否則Apache啟動的時候,不會啟動相應的通訊端。

比如

Listen 80Listen 8080

 

3.NameVirtualHost 如果沒有這個,<VirtualHost>標籤就沒什麼作用。

4.最關鍵的VirtualHost。
重要:Apache 在接受到請求時,首先會預設第一個VirtualHost,然後再找匹配的,如果沒有匹配的,就是第一個VirtualHost起作用。
因此在httpd.conf中,將<Dicrectory />(這個是所有目錄的預設配置)
和<Direcotry /opt/lampp/htdocs>的許可權,都是deny from all.作為預設;或者直接在httpd.conf中配置

<Directory />    Options Indexes FollowSymLinks     AllowOverride All </Directory>

至此,項目就能正常運行起來了!

 

Apache VirtualHost的配置

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.