apache开启虚拟主机VHOST后 localhost无法访问,出现Forbidden 403

本文介绍如何在Apache环境下配置localhost及自定义域名虚拟主机,包括修改httpd-vhosts.conf文件、设置目录访问权限、编辑hosts文件等步骤。
今天在集成环境下配虚拟主机,没想到虚拟主机开启后,localhost竟然无法访问了,解决办法是这样的:
实例一,Apache 配置localhost虚拟主机步骤
1,用记事本打开apache目录下httpd文件(如:D:\wamp\bin\apache\apache2.2.8\conf),找到如下模块

    # Virtual hosts
    #Includeconf/extra/httpd-vhosts.conf

去掉前面的#,这样就开启了httpd-vhosts虚拟主机文件。这时候重启wamp环境,无法打开localhost,需要在httpd-vhosts.conf配置一下。

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

   
    ServerAdminwebmaster@dummy-host.localhost
    DocumentRoot"D:\wamp\www"
    ServerNamelocalhost
    ServerAliaslocalhost
    ErrorLog"logs/dummy-host.localhost-error.log"
    CustomLog"logs/dummy-host.localhost-access.log" common
   

修改配置如下:
DocumentRoot 修改为本地wamp环境下的www目录(如:D:\wamp\www)
ServerName改为localhost
3,重启Apache,发现localhost可以正常打开,配置localhost比较简 单。

实例二,Apache配置 test.biuuu.com虚拟主机步骤

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

   
    ServerAdmintest@biuuu.com
    DocumentRootE:\WebRoot\biuuu
    ServerNametest.biuuu.com
    ErrorLog"logs/dummy-host2.localhost-error.log"
    CustomLog"logs/dummy-host2.localhost-access.log" common
   

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

    127.0.0.1     test.biuuu.com

3,在浏览器中打开test.biuuu.com,发现如下错误403 Forbidden错误
Forbidden
You don't have permission to access / on this server.

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

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

   
    OptionsFollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
   

复制以上代码,并进行目录修改,把/替换为E:\WebRoot\biuuu,修改virtualHost代码如下

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

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

注意事项
1,目录路径,如E:\WebRoot\biuuu
2,访问权限,如上Deny from all修改为allow from all
3,host文件,配置虚拟域名host指向
4,httpd文件,打开Include conf/extra/httpd-vhosts.conf模块
5,httpd-vhosts文件,配置虚拟主机

使用 Apache配置httpd-vhosts虚拟主机对于开发人员来说比较简单,但却非常重要,仅供参考!



PS: D:\wamp\alias 也可以这样配置虚拟域名,和例二相同效果

 ServerName blog.cc
 ServerAlias blog.cc
 DocumentRoot "D:\wamp\www\blog"
 Options All FollowSymLinks IncludesNOEXECIndexes
 DirectoryIndex index.html index.htmdefault.htm index.php default.php index.cgi default.cgi index.shtmlindex.aspx default.aspx  
 AllowOverride All
 Order Deny,Allow
 Allow from all
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值