一,修改httpd.conf
找到Apache安装目录下conf\httpd.conf文件,分别去掉下面两行文字前面的#号如下:
启用apache的虚拟主机功能
LoadModule vhost_alias_module modules/mod_vhost_alias.so
使httpd-vhosts.conf文件起作用
Include conf/extra/httpd-vhosts.conf
若多端口的话,需要增加Listen如:
Listen 80
Listen 8888
二,修改httpd-vhosts.conf
找到Apache安装目录下conf\extra\httpd-vhosts.conf文件
NameVirutalHost *:80 指定某个IP的某个端口是哪个主机,保持默认即可,新版的Apache已经去掉了这一项
#域名形式
<VirtualHost *:80>
DocumentRoot "E:/WebSite/yuanshili"
ServerName www.demo.com
<Directory "E:/WebSite/yuanshili">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
#IP形式
<VirtualHost *:80>
DocumentRoot "E:/WebSite/weidongli"
ServerName 127.0.0.2
<Directory "E:/WebSite/weidongli">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
#端口形式
<VirtualHost *:8888>
DocumentRoot "E:/WebSite/test"
ServerName 127.0.0.2
<Directory "E:/WebSite/test">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
注意:若没有Directory下的Allow from all则会出现无权限访问错误如:
此错误(HTTP 403 禁止)意味着 Internet Explorer 可以连接到该网站,但是没有查看该网页的权限
三,修改C:\Windows\System32\drivers\etc下的hosts文件
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
127.0.0.1 www.demo.com
可在浏览器测试如下:
http://www.demo.com
http://127.0.0.2
http://127.0.0.2:888