PHP虚拟主机的配置

手册/FAQ (460) 2015-11-25 14:36:10

一、找到文件httpd.conf

  找到# Virtual hosts下边的一行#Include conf/extra/httpd-vhosts.conf

去掉前边的注释符号

二、找到这个文件conf/extra/httpd-vhosts.conf在文件最后添加一段代码

#添加部分   配置虚拟主机

<VirtualHost 127.0.0.1:80>

    DocumentRoot "D:/phpProject/Web"

    DirectoryIndex index.html index.php

<Directory />

    Options FollowSymLinks

    AllowOverride None

    Order deny,allow

    Deny from all

</Directory>

</VirtualHost>

三、找到C:\WINDOWS\system32\drivers\etc\hosts文件添加一行代码

127.0.0.1       localhost

127.0.0.1       xxx.xxx.xxx

在地址栏里输入:http://xxx.xxx.xxx结果如图所示:

 

如果想让一个IP配置多个虚拟主机则:

方案一:端口区别

在和第二步一样再写一段代码如下所示:

<VirtualHost 127.0.0.1:81>

    DocumentRoot "D:/phpProject/Web"

    DirectoryIndex index.html index.php

<Directory />

    Options FollowSymLinks

    AllowOverride None

    Order deny,allow

    Deny from all

</Directory>

</VirtualHost>

只不过需要换个端口,然后修改httpd.conf文件找到#Listen

在下边添加:

Listen 80

Listen 81

让apache监听多个端口

最后修改hosts文件再加一行

127.0.0.1       localhost

127.0.0.1       xxx.xxx.xxx

127.0.0.1       xxx.xxx.xxx

方案二:SeverName区别

找到这个文件conf/extra/httpd-vhosts.conf在文件最后添加一段代码

#添加部分   配置虚拟主机 ServerName 区别方式不同的域名对应不同的servername

<VirtualHost *:80>

    DocumentRoot "D:/phpProject/Web"

    ServerName www.darren.com

    DirectoryIndex index.html index.php

<Directory />

    Options FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

</Directory>

</VirtualHost>

<VirtualHost *:80>

    DocumentRoot "D:/phpProject/Webt"

    ServerName www.hello.com

    DirectoryIndex index.html index.php

<Directory />

    Options FollowSymLinks

    AllowOverride None

    Order allow,deny

    Allow from all

</Directory>

</VirtualHost>

THE END