apache基本組態管理之:apache基於IP,port和網域名稱三種虛擬機器主機配置方法

來源:互聯網
上載者:User

標籤:apche 3中配置方式

apache組態管理

一、基於連接埠,即基於不同的連接埠,相同的IP的虛擬機器主機

     基於相同ip不同port的虛擬機器主機

 
  1)vi /etc/http/conf/httpd.conf

  2)將Listen欄位改為
  Listen 80
  Listen 8888
  (以上設定表示使用80以及8888連接埠)
  
  3)更改虛擬機器主機部分為:
  <VirtualHost 192.168.0.1:80>
  DocumentRoot /var/www/html/website1

      ServerName www.website1.com

  </VirtualHost>
  <VirtualHost 192.168.0.1:8888>;
  DocumentRoot /var/www/html/website2

      ServerName www.website2.com

  </VirtualHost>
  
  4)儲存以上設定
  
  5)建立目錄以及分頁檔:
  #mkdir –p /var/www/html/website1
  #mkdir –p /var/www/html/website2
  #cd /var/www/html/website1
  #cat >index.html <<EOF
  >website1
  >EOF
  #cd /var/www/html/website2
  #cat >index.html <<EOF
  >website2
  >EOF

補充關於EOF:

   在shell中,檔案分界符(通常寫成EOF,你也可以寫成FOE或者其他任何字串)緊跟在<<符號後,  意思是分界符後的內容將被當做標準輸入傳給<<前面的命令

使用格式基本是這樣的: 
命令 << EOF
  內容段
EOF
將“內容段”整個作為命令的輸入。
你的代碼裡就是用cat命令讀入整段字串並賦值給list變數。
其實,不一定要用EOF,只要是“內容段”中沒有出現的字串,都可以用來替代EOF,只是一個起始和結束的標誌罷了。
 
有個特殊用法不得不說:
: << COMMENTBLOCK
   shell指令碼
COMMENTBLOCK
這個用來注釋整段指令碼代碼。 : 是shell中的空語句。
 
搜尋一下 Here document 你會明白更多。

     (註:在/etc/httpd/conf/httpd.conf中有DirectoryIndex index.html index.html.var,表示唯讀index.html,而不讀index.htm,切記)
  
  6)service httpd restart
  
  完成以上設定後,可以通過以下方式訪問:
  
  1)開啟瀏覽器
  
  2)輸入http://192.168.0.1:80以及http://192.168.0.1:8888
  
 二、基於IP,即基於不同IP,相同連接埠的虛擬機器主機

       基於相同Port不同IP的虛擬機器主機
  
  1)不同IP地址的配置:
  #cd /etc/sysconfig/network-scripts
  #cp ifcfg-eth0 ifcfg-eth0:1
  #vi ifcfg-eth0:1
  將eth0:1更改為:
  DEVICE=eth0:1
  ONBOOT=YES
  BOOTPROTO=static
  IPADDR=192.168.0.2
  NETMASK=255.255.255.0

       ifconfig eth0:1 192.168.1.11 臨時生效,需要寫入到/etc/sysconfig/network-scripts/ifcfg-eth0:1設定檔
  2)service network restart
  3)vi /etc/httpd/conf/httpd.conf
  4)更改虛擬機器主機部分為:
  <VirtualHost 192.168.0.1:80>
  DocumentRoot /var/www/html/website1
  </VirtualHost>
  <VirtualHost 192.168.0.2:80>
  DocumentRoot /var/www/html/website2
  </VirtualHost>
  5)建立目錄以及分頁檔:
  #mkdir –p /var/www/html/website1
  #mkdir –p /var/www/html/website2
  #cd /var/www/html/website1
  #cat >index.html <<EOF
  >website1
  >EOF
  #cd /var/www/html/website2
  #cat >index.html <<EOF
  >website2
  >EOF
  
  完成以上設定後,可以通過以下方式訪問:
  
  1)開啟瀏覽器
  2)輸入http://192.168.0.1:80以及http://192.168.0.2:80

三、基於主機名稱,即基於網域名稱的虛擬機器主機的訪問
  

  3.基於網域名稱的虛擬機器主機的訪問

       1) 設定網域名稱映射同一個IP,修改/etc/hosts:

        192.168.0.1  www1.example.com
       192.168.0.1  www2.example.com


  2)建立目錄以及分頁檔:
  #mkdir –p /var/www/html/website1
  #mkdir –p /var/www/html/website2
  #cd /var/www/html/website1
  #cat >index.html <<EOF
  >website1
  >EOF
  #cd /var/www/html/website2
  #cat >index.html <<EOF
  >website2
  >EOF
  

       3)在httpd-vhosts.conf配置虛擬機器主機部分為:
  NameVirtualHost 192.168.0.1   [NameVirtualHost *.80]
  <VirtualHost www1.example.com>
  DocumentRoot /var/www/html/website1
  ServerName www1.example.com
  </VirtualHost>
  <VirtualHost www2.example.com>
  DocumentRoot /var/www/html/website2
  ServerName www2.example.com
  </VirtualHost>
  (註:以上設定中NameVirtualHost不可以省略)


    4)完成以上設定後,可以通過以下方式訪問:
  1)開啟瀏覽器
  2)輸入http://www1.example.com 以及 http://www2.example.com 

發現不能正常訪問,配置的虛 含進來,在http.conf中搜尋vhost這行,然後開啟該行,然後重啟動服務即可。


       

  為了使用基於網域名稱的虛擬機器主機,必須指定伺服器IP地址(和可能的連接埠)來使主機接受請求。可以用NameVirtualHost指令來進行配置。 如果伺服器上所有的IP地址都會用到, 你可以用*作為NameVirtualHost的參數。在NameVirtualHost指令中指明IP地址並不會使伺服器自動偵聽那個IP地址。 這裡設定的IP地址必須對應伺服器上的一個網路介面。

  下一步就是為你建立的每個虛擬機器主機設定<VirtualHost>配置塊,<VirtualHost>的參數與NameVirtualHost指令的參數是一樣的。每個<VirtualHost>定義塊中,至少都會有一個ServerName指令來指定伺服哪個主機和一個DocumentRoot指令來說明這個主機的內容存在於檔案系統的什麼地方。

  如果在現有的web伺服器上增加虛擬機器主機,必須也為現存的主機建造一個<VirtualHost>定義塊。其中ServerName和DocumentRoot所包含的內容應該與全域的保持一致,且要放在設定檔的最前面,扮演預設主控件的角色。

     

NameVirtualHost *:80
<VirtualHost *:80>  

  ServerName *

  DocumentRoot /www/ 

</VirtualHost>

<VirtualHost *:80>

  ServerName www.test1.com

  DocumentRoot /www/test1/

  <Directory "/www/test1">

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

  </Directory>

</VirtualHost>

 

<VirtualHost *:80>

  ServerName www.test2.com

  DocumentRoot /www/test2/

  <Directory "/www/test2">

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

  </Directory>

</VirtualHost>

<VirtualHost *:80>

  ServerName www.test3.com

  DocumentRoot /www/test3/

  <Directory "/www/test3">

    Options Indexes FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

  </Directory>

</VirtualHost>


參考地址:

http://www.cnblogs.com/hi-bazinga/archive/2012/04/23/2466605.html

http://blog.csdn.net/joliny/article/details/2514293




   




本文出自 “12214694” 部落格,請務必保留此出處http://12224694.blog.51cto.com/12214694/1895589

apache基本組態管理之:apache基於IP,port和網域名稱三種虛擬機器主機配置方法

聯繫我們

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