今天下午,开始在本机使用不同端口号.可以实现一个ip挂多个网站。如下
1、多个端口挂网站
NameVirtualHost 127.0.0.1:8080
<VirtualHost 127.0.0.1:8080>
ServerName www.test.com
DocumentRoot D:/www2
</VirtualHost>
<Directory "D:/www2">
Options Indexes FollowSymLinks Multiviews
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
但是这样对已经使用ip地址绑定的同一端口的多个域名就不能看到多个网站了。而现在又没有可以选定端口的绑定方法。
2、多个ip挂网站
NameVirtualHost 127.0.0.2
<VirtualHost 127.0.0.2>
ServerName www.test.com
DocumentRoot D:/www2
</VirtualHost>
<Directory "D:/www2">
Options Indexes FollowSymLinks Multiviews
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
这种方法适用于机子有多个ip的,但一般不常见。在本机上试验,127段的均指向本机所以可以这样试验。需要修改hosts文件(C:/WINDOWS/system32/drivers/etc/hosts)。
hosts文件最后需要这样修改:
127.0.0.1 localhost
127.0.0.2 www.test.com
127.0.0.1 www.uu.cn
127.0.0.1 wwww.kssme.com
3、适用同一ip+端口挂不同网站,这个常用
NameVirtualHost 127.0.0.1:80 ----------这一句在每一个网站虚拟目录前都要加上,不然后面几个站都访问第一个站了。
<VirtualHost 127.0.0.1:80>
ServerName www.test.com
DocumentRoot D:/www2
</VirtualHost>
<Directory "D:/www2"> ---------第一种方案,程序目录配置与虚拟主机分开写
Options Indexes FollowSymLinks Multiviews
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
ServerName www.uu.cn
DocumentRoot D:/www3
<Directory "D:/www3"> ---------------第二种方案,虚拟目录里面写程序目录配置
Options Indexes FollowSymLinks Multiviews
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
ServerName www.kssme.com
DocumentRoot D:/www4
<Directory "D:/www4">
Options Indexes FollowSymLinks Multiviews
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
</VirtualHost>
这样就可以访问上面几个不同的网站了,原理就是不同的域名对应不同的程序目录。