Apache 虛擬機器主機NameVirtualHost

來源:互聯網
上載者:User

標籤:

httpd支援的虛擬機器主機類型包括以下三種

  • 基於網域名稱:為每個虛擬機器主機使用不同的網域名稱、但其對應的IP使相同的。
  • 基於IP地址:為每個虛擬機器主機使用不同的網域名稱,切各自對應的IP地址也不相同。
  • 基於連接埠:這種方式並不使用網域名稱、IP地址來區分不同的網站內容,而是使用了不同的TCP連接埠號碼,因此使用者在瀏覽不同的虛擬機器主機網站時需要同時指定連接埠號碼才能訪問。

1、基於網域名稱的虛擬機器主機

  • 為虛擬機器主機提供網域名稱解析

可以自行搭建測試使用的DNS伺服器、這裡採用本地hosts解析

  •  為虛擬機器主機準備網頁文檔

為每個虛擬web主機準備網站目錄及網頁文檔

[[email protected] /]# mkdir -p /var/www/html/bbs[[email protected] /]# mkdir -p /var/www/html/blog[[email protected] /]# echo "<h1>bbs.httpd.com<h1/>" > /var/www/html/bbs/index.html[[email protected] /]# echo "<h1>blog.httpd.com<h1/>" > /var/www/html/blog/index.html

 

  • 添加虛擬機器主機配置
[[email protected] /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf                       //配置獨立的設定檔NameVirtualHost *:80      //設定虛擬機器主機監聽地址<Directory "/var/www/html">   //設定目錄存取權限  Order allow,deny  Allow from all</Directory><VirtualHost *:80>                         DocumentRoot "/var/www/html/bbs"    ServerName bbs.httpd.com    ErrorLog "logs/bbs.httpd.com.error_log"    CustomLog "logs/bbs.httpd.com.access_log" common</VirtualHost><VirtualHost *:80>    DocumentRoot "/var/www/html/blog"    ServerName blog.httpd.com    ErrorLog "logs/blog.httpd.com.error_log"    CustomLog "logs/dummy-host2.example.com.access_log" common</VirtualHost>[[email protected] /]# vim /usr/local/httpd/conf/httpd.confInclude conf/extra/httpd-vhosts.conf    //載入獨立的設定檔
[[email protected] /]# /usr/local/httpd/bin/apachectl restart  //重啟服務使配置生效
 

 

  • 修改hosts檔案
[[email protected] /]# vim /etc/hosts192.168.119.131  bbs.httpd.com192.168.119.131  blog.httpd.com

 

  • 在客戶機中訪問虛擬web主機
[[email protected] /]# curl bbs.httpd.com<h1>bbs.httpd.com<h1/>[[email protected] /]# curl blog.httpd.com<h1>blog.httpd.com<h1/>

 

2、基於IP地址的虛擬機器主機

配置基於IP地址的虛擬web主機時,不使用NameVirtualHost配置項來指定監聽服務的IP地址

[[email protected] /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf                       //配置獨立的設定檔<Directory "/var/www/html">   //設定目錄存取權限  Order allow,deny  Allow from all</Directory><VirtualHost 192.168.119.131:80>                         DocumentRoot "/var/www/html/bbs"    ServerName bbs.httpd.com    ErrorLog "logs/bbs.httpd.com.error_log"    CustomLog "logs/bbs.httpd.com.access_log" common</VirtualHost><VirtualHost 192.168.119.132:80>    DocumentRoot "/var/www/html/blog"    ServerName blog.httpd.com    ErrorLog "logs/blog.httpd.com.error_log"    CustomLog "logs/dummy-host2.example.com.access_log" common</VirtualHost>[[email protected] /]# vim /usr/local/httpd/conf/httpd.confInclude conf/extra/httpd-vhosts.conf    //載入獨立的設定檔[[email protected] /]# /usr/local/httpd/bin/apachectl restart  //重啟服務使配置生效

 

3、基於連接埠的虛擬機器主機

配置基於連接埠的虛擬web主機時,不使用NameVirtualHost配置項來指定監聽服務的IP地址
[[email protected] /]# vim /usr/local/httpd/conf/extra/httpd-vhosts.conf                       //配置獨立的設定檔<Directory "/var/www/html">   //設定目錄存取權限  Order allow,deny  Allow from all</Directory><VirtualHost 192.168.119.131:80>                         DocumentRoot "/var/www/html/bbs"    ServerName bbs.httpd.com    ErrorLog "logs/bbs.httpd.com.error_log"    CustomLog "logs/bbs.httpd.com.access_log" common</VirtualHost><VirtualHost 192.168.119.131:80>    DocumentRoot "/var/www/html/blog"    ServerName blog.httpd.com    ErrorLog "logs/blog.httpd.com.error_log"    CustomLog "logs/dummy-host2.example.com.access_log" common</VirtualHost>[[email protected] /]# vim /usr/local/httpd/conf/httpd.confInclude conf/extra/httpd-vhosts.conf    //載入獨立的設定檔Listen 192.168.119.131:80    //監聽80連接埠Listen 192.168.119.131:8080   //監聽8080連接埠[[email protected] /]# /usr/local/httpd/bin/apachectl restart  //重啟服務使配置生效

 

Apache 虛擬機器主機NameVirtualHost

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.