首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Apache 虛擬 IP 與基於 IP 的虛擬主機配置詳解

     編輯:PHP 相關知識

Apache 虛擬 IP 與基於 IP 的虛擬主機配置詳解

Apache 支援「基於 IP」與「基於域名」兩種虛擬主機模式。本文針對基於 IP 的虛擬主機,說明 Windows 與 Linux 下的完整配置步驟,並探討高可用架構中虛擬 IP(VIP)的角色。

一、Windows (XAMPP) 範例

在 httpd-vhosts.conf 中定義兩個不同 IP 的虛擬主機:

<VirtualHost 127.0.0.1:80>
    DocumentRoot "C:/xampp/htdocs/yiyunmap"
    ServerName 127.0.0.1:80
</VirtualHost>

<VirtualHost 127.0.0.2:80>
    DocumentRoot "C:/xampp/htdocs/user"
    ServerName 127.0.0.2:80
</VirtualHost>

<Directory "C:/xampp/htdocs/yiyunmap">
    Options Indexes FollowSymLinks Multiviews
    AllowOverride All
    Require all granted
</Directory>

<Directory "C:/xampp/htdocs/user">
    Options Indexes FollowSymLinks Multiviews
    AllowOverride All
    Require all granted
</Directory>

需在系統網卡綁定 127.0.0.2 別名(Windows 可用 netsh interface ip add address 或圖形介面新增)。

二、Linux (CentOS/RHEL) 完整步驟

  1. 配置網卡別名:
    ifconfig eth0 192.168.149.1 netmask 255.255.255.0 up
    ifconfig eth0:1 192.168.149.2 netmask 255.255.255.0 up
    永久生效請修改 /etc/sysconfig/network-scripts/ifcfg-eth0:1。
  2. Apache 虛擬主機配置(/etc/httpd/conf/httpd.conf 或 conf.d/vhosts.conf):
    <VirtualHost 192.168.149.1>
        ServerAdmin root@abc.com
        DocumentRoot /www/abc
        ServerName www.abc.com
    </VirtualHost>
    
    <VirtualHost 192.168.149.2>
        ServerAdmin root@good.com
        DocumentRoot /www/good
        ServerName www.good.com
    </VirtualHost>
  3. 建立測試首頁:
    echo 'Welcome to abc' > /www/abc/index.html
    echo 'Welcome to GOOD' > /www/good/index.html
  4. 重啟服務:service httpd restart

三、高可用架構中的虛擬 IP (VIP)

單點 Apache 宕機會導致整個叢集不可用。生產環境常採用 LVS (Linux Virtual Server) + Keepalived / Heartbeat 方案:

  • LVS 工作於網路層/傳輸層(L4),效能極高,支援 NAT/DR/TUN 模式。
  • Keepalived 提供 VRRP 協議,實現 VIP 漂移與健康檢查。
  • Apache 僅作為後端 Real Server,專注於應用層邏輯。

架構圖:Client → VIP (Keepalived) → LVS → Apache 叢集

四、常見誤區

以為 Apache 就能做高可用負載均衡?
Apache mod_proxy_balancer 屬應用層轉發,效能與健檢能力遠弱於 LVS,適合中小流量;大併發必須上 LVS/Nginx L4。
Windows 下直接修改 hosts 測試 VIP?
Windows 沒有 VRRP 實作,無法真正模擬 VIP 漂移,建議使用 Linux 虛擬機驗證。

正確規劃虛擬 IP 與虛擬主機,是構建可擴展、高可用 Web 基礎設施的關鍵一步。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved