首先我們用記事本打開(kāi)httpd.conf文件 ,該文件在apache的目錄下,如: D:AppServApache2.2conf,修改如下兩處:
(推薦學(xué)習(xí):apache從入門(mén)到精通)
LoadModule vhost_alias_module modules/mod_vhost_alias.so //去掉前面的#,意思是啟用apache的虛擬主機(jī)功能,第203行
Include conf/extra/httpd-vhosts.conf //去掉#的意思是從httpd-vhosts.conf這個(gè)文件導(dǎo)入虛擬主機(jī)配置
配置虛擬主機(jī)后,不能用localhost 訪問(wèn),只需要把httpd.conf文件的ServerName localhost:80 那行注釋掉就可以了。
基于IP
1、假設(shè)服務(wù)器有個(gè)IP地址為192.168.1.10,使用ifconfig在同一個(gè)網(wǎng)絡(luò)接口eth0上綁定3個(gè)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文件,添加三個(gè)域名與之一一對(duì)應(yīng):
192.168.1.11 www.test1.com 192.168.1.12 www.test2.com 192.168.1.13 www.test3.com
3、建立虛擬主機(jī)存放網(wǎng)頁(yè)的根目錄,如在/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包含進(jìn)來(lái),接著在httpd-vhosts.conf中寫(xiě)入如下配置:
<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>
完成,現(xiàn)在測(cè)試下每個(gè)虛擬主機(jī),分別訪問(wèn)www.test1.com、www.test2.com、www.test3.com。