apache本地(localhost)配置 虚拟主机

*非原创。

*内容来源地址:http://hi.baidu.com/php_fly/item/5e403510f5f975fe5e53b11b

Apache配置localhost虚拟主机步骤

1,用记事本打开apache目录下httpd文件,找到如下模块

#Virtualhosts

#Includeconf/extra/httpd-vhosts.conf

去掉前面的#,这样就开启了httpd-vhosts虚拟主机文件,这时httpd.conf中的documentRoot不在起作用,需要在httpd-vhosts.conf重新配置。

2,用记事本打开httpd-vhosts文件,配置好localhost虚拟主机,参照httpd-vhosts文件中实例,修改成如下:

<VirtualHost*:80>

ServerAdminwebmaster@dummy-host.localhost

DocumentRoot"D:/zend/Apache2/htdocs/dorm"

ServerNamelocalhost

ServerAliaslocalhost

ErrorLog"logs/dummy-host.localhost-error.log"

CustomLog"logs/dummy-host.localhost-access.log"common

</VirtualHost>

修改配置如下:

DocumentRoot修改为本地apache的www目录

ServerName改为localhost

3,重启Apache,发现localhost可以正常打开,配置localhost比较简单。

实例:Apache配置dxccms.dxc.com虚拟主机步骤

1,方法同上,复制配置代码修改如下:

<VirtualHost*:80>

ServerAdmin

DocumentRootD:/zend/Apache2/htdocs/dxccms

ServerNamedxccms.dxc.com

ErrorLog"logs/dummy-host2.localhost-error.log"

CustomLog"logs/dummy-host2.localhost-access.log"common

</VirtualHost>

2,打开host文件(C:/WINDOWS/system32/drivers/etc/hosts),增加一行代码

127.0.0.1dxccms.dxc.com

3,在浏览器中打开dxccms.dxc.com,如果发现如下错误403Forbidden错误

ForbiddenYoudon'thavepermissiontoaccess/onthisserver.

分析:这主要是目录访问权限没有设置,需要设置对目录的访问权!

4,打开httpd文件,找到如下语句

<Directory/>

OptionsFollowSymLinks

AllowOverrideNone

Orderdeny,allow

Denyfromall

</Directory>

复制以上代码,并进行目录修改,把/替换为D:/zend/Apache2/htdocs/dxccms,修改virtualHost代码如下

<VirtualHost*:80>

ServerAdmin

DocumentRootD:/zend/Apache2/htdocs/dxccms

ServerNamedxccms.dxc.com

ErrorLog"logs/dummy-host2.localhost-error.log"

CustomLog"logs/dummy-host2.localhost-access.log"common

<DirectoryD:/zend/Apache2/htdocs/dxccms>

OptionsFollowSymLinks

AllowOverrideNone

Orderdeny,allow

Denyfromall

</Directory>

</VirtualHost>

在浏览器中测试如果发现还是打不开,提示如上403Forbidden错误,修改其中的Denyfromall为allowfromall

5,重启Apache,虚拟主机配置成功!

注意事项

1,目录路径,如D:/zend/Apache2/htdocs/dxccms

2,访问权限,如上Denyfromall修改为allowfromall

3,host文件,配置虚拟域名host指向

4,httpd文件,打开Includeconf/extra/httpd-vhosts.conf模块

5,httpd-vhosts文件,配置虚拟主机

相关推荐