***阿里雲ECS實戰配置虛擬主機 + Apache 配置虛擬主機三種方式

阿里雲ECS實戰配置虛擬主機html

買了一臺ECS阿里雲服務器,性能感受有點富餘,想着能夠陪着虛擬主機多一些WWW目錄好放一些其餘的程序。好比DEMO什麼的。web

今天研究了下,主要是就是作基於不一樣域名的虛擬主機,也就是下面參考方案中的第二個。服務器

 

1. 設置域名映射同一個IP,修改hosts(host文件位置:/etc/hosts):網絡

192.168.1.10  abc.com
192.168.1.10  test.tang.cn

 2. 跟上面同樣,創建虛擬主機存放網頁的根目錄(本人採用的是XAMPP)ide

/htdocs/abc/1.html
/htdocs/test/2.html

 

 3. 在httpd.conf中將附加配置文件httpd-vhosts.conf包含進來,接着在httpd-vhosts.conf中寫入以下配置:
    就是將httpd.conf文件中的 (位置:/opt/lampp/etc/extra)
Include etc/extra/httpd-vhosts.conf  註釋去掉
# Virtual hosts
Include etc/extra/httpd-vhosts.conf
  爲了使用基於域名的虛擬主機,必須指定服務器IP地址(和可能的端口)來使主機接受請求。能夠用NameVirtualHost指令來進行配置。 若是服務器上全部的IP地址都會用到, 你能夠用*做爲NameVirtualHost的參數。在NameVirtualHost指令中指明IP地址並不會使服務器自動偵聽那個IP地址。 這裏設定的IP地址必須對應服務器上的一個網絡接口。

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

  若是在現有的web服務器上增長虛擬主機,必須也爲現存的主機建造一個<VirtualHost>定義塊。其中ServerName和DocumentRoot所包含的內容應該與全局的保持一致,且要放在配置文件的最前面,扮演默認主機的角色。測試

   注意參考httpd-vhosts.conf中的推薦寫法來寫,有些沒有必要的不用寫阿里雲

<VirtualHost *:80>

ServerAdmin abc@163.com
DocumentRoot "/opt/lampp/htdocs/abc/"
ServerName abc.com
ServerAlias www.abc.com
ErrorLog "logs/abc.com-error_log"
CustomLog "logs/abc.com-access_log" common
</VirtualHost>
spa


<VirtualHost *:80>
ServerAdmin tang@163.com
DocumentRoot "/opt/lampp/htdocs/test/"
ServerName test.tang.cn
ServerAlias www.test.tang.cn
ErrorLog "logs/test.tang.cn-error_log"
CustomLog "logs/test.tang.cn-access_log" common
</VirtualHost>code

 4. 大功告成,重啓服務器,測試下每一個虛擬主機,分別訪問www.abc.com、test.tang.cn

 

 

 

 

 

 

 

 

 


 

1、基於IP

 1. 假設服務器有個IP地址爲192.168.1.10,使用ifconfig在同一個網絡接口eth0上綁定3個IP:

[root@localhost root]# ifconfig eth0:1 192.168.1.11
[root@localhost root]# ifconfig eth0:2 192.168.1.12
[root@localhost root]# ifconfig eth0:3 192.168.1.13

 2. 修改hosts文件,添加三個域名與之一一對應:

192.168.1.11   www.test1.com
192.168.1.12   www.test2.com
192.168.1.13   www.test3.com

 3. 創建虛擬主機存放網頁的根目錄,如在/www目錄下創建test一、test二、test3文件夾,其中分別存放1.html、2.html、3.html

/www/test1/1.html
/www/test2/2.html
/www/test3/3.html

 

 4. 在httpd.conf中將附加配置文件httpd-vhosts.conf包含進來,接着在httpd-vhosts.conf中寫入以下配置:

 

複製代碼
<VirtualHost 192.168.1.11: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 192.168.1.12:80>
  ServerName www.test1.com
  DocumentRoot /www/test2/
  <Directory "/www/test2">
     Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow From All
   </Directory>
</VirtualHost>

<VirtualHost 192.168.1.13:80>
  ServerName www.test1.com
  DocumentRoot /www/test3/
  <Directory "/www/test3">
     Options Indexes FollowSymLinks
     AllowOverride None
     Order allow,deny
     Allow From All
   </Directory>
</VirtualHost>
複製代碼

 5. 大功告成,測試下每一個虛擬主機,分別訪問www.test1.com、www.test2.com、www.test3.com

 

2、基於主機名(域名)

 1. 設置域名映射同一個IP,修改hosts:

192.168.1.10  www.test1.com
192.168.1.10  www.test2.com
192.168.1.10  www.test3.com

 2. 跟上面同樣,創建虛擬主機存放網頁的根目錄

/www/test1/1.html
/www/test2/2.html
/www/test3/3.html

 

 3. 在httpd.conf中將附加配置文件httpd-vhosts.conf包含進來,接着在httpd-vhosts.conf中寫入以下配置:

 

  爲了使用基於域名的虛擬主機,必須指定服務器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>
複製代碼

 4. 大功告成,測試下每一個虛擬主機,分別訪問www.test1.com、www.test2.com、www.test3.com

 

3、基於端口

1.  修改配置文件

  將原來的

    Listen 80
      改成
      Listen 80
      Listen 8080


2. 更改虛擬主機設置:

複製代碼
<VirtualHost 192.168.1.10:80>
    DocumentRoot /var/www/test1/
    ServerName www.test1.com
</VirtualHost>

<VirtualHost 192.168.1.10:8080>
    DocumentRoot /var/www/test2
    ServerName www.test2.com
</VirtualHost>
複製代碼
相關文章
相關標籤/搜索