在獨立檔案內定義 Apache 的虛擬主機

1. 引言

正因為 Apache 設置設的檔案不小,假若每台虛擬主機的容器(定義)都放置在獨立檔案內,尋找及更改它們的設定便容易得多了。

<!> 嚴格來說上游文件並不建議這樣做,但這是個常見的人手改動令系統更易管理。

<!> 你應該記錄系統上的一切人手改動作為日後參考之用。

<!> 虛擬主機的來源檔可以放置在數個不同位置。這個複雜議題備有多個不同的見解,而且不會在這份文件內全面探討。

2. 一般做法:conf.d/

放置獨立虛擬主機設定檔的最常用位置是 /etc/httpd/conf.d/。只要設定檔的名稱與現有或將來模塊的設定檔沒有衝突,它們的名稱可以用來反映所涉及的網站。相關的內容可以放置在 /var/www//var/www/html/ 的子目錄內,例如 /var/www/example.com//var/www/html/example.com/

這樣做的好處就是上層的目錄已經獲建立,而 SELinux 可以透過承繼規則來處理存取權限。當然,套件系統亦運用這個特性,例如透過套件安裝 phpphp.conf 這個檔案便會被放在設定目錄內,而當網頁伺服器重新啟動時,PHP 編譯的頁面便會「運作自如」。

雖然將一個虛擬主機設定檔放在一個滿載 php.confmailman.conf 等與虛擬主機無關的檔案的目錄內,起初看似不洽當,但是細閱文件時,我們在 Apache HTTP Server Version 2.2 - Configuration Sections 內會看見:「多數容器都按每個要求重新運算。」

因此,不論虛擬主機的指令是在設置檔內任何一個位置,又或者是在一個滿載設定檔的目錄內,Apache 永遠先應用所有非虛擬主機的指令,然後才應用虛擬主機的指令。

這方面在系統管理上已成定局,甚至乎上游的最新文件在 CentOS 5 系列內並不包含「系統管理指南」。在 CentOS 4 的系統管理指南中,Chapter 24. Apache HTTP Server Configuration 對這方面有更深入的講解。

3. 虛擬主機檔

你可以直接將虛擬主機容器檔放在設定目錄內,或者採用連結。假如用連結的話,請確定實際案檔的 SELinux 屬性已被更新 。

3.1. 範例

每個檔案都應該含有針對該主機的設定項目。以下是一個 example.conf 範例檔……

# 檔案:/etc/http/conf.d/example.conf
# 虛擬主機:example.org *.example.org
<VirtualHost *:80>
  ServerName example.org
  ServerAlias *.example.org
  ServerAdmin webmaster@example.org
  ErrorLog /var/log/httpd/example.err
  CustomLog /var/log/httpd/example.log combined
  DocumentRoot /var/www/example.org
  <Directory "/var/www/example.org">
    Order allow,deny
    Allow from all
  </Directory>
</VirtualHost>

當然,一個虛擬設定檔可以透過單一目錄樹來對應多於一個 URL 或 DNS 結果。

[userid@webhost conf.d]$ cat example.conf 
### 這裡處理兩個網域的虛擬網站:
### example.com 及 example.org
###
### .COM 網域
<VirtualHost x.x.x.x >
        ServerAdmin webmaster@example.com
        DocumentRoot /var/www/html/example.com/public_html/
        ServerName example.com
        ServerAlias www.example.com
        ErrorLog logs/example.com-error_log
        TransferLog logs/example.com-access_log
        AccessFileName .htaccess
</VirtualHost>

### .ORG 網域
<VirtualHost x.x.x.x >
        ServerAdmin webmaster@example.org
        DocumentRoot /var/www/html/example.org/public_html/
        ServerName example.org
        ServerAlias www.example.org
        ErrorLog logs/example.org-error_log
        TransferLog logs/example.org-access_log
        AccessFileName .htaccess
</VirtualHost>

這個檔案設置兩個網域,一個是 .com,另一個是 .org。當瀏覽器的 URL 指向這些網域或它們的 www 子網域時,同樣的內容便會被傳回,而這是一般想要的結果,但它當然亦會按著低層目錄樹裡的內容,為這兩個不同的首層網域傳回不同的內容。

4. 匯入虛擬主機

有些時候你也許會想停用一台虛擬主機。由於 /etc/httpd/conf/httpd.conf 內的 include 詞句指定 *.conf,你可以透過更改設定檔的名稱來「隱藏」一台虛擬主機。

4.1. 停用虛擬主機

透過在虛擬主機檔的名稱加上一個 _ 來停用該主機:

