httpd之Forbidden问题解决

简介:

    问题由来,在安装好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
[root@1inux /]#  mkdir  -pv /vhost/www/html
mkdir : created directory `/vhost'
mkdir : created directory `/vhost/www'
mkdir : created directory `/vhost/www/html'
[root@1inux /]#  echo  "<h1> I LOVE LINUX </h1>"  > /vhost/www/html/index.html
[root@1inux /]#

    2、修改配置文件:

     2.1、 # vim /etc/httpd/conf/httpd.conf

1
2
将 默认的:#DocumentRoot  "/var/www/html"
修改为:    DocumentRoot  "/vhost/www/html"

      2.2、重启httpd服务

[root@1inux conf]# service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]
[root@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
[root@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    
[root@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
[root@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
[root@1inux conf]# setenforce 0
[root@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
[root@1inux conf]# 
 
如果对Selinux不太了解的童鞋 ,可以自行询问度娘..........



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

wKiom1VAVxaTdvo1AAD2thZSCuc244.jpg


至此Forbidden的问题已经成功解决.............

======================================================

后记:

    1、在解决Forbidden的问题上走了好多弯路,在出现刚出现Forbidden的就应该考虑到查看配置文件

    2、如果仅仅只是更改根目录的话,2.1中定义<Directory>这步骤 是完全没有必要的,只要更改DirectoryRoot就能访问了........


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



本文转自 1inux 51CTO博客,原文链接:http://blog.51cto.com/1inux/1640243

相关文章
|
应用服务中间件 nginx
403 Forbidden nginx/1.20.1
403 Forbidden nginx/1.20.1
927 0
403 Forbidden nginx/1.20.1
|
Web App开发 应用服务中间件 PHP
|
6月前
|
应用服务中间件 nginx
【Nginx异常】[error] 4236#29900: OpenEvent(“Global\ngx_reload_27128“) failed (5: Access is denied)
【Nginx异常】[error] 4236#29900: OpenEvent(“Global\ngx_reload_27128“) failed (5: Access is denied)
135 0
|
应用服务中间件 PHP nginx
Nginx auth_basic 403 Forbidden
Nginx auth_basic 403 Forbidden
125 0
|
前端开发 应用服务中间件 nginx
(最简单详细)关于nginx配置解决页面刷新以后404 Not Found和403 Forbidden
(最简单详细)关于nginx配置解决页面刷新以后404 Not Found和403 Forbidden
1726 0
(最简单详细)关于nginx配置解决页面刷新以后404 Not Found和403 Forbidden
|
应用服务中间件 nginx 开发工具
Nginx 13: Permission denied 解决方案
本文首发于我的个人博客:尾尾部落 今天在用uwsgi+nginx在部署flask应用时,遇到502的错误,vim /var/log/nginx/error.
3088 0
|
应用服务中间件 nginx
Failed:(13: Permission denied)导致访问浏览器出现Nginx 500 Internal Server Error
Failed:(13: Permission denied)导致访问浏览器出现Nginx 500 Internal Server Error
606 1
Failed:(13: Permission denied)导致访问浏览器出现Nginx 500 Internal Server Error
|
应用服务中间件 nginx Perl
解决Nginx出现403 forbidden (13: Permission denied)报错的四种方法
解决Nginx出现403 forbidden (13: Permission denied)报错的四种方法 我是在在本地用虚拟机中通过yum安装nginx的,安装一切正常,但是访问时报403, 于是查看nginx日志,路径为/var/log/nginx/error.log。
4084 0