虚拟主机的配置在apache安装目录下/conf/extra/httpd-vhosts.conf文件中,需要在/conf/httpd.conf中开启。
LoadModule vhost_alias_module modules/mod_vhost_alias.so
Include conf/extra/httpd-vhosts.conf
1、基于IP地址的虚拟主机 (*服务器有多个IP地址)
<VirtualHost 192.168.1.1>
DocumentRoot D:/wwwroot/site1
ServerName www.site1.com
<Directory "D:/wwwroot/site1">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow From All
</Directory>
</VirtualHost>
<VirtualHost 192.168.1.2>
DocumentRoot D:/wwwroot/site2
ServerName www.site2.com
<Directory "D:/wwwroot/site2">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow From All
</Directory>
</VirtualHost>
2、基于IP地址和多端口 (*分别绑定8080和8088端口)
Listen 127.0.0.1:8080
<VirtualHost 127.0.0.1:8080>
DocumentRoot D:/wwwroot/site1
ServerName www.site1.com
</VirtualHost>
Listen 127.0.0.1:8088
<VirtualHost 127.0.0.1:8088>
DocumentRoot D:/wwwroot/site2
ServerName www.site2.com
</VirtualHost>
3、单IP地址基于域名的虚拟主机
Listen 80
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot D:/wwwroot/site1
ServerName www.site1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot D:/wwwroot/site2
ServerName www.site2.com
</VirtualHost>
4、多IP地址基于域名的虚拟主机
<VirtualHost 192.168.1.1>
DocumentRoot D:/wwwroot/site1
ServerName www.site1.com
</VirtualHost>
<VirtualHost 192.168.1.2>
DocumentRoot D:/wwwroot/site2
ServerName www.site2.com
</VirtualHost>
5、多端口配置基于域名的虚拟主机
Listen 80
<VirtualHost *:80>
DocumentRoot D:/wwwroot/site1
ServerName www.site1.com
</VirtualHost>
Listen 8080
<VirtualHost *:8080>
DocumentRoot D:/wwwroot/site2
ServerName www.site2.com
</VirtualHost>
6、基于域名和基于IP 的混合虚拟主机
Listen 80
NameVirtualHost 192.168.1.1
<VirtualHost 192.168.1.1:80>
DocumentRoot D:/wwwroot/site1
ServerName www.site1.com
</VirtualHost>
Listen 8080
<VirtualHost 192.168.1.1:8080>
DocumentRoot D:/wwwroot/site2
ServerName www.site2.com
</VirtualHost>