问题由来,在安装好Apache后,不想使用默认网站根目录[DocumentRoot],便通过修改配置文件/etc/httpd/conf/httpd.conf 中的DocumentRoot来修改,然后访问之 出现Forbidden错误,便有了此文:
【此次配置httpd使用的是官网的CentOS-6.6-x86_64-bin-DVD1.iso】
一、问题重现:
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不太了解的童鞋 ,可以自行询问度娘..........
|
然后访问站点,终于看到我们刚才定义的主页文件了...........
至此Forbidden的问题已经成功解决.............
======================================================
后记:
1、在解决Forbidden的问题上走了好多弯路,在出现刚出现Forbidden的就应该考虑到查看配置文件
2、如果仅仅只是更改根目录的话,2.1中定义<Directory>这步骤 是完全没有必要的,只要更改DirectoryRoot就能访问了........
Linux ,纵然你要虐我千百遍,我依然待你如初恋.........
本文转自 1inux 51CTO博客,原文链接:http://blog.51cto.com/1inux/1640243