基于不同情況下配置apache虛擬主機的常用方法一共三種情況。首先查看你虛擬主機屬于哪一種情況,然后對號入座就好了。過程不復(fù)雜,關(guān)鍵在于對癥下藥。
基于不同情況下配置apache虛擬主機的常用方法一共三種情況。首先查看你虛擬主機屬于哪一種情況,然后對號入座就好了。過程不復(fù)雜,關(guān)鍵在于對癥下藥。
一、基于IP
1. 假設(shè)服務(wù)器有個IP地址為192.168.1.10,使用ifconfig在同一個網(wǎng)絡(luò)接口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文件,添加三個域名與之一一對應(yīng):
192.168.1.11 www.test1.com
192.168.1.12 www.test2.com
192.168.1.13 www.test3.com
3. 建立虛擬主機存放網(wǎng)頁的根目錄,如在/www目錄下建立test1、test2、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中寫入如下配置:
復(fù)制代碼
<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>
復(fù)制代碼
5. 大功告成,測試下每個虛擬主機,分別訪問www.test1.com、www.test2.com、www.test3.com
二、基于主機名
1. 設(shè)置域名映射同一個IP,修改hosts:
192.168.1.10 www.test1.com
192.168.1.10 www.test2.com
192.168.1.10 www.test3.com
2. 跟上面一樣,建立虛擬主機存放網(wǎng)頁的根目錄
/www/test1/1.html
/www/test2/2.html
/www/test3/3.html
3. 在httpd.conf中將附加配置文件httpd-vhosts.conf包含進來,接著在httpd-vhosts.conf中寫入如下配置:
為了使用基于域名的虛擬主機,必須指定服務(wù)器IP地址(和可能的端口)來使主機接受請求。可以用NameVirtualHost指令來進行配置。 如果服務(wù)器上所有的IP地址都會用到,
你可以用*作為NameVirtualHost的參數(shù)。在NameVirtualHost指令中指明IP地址并不會使服務(wù)器自動偵聽那個IP地址。 這里設(shè)定的IP地址必須對應(yīng)服務(wù)器上的一個網(wǎng)絡(luò)接口。
下一步就是為你建立的每個虛擬主機設(shè)定<VirtualHost>配置塊,<VirtualHost>的參數(shù)與NameVirtualHost指令的參數(shù)是一樣的。每個<VirtualHost>定義塊中,至少都會有一個ServerName指令來指定伺服哪個主機和一個DocumentRoot指令來說明這個主機的內(nèi)容存在于文件系統(tǒng)的什么地方。
如果在現(xiàn)有的web服務(wù)器上增加虛擬主機,必須也為現(xiàn)存的主機建造一個<VirtualHost>定義塊。其中ServerName和DocumentRoot所包含的內(nèi)容應(yīng)該與全局的保持一致,且要放在配置文件的最前面,扮演默認主機的角色。
復(fù)制代碼
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>
復(fù)制代碼
4. 大功告成,測試下每個虛擬主機,分別訪問www.test1.com、www.test2.com、www.test3.com
三、基于端口
1. 修改配置文件
將原來的
Listen 80
改為
Listen 80
Listen 8080
2. 更改虛擬主機設(shè)置:
復(fù)制代碼
<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>
河南億恩科技股份有限公司(mszdt.com)始創(chuàng)于2000年,專注服務(wù)器托管租用,是國家工信部認定的綜合電信服務(wù)運營商。億恩為近五十萬的用戶提供服務(wù)器托管、服務(wù)器租用、機柜租用、云服務(wù)器、網(wǎng)站建設(shè)、網(wǎng)站托管等網(wǎng)絡(luò)基礎(chǔ)服務(wù),另有網(wǎng)總管、名片俠網(wǎng)絡(luò)推廣服務(wù),使得客戶不斷的獲得更大的收益。
服務(wù)器/云主機 24小時售后服務(wù)電話:
0371-60135900
虛擬主機/智能建站 24小時售后服務(wù)電話:
0371-55621053
網(wǎng)絡(luò)版權(quán)侵權(quán)舉報電話:
0371-60135995
服務(wù)熱線:
0371-60135900