设为首页 收藏本站
查看: 2673|回复: 0

[经验分享] httpd之Forbidden问题解决

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2015-4-30 08:46:01 | 显示全部楼层 |阅读模式
问题由来,在安装好Apache后,不想使用默认网站根目录[DocumentRoot],便通过修改配置文件/etc/httpd/conf/httpd.conf 中的DocumentRoot来修改,然后访问之 出现Forbidden错误,便有了此文:【此次配置httpd使用的是官网的CentOS-6.6-x86_64-bin-DVD1.iso】

wKioL1VATYzQ92OSAAFEltt1K6A821.jpg
一、问题重现:
    1、创建新的目录文件:   
1
2
3
4
5
6
[iyunv@1inux /]# mkdir -pv /vhost/www/html
mkdir: created directory `/vhost'
mkdir: created directory `/vhost/www'
mkdir: created directory `/vhost/www/html'
[iyunv@1inux /]# echo "<h1> I LOVE LINUX </h1>" > /vhost/www/html/index.html
[iyunv@1inux /]#



    2、修改配置文件:
     2.1、 # vim /etc/httpd/conf/httpd.conf
1
2
将 默认的:#DocumentRoot "/var/www/html"
修改为:    DocumentRoot "/vhost/www/html"



      2.2、重启httpd服务
[iyunv@1inux conf]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
[iyunv@1inux conf]#

发现拒绝访问:
1
2
3
4
Forbidden
You don't have permission to access /
on this server.
Apache/2.2.15 (CentOS) Server at 192.168.65.80 Port 80



二、寻找解决方案

    出现上述问题后就开始了漫长的寻找解决方案的过程
    1、刚开始以为是没有定义<Directory...的缘故:于是便开始修改:

1
2
3
4
5
6
<Directory "/vhost/www/html">          //修改Directory为 自己的WEB路径
    Options Indexes FollowSymLinks    //当存在主页文件时允许以列表形式显示文件,允许连接文件
    AllowOverride None        //不允许重写
    Order allow,deny        //定义访问控制
    Allow from all        //允许所有用户访问
</Directory>



    然后重载配置文件,发现依然 Forbidden,【此时的我感觉整个人都不太好了】意识到不是Direcotry的问题,然后我又耐心的重读了一遍配置文件的配置,发现配置文件没有什么问题。
     2、然后就是苦苦思索,认为是自己对配置文件的认识有限,然后在其他已经成功更改过根目录并能访问的主机上copy了一份httpd.conf文件,使用diff 进行对比:
    【注:此时出现的另一台机器是通过Internet安装的实验环境,其安装之初便已经关闭了Selinux】

1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@1inux conf]# scp root@192.168.65.81:/etc/httpd/conf/httpd.conf ./httpd.81.com
root@192.168.65.81's password:
httpd.conf                                                                                                                 
100%   34KB  33.7KB/s   00:00   
[iyunv@1inux conf]#
< DocumentRoot "/vhost/www/html"
---
> DocumentRoot "/vhost/web/html"
318c318,319
< <Directory "/vhost/www/html">
---
> #<Directory "/var/www/html">
> <Directory "/vhost/web/html">



    发现除了根目录不一样外其他配置都一样,到这里,已经能明显感觉到不是配置文件的问题了,此时的我 只想说:Apache啊 咱们还能在一起愉快的玩耍吗?
    3、通过漫长的思考后,突然想到Apache还有日志可供分析查看..........
1
2
3
4
5
6
7
8
9
10
[iyunv@1inux conf]# tail /var/log/httpd/error_log
[Sat Mar 28 15:39:47 2015] [error] [client 192.168.65.1] (13)Permission denied: access to /index.html denied
[Sat Mar 28 15:41:42 2015] [notice] caught SIGTERM, shutting down
[Sat Mar 28 15:41:43 2015] [notice] SELinux policy enabled; httpd running as context unconfined_u:system_r:httpd_t:s0
[Sat Mar 28 15:41:43 2015] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Sat Mar 28 15:41:47 2015] [notice] Digest: generating secret for digest authentication ...
[Sat Mar 28 15:41:47 2015] [notice] Digest: done
[Sat Mar 28 15:41:47 2015] [notice] Apache/2.2.15 (Unix) DAV/2 configured -- resuming normal operations
[Sat Mar 28 15:41:47 2015] [error] [client 192.168.65.1] (13)Permission denied: access to /index.html denied
[Sat Mar 28 15:42:00 2015] [error] [client 192.168.65.1] (13)Permission denied: access to /index.html denied



    突然眼前一亮,看到了久违了SElinux 【[notice] SELinux policy enabled  】,发现它是开启状态的,心想 估计无法访问的原因 就是这个家伙在作怪吧,然后通过修改配置文件/etc/selinux/config

1
2
#SELINUX=enforcing  
===》 SELINUX=disable    //修改为关闭



    不过通过修改配置文件不能立即生效需要重启机器才能使更改后配置生效,在这个Forbidden问题是已经纠结了太长时间,已经对其失去耐心去重启了...........-_-
    其实  Selinux还有另外一种方法可以临时关闭Selinux  【但重启后失效】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[iyunv@1inux conf]# setenforce 0
[iyunv@1inux conf]# sestatus -v
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   permissive    //Selinux已经更改为permissive模式了
Mode from config file:          error (Success)
Policy version:                 24
Policy from config file:        targeted
Process contexts:
Current context:                unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
Init context:                   system_u:system_r:init_t:s0
/sbin/mingetty                  system_u:system_r:getty_t:s0
/usr/sbin/sshd                  system_u:system_r:sshd_t:s0-s0:c0.c1023
File contexts:
Controlling term:               unconfined_u:object_r:user_devpts_t:s0
/etc/passwd                     system_u:object_r:etc_t:s0
/etc/shadow                     system_u:object_r:shadow_t:s0
/bin/bash                       system_u:object_r:shell_exec_t:s0
/bin/login                      system_u:object_r:login_exec_t:s0
/bin/sh                         system_u:object_r:bin_t:s0 -> system_u:object_r:shell_exec_t:s0
/sbin/agetty                    system_u:object_r:getty_exec_t:s0
/sbin/init                      system_u:object_r:init_exec_t:s0
/sbin/mingetty                  system_u:object_r:getty_exec_t:s0
/usr/sbin/sshd                  system_u:object_r:sshd_exec_t:s0
[iyunv@1inux conf]#

如果对Selinux不太了解的童鞋 ,可以自行询问度娘..........





然后访问站点,终于看到我们刚才定义的主页文件了...........
wKiom1VAVxaTdvo1AAD2thZSCuc244.jpg

至此Forbidden的问题已经成功解决.............
======================================================
后记:
    1、在解决Forbidden的问题上走了好多弯路,在出现刚出现Forbidden的就应该考虑到查看配置文件
    2、如果仅仅只是更改根目录的话,2.1中定义<Directory>这步骤 是完全没有必要的,只要更改DirectoryRoot就能访问了........

Linux ,纵然你要虐我千百遍,我依然待你如初恋.........

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-62100-1-1.html 上篇帖子: Linux环境下Apache配置多个虚拟主机挂载多站点同时运行 下篇帖子: 使用apache建立https站点详细 Forbidden
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表