当前位置: 首页 > 图文教程 > 服务器 > Linux服务器 > Apache [forbidden 403]错误的解决办法

Linux服务器
Linux下利用Squid构建高速的ProxyServer
在Linux操作系统上配置DNS服务器的日志
搭建性能比Squid高很多的Varnish服务器
Linux系统挂载AIXNFS服务器的配置方法
Linux上架设一台高容量POP3服务器
鸟哥的Linux学习笔记DNS服务器知识
部署Linux下OpenVZ虚拟化服务器
在RHEL5服务器上远程桌面(VNC)的配置
Samba服务器多系统用户共享目录设计方法
Linux攻略Samba服务的搭建与配置
Linux操作系统下配置DNS服务器的方法介绍
用Linux系统自身做一个L2TP的VPN服务器
Linux环境用Sendmail架设Mail服务器
细述Apachewebserver的安装与配置
linux环境下轻松搭建CVS服务器
UbuntuLinux通过SSh远程管理服务器
Linux操作系统下DHCP中继代理的实现方法
Ubuntu7.10Linux上构建全功能的服务器
Linux系统搭建Discuz论坛的方法
Ubuntub7.10Linux架设Samba服务

Linux服务器 中的 Apache [forbidden 403]错误的解决办法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-30   浏览: 149 ::
收藏到网摘: n/a

今天准备试着在Apache服务器上配置用户目录(User Directory)。遇到一个棘手的问题,现将配置过程和问题及其解决总结如下:

1、常规的配置:
添加用户web:
adduser web           
 
passwd  web

在web用户目录下建立public_html目录,并将权限设置为755:
mkdir public_html
chmod 755 public_html -R

修改/etc/http/httpd.conf:
#
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received.
#
# The path to the end user account 'public_html' directory must be
# accessible to the webserver userid.  This usually means that ~userid
# must have permissions of 711, ~userid/public_html must have permissions
# of 755, and documents contained therein must be world-readable.
# Otherwise, the client will only receive a "403 Forbidden" message.
#
# See also: http://httpd.apache.org/docs/misc/FAQ.html#forbidden
#
<IfModule mod_userdir.c>
    #
    # UserDir is disabled by default since it can confirm the presence
    # of a username on the system (depending on home directory
    # permissions).
    #
    #UserDir disable
    #
    # To enable requests to /~user/ to serve the user's public_html
    # directory, remove the "UserDir disable" line above, and uncomment
    # the following line instead:
    #
    UserDir public_html
</IfModule>

2、测试、问题出现:
http://127.0.0.1/~web
================================

Forbidden
You don't have permission to access /~web on this server.

Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.


--------------------------------------------------------------------------------

Apache/2.0.54 (Fedora) Server at 127.0.0.1 Port 80
 

一般出现这个问题,直观地会想到的目录的存取权限问题,查了很久,调了很久也没有解决问题。其间曾想到是否Selinux的问题,进去看了一圈,没有发现什么要改的地方。(后来的事实证明,有时候直觉是很准的,能否找到答案,区别往往是:是否在直觉上走的更深入)。

3、问题的解决
用Google以Apache 403搜了好一会,终于在一个博客里看到,作者遇到和我完全相同的问题:Apache、目录的配置都没问题,但就是不能显示页面。而解决方法恰恰就是修改Selinux对public_html的访问控制。
用以下命令修改文件夹安全属性
chcon -R -t httpd_user_content_t public_html/ 

4、关联知识的总结:

Fedora Core 5 SELinux FAQ
http://fedora.redhat.com/docs/selinux-faq-fc5/#faq-entry-public_html Q:  How do I make a user public_html directory work under SELinux? 
 
A: This process presumes that you have enabled user public HTML directories in your Apache configuration file, /etc/httpd/conf/httpd.conf. This process only covers serving static Web content. For more information about Apache HTTP and SELinux, refer to http://fedora.redhat.com/docs/selinux-apache-fc3/. 

If you do not already have a ~/public_html directory, create it and populate it with the files and folders to be served. 

cd ~mkdir public_htmlcp /path/to/content ~/public_html

At this point, httpd is configured to serve the contents, but you still receive a 403 forbidden error. This is because httpd is not allowed to read the security type for the directory and files as they are created in the user's home directory. Change the security context of the folder and its contents recursively using the -R option: 

ls -Z -d public_html/drwxrwxr-x  auser    auser    user_u:object_r:user_home_t      public_htmlchcon -R -t httpd_user_content_t public_html/ls -Z -d public_html/drwxrwxr-x  auser    auser    user_u:object_r:httpd_user_content_t public_html/ls -Z public_html/-rw-rw-r--  auser    auser    user_u:object_r:httpd_user_content_t bar.html-rw-rw-r--  auser    auser    user_u:object_r:httpd_user_content_t baz.html-rw-rw-r--  auser    auser    user_u:object_r:httpd_user_content_t foo.html

You may notice at a later date that the user field, set here to user_u, is changed to system_u. This does not affect how the targeted policy works. The field that matters is the type field. 

Your static webpages should now be served correctly. If you continue to have errors, ensure that the Boolean which enables user home directories is enabled. You can set it using system-config-securitylevel. Select the SELinux tab, and then select the Modify SELinux Policy area. Select Allow HTTPD to read home directories. The changes take effect immediately. 

所用命令解析:

ls -Z -d public_html/
      #显示文件/目录的安全语境-Z, --context
             Display  security context so it fits on most displays.  Displays only mode, user, group, security              context and file name.-d, --directory
              list directory entries instead of contents, and do not dereference symbolic links


chcon -R -t httpd_user_content_t public_html/
       #修改文件/目录的安全语境-R, --recursive
              change files and directories recursively-t, --type
              set type TYPE in the target security context