php 配置虚拟主机

1.找到Apache的\conf目录下的httpd.conf文件,打开它,找到下面这两行:
# Virtual hosts
#Include conf/extra/httpd-vhosts.conf


把它改成下面这样:
# Virtual hosts
Include conf/extra/httpd-vhosts.conf



意思是启用虚拟主机的设置文件httpd-vhosts.conf,虚拟主机的设置都在这个文件下

设置,不用去改httpd.conf,这样比较清晰。


2.找到Apache的\conf\extra目录下的httpd-vhost.conf这个文件。
打开它的时候里面已经含有像下面这样的内容:
–虚拟主机配置开始

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.localhost
DocumentRoot “C:/Program Files/Apache Software Foundation/Apache2.2/docs/dummy-host.localhost”
ServerName dummy-host.localhost
ServerAlias www.dummy-host.localhost
ErrorLog “logs/dummy-host.localhost-error.log”
CustomLog “logs/dummy-host.localhost-access.log” common
</VirtualHost>


COPY上面这几行,关键是:把
DocumentRoot “”–虚拟主机的目录,即域名对应的根目录
ServerName dummy-host.localhost–你想要的域名。
还要在每个虚拟主机配置开始前加入以下内容:
<Directory “c:\test”>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>

其中:
<Directory “c:\test”>  –这是你的虚拟主机的被授权的目录
一般与
DocumentRoot “”–虚拟主机的目录,即域名对应的根目录
相同。
Allow from all –这句是重点,只要allow别人才行访问上面定义的目录,
否则在IE或firefox访问时会显示503错误,即禁止访问。

如果你不想在httpd-vhost.conf 里加入
<Directory “c:\test”>
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
那么你也可以这样做:
在httpd.conf里找到以下内容:
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
</Directory>

这是Apache默认的设置,最后一行是这样的
Deny from all
意思是拒绝所有访问对根目录的访问,别人是访问不了你的网站的,
所以要改成
Allow from all

不过还是建议用第一种方法,即修改httpd-vhost.conf的内容,这样比较清楚,也比较安全。
这样apache的设置就完成了,不过还要做下面这一步。

3.还有关键的一步:
这一步不做,一定不行因为你本地的机器没有DNS解析。
找到C:\WINDOWS\system32\drivers\etc目录下的hosts文件
加上这样的记录
127.0.0.1 demoa–这个是你的域名,你要设置多少个虚拟主机,就要加多少行这样的记录。

OK,现在你可以设置N+1个域名了。
也可以参考一下这个地址:http://httpd.apache.org/docs/2.0/vhosts/examples.html


样例:

--------------------------------------------------------------------------

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host.localhost
DocumentRoot /www/docs/dummy-host.localhost
ServerName dummy-host.localhost
ServerAlias www.dummy-host.localhost
ErrorLog logs/dummy-host.localhost-error_log
CustomLog logs/dummy-host.localhost-access_log common
</VirtualHost>

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.localhost
DocumentRoot /www/docs/dummy-host2.localhost
ServerName dummy-host2.localhost
ErrorLog logs/dummy-host2.localhost-error_log
CustomLog logs/dummy-host2.localhost-access_log common
</VirtualHost>


<Directory "D:\phpwork">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.localhost
DocumentRoot D:\phpwork
ServerName localhost
ErrorLog logs/dummy-host2.localhost-error_log
CustomLog logs/dummy-host2.localhost-access_log common
</VirtualHost>

<Directory "D:\phpwork\phpMyAdmin">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.localhost
DocumentRoot D:\phpwork\phpMyAdmin
ServerName phpMyAdmin.local
ErrorLog logs/dummy-host2.localhost-error_log
CustomLog logs/dummy-host2.localhost-access_log common
</VirtualHost>


<Directory "D:\phpwork\test">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.localhost
DocumentRoot D:\phpwork\test
ServerName test.local
ErrorLog logs/dummy-host2.localhost-error_log
CustomLog logs/dummy-host2.localhost-access_log common

</VirtualHost>


原文转载地址:http://hi.baidu.com/xiyi2008/item/ebfc92001cd6cc10cd34ea64

posted @ 2013-01-28 20:52  梦你所梦  阅读(319)  评论(0编辑  收藏  举报