在apache2的httpd配置中,很多情况都会出现403。

刚安装好httpd服务,当然是不会有403的问题了。主要是修改了一些配置后出现,问题描述如下:
下面一步步解决问题时注意错误日志内容。ok,开始。
如果显示更改了DocumentRoot,比如改为 "/usr/local/site/test" 。site目录和test目录是通过使用mkdir建立的,然后在test目录下放一个index.html。这种情况应该查看httpd.conf中配置。
你的<Directory "/usr/local/site/test">一定要和DocumentRoot一致,因为这段Directory是apache对该目录访问权限的设置,只有设置正确的目录,DocumentRoot才会生效。
| 
					 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  | 
						<Directory "/usr/local/site/test">     #     # Possible values for the Options directive are "None", "All",     # or any combination of:     #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews     #     # Note that "MultiViews" must be named *explicitly* --- "Options All"     # doesn't give it to you.     #     # The Options directive is both complicated and important.  Please see     # http://httpd.apache.org/docs/2.4/mod/core.html#options     # for more information.     #     Options Indexes FollowSymLinks     #     # AllowOverride controls what directives may be placed in .htaccess files.     # It can be "All", "None", or any combination of the keywords:     #   Options FileInfo AuthConfig Limit     #     AllowOverride None     #     # Controls who can get stuff from this server.     #     Require all granted </Directory>  | 
					
第一步配置正确还是出现403,检查目录配置<Directory "/usr/local/site/test">中是否有Deny from all。有则所有访问都会被拒绝,当然403了。
可以设置为Allow from all或者Require all granted来处理。
不要修改<Directory />根目录中Deny from all。
如果至此还是403,可能是网站目录的权限问题。
apache要求目录具有执行权限,也就是x,要注意的是,你的目录树都应该拥有这些权限。
假如你的目录是/usr/local/site/test,那么要保证/usr,/usr/local,/usr/local/site,/usr/local/site/test这四个层级的目录都是755权限。
| 
					 1 2  | 
						#chmod 755 /usr/local/site #chmod 755 /usr/local/site/test  | 
					
我犯过一个错就是只设置了最后一级目录权限,没有设置上级目录权限,导致403。
【这个问题我没遇到过,因为我没这样写过,网上资料这么写,可作为参考】
如果设置的是虚拟目录,那么你需要在httpd.conf中定义一个虚拟目录,而且像极了如下的片段:
| 
					 1 2 3 4 5 6 7 8 9  | 
						Alias /<strong>folder </strong>"/usr/local/<strong>folder</strong>"                        <Directory "/usr/local/<strong>folder</strong>">                              Options FollowSymLinks                                 AllowOverride None                                   Order deny,allow                                    Deny from all                                      Allow from 127.0.0.1 192.168.1.1                        </Directory>  | 
					
如果是这一种情况,而且你写得类似我上面的代码,三处folder都是一样一样的,那绝对会是403!怎么解决呢,就是把跟在Alias后面斜杠后面的那串改了,改成什么,不要和虚拟目录的文件夹同名就好,然后我就可以用改过后的虚拟目录访问了,当然,改文件夹也行,只要你不怕麻烦,只要Alias后面的虚拟目录定义字符(红色)和实际文件夹名(黑色)不相同就OK。
如果依然是403,那就是selinux在作怪了,于是,你可以把你的目录进行一下selinux权限设置。
今天我遇到的就是这个问题了。
| 
					 1 2  | 
						#chcon -R -t httpd_sys_content_t /usr/local/site #chcon -R -t httpd_sys_content_t /usr/local/site/test  | 
					
网上资料说不过,这一步大多不会发生。但我的问题确实是,可能跟系统有关,具体原理还不是很懂。
我的虚拟主机配置为:
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  | 
						<VirtualHost *:80> WSGIScriptAlias / /srv/lxyproject/wsgi/django.wsgi Alias /static/ /srv/lxyproject/collectedstatic/ ServerName 10.1.101.31 #ServerName example.com #ServerAlias www.example.com <Directory /srv/lxyproject/collectedstatic>   Options Indexes  FollowSymLinks     AllowOverride None     Require all granted </Directory> <Directory /srv/lxyproject/wsgi/>     Allow from all </Directory> ErrorLog   /etc/httpd/logs/lxyproject.error.log LogLevel warn </VirtualHost>  | 
					
我访问

log报错:
| 
					 1  | 
						client denied by server configuration: /srv/lxyproject/wsgi/django.wsgi  | 
					
解决办法:
修改<Directory /srv/lxyproject/wsgi/>中Allow from all为:Require all granted。
这个问题是因为版本的原因,
我的httpd版本为:
| 
					 1 2 3 4  | 
						[root@yl-web conf.d]# rpm -qa |grep httpd httpd-devel-2.4.6-31.el7.centos.x86_64 httpd-tools-2.4.6-31.el7.centos.x86_64 httpd-2.4.6-31.el7.centos.x86_64  | 
					
而2.3以下版本用Allow from all,2.3及以上版本为Require all granted。
| 
					 1 2 3 4 5 6 7 8 9  | 
						<Directory /home/aettool/aet/apache>   <IfVersion < 2.3 >    Order allow,deny    Allow from all   </IfVersion>   <IfVersion >= 2.3>    Require all granted   </IfVersion> </Directory>  | 
					
参考:http://stackoverflow.com/questions/17766595/403-forbidden-error-with-django-and-mod-wsgi