mv -v /etc/httpd/conf.d/example.conf{,_}

4.2. 啟用虛擬主機

透過去除虛擬主機檔名稱尾部的 _ 來啟用該主機:

mv -v /etc/httpd/conf.d/example.conf{_,}

<!> 利用這個方法來啟用或停用一台虛擬主機,網頁伺服器必須重新啟動才會生效。

5. 重新啟動 Apache

要讓你的改動生效,請重新啟動 Apache。利用 graceful 這個選項會確保所有進序能完成伺服現有的頁面,減低用戶瀏覽時被中斷的機會。

service httpd graceful


{i} Filipe,多請你建議使用 graceful,及提供指引關於 Apache 如何編譯分段。

6. 另類方法:vhost.d/

6.1. 聲明

<!> 這部份所介紹的另類方法超越了很多 CentOS 系統管理員的嚴格、最基本設定。正如任何對 CentOS 系統所作出的人手改動,發行版本更新時也許會導致系統無法運作。

另一個放置虛擬主機容器檔的地方就是建立一個新的目錄,例如 /etc/httpd/vhost.d/,然後在發行版本的設置檔內,於虛擬主機範例的位置放進 include 詞句。這樣做保持了虛擬主機定義在 Apache 設置檔內出現的位置,亦將虛擬主機容器檔與模塊設定檔分割開來。這個做方容讓你更容易地備份虛擬主機設定上的改動,並且去除不同類型的檔案在檔名上有衝突的可能性。

6.2. 建立目錄

建立一個與 /etc/httpd/conf.d/ 有相同 SELinux 屬性的目錄……

mkdir --context=system_u:object_r:httpd_config_t /etc/httpd/vhost.d

6.3. 更新 SELinux 屬性

檢查 SELinux 屬性的方法很簡單……

ls -lZ /etc/httpd

它應該傳回類似以下的東西……

drwxr-xr-x  root root system_u:object_r:httpd_config_t conf
drwxr-xr-x  root root system_u:object_r:httpd_config_t conf.d
lrwxrwxrwx  root root system_u:object_r:httpd_log_t    logs -> ../../var/log/httpd
lrwxrwxrwx  root root system_u:object_r:httpd_modules_t modules -> ../../usr/lib/httpd/modules
lrwxrwxrwx  root root system_u:object_r:httpd_config_t run -> ../../var/run
drwxr-xr-x  root root system_u:object_r:httpd_config_t vhost.d

由於 /etc/httpd/vhost.d/ 位於 /etc/httpd/ 之內,它應該會承繼所有關於 /etc/httpd/ 的原則的改動,並能過渡檔案系統的重新標籤。

6.4. 更新 Apache 的設定

假設 Apache 的設定檔基本上是安裝時預設的版本,簡單地在檔案底部加入 include 這行便成了。我們會仿效模塊設定檔內的 include 詞句匯入以 conf 結尾的檔案。

echo "Include vhost.d/*.conf" >>/etc/httpd/conf/httpd.conf

7. 另類方法:檔案前置詞

對某些人來說,為檔案加入 vhost- 等前置詞也許會更易明、令首層網域更顯著、並可將使用中及停用了的虛擬主機集合起來。

<!> 這個方法必須在 /etc/httpd/conf/httpd.conf 內加入額外的 include 詞句才能生效。

7.1. include 詞句

請不要匯入 .conf 結尾的檔案,改而匯入以 vhost- 起首的檔案:

echo "Include conf.d/vhost-*" >>/etc/httpd/conf/httpd.conf

又或者在利用 vhost.d/ 這個方法時:

echo "Include vhost.d/vhost-*" >>/etc/httpd/conf/httpd.conf

7.2. 停用虛擬主機

在一個虛擬主機檔的名稱起首加上 _ 來防止它被匯入:

mv -v /etc/httpd/conf.d/{,_}vhost-example.org

又或者在利用 vhost.d/ 這個方法時:

mv -v /etc/httpd/vhost.d/{,_}vhost-example.org

7.3. 啟用虛擬主機

移除一個虛擬主機檔名起首的 _ 來容讓它被匯入:

mv -v /etc/httpd/conf.d/{_,}vhost-example.org

又或者在利用 vhost.d/ 這個方法時:

mv -v /etc/httpd/vhost.d/{_,}vhost-example.org

<!> 利用這個方法來啟用或停用一個虛擬主機,網頁伺服器必須重新啟動才會生效。


這頁的英文版本由 Ed Heron 建立。

Translation of revision 18

zh-tw/TipsAndTricks/ApacheVhostDir (last edited 2010-02-27 08:17:16 by TimothyLee)