apache httpd服務器403 forbidden的問題
一、問題描述
在apache2的httpd配置中,很多情況都會出現403。
剛安裝好httpd服務,當然是不會有403的問題了。主要是修改了一些配置後出現,問題描述如下:
- 修改了DocumentRoot目錄指向後,站點出現403錯誤。
- 設置了虛擬主機目錄也可能導致403。
- apache的httpd服務成功啓動,看起來都很正常,卻沒有權限訪問
- 日誌出現: access to / denied (filesystem path '/srv/lxyproject/wsgi/django.wsgi') because search permissions are missing on a component of the path
- 設置虛擬目錄後,錯誤日誌出現:client denied by server configuration: /srv/lxyproject/wsgi/django.wsgi
二、分析問題及方案
下面一步步解決問題時注意錯誤日誌內容。ok,開始。
1、httpd.conf中目錄配置文件
如果顯示更改了DocumentRoot,比如改爲 "/usr/local/site/test" 。site目錄和test目錄是通過使用mkdir建立的,然後在test目錄下放一個index.html。這種情況應該查看httpd.conf中配置。
你的<Directory "/usr/local/site/test">一定要和DocumentRoot一致,因爲這段Directory是apache對該目錄訪問權限的設置,只有設置正確的目錄,DocumentRoot纔會生效。
<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>
2、目錄訪問權限
第一步配置正確還是出現403,檢查目錄配置<Directory "/usr/local/site/test">中是否有Deny from all。有則所有訪問都會被拒絕,當然403了。
可以設置爲Allow from all或者Require all granted來處理。
不要修改<Directory />根目錄中Deny from all。
3、目錄權限
如果至此還是403,可能是網站目錄的權限問題。
apache要求目錄具有執行權限,也就是x,要注意的是,你的目錄樹都應該擁有這些權限。
假如你的目錄是/usr/local/site/test,那麼要保證/usr,/usr/local,/usr/local/site,/usr/local/site/test這四個層級的目錄都是755權限。
#chmod 755 /usr/local/site
#chmod 755 /usr/local/site/test
我犯過一個錯就是隻設置了最後一級目錄權限,沒有設置上級目錄權限,導致403。
4、 虛擬目錄
【這個問題我沒遇到過,因爲我沒這樣寫過,網上資料這麼寫,可作爲參考】
如果設置的是虛擬目錄,那麼你需要在httpd.conf中定義一個虛擬目錄,而且像極了如下的片段:
Alias /folder "/usr/local/folder"
<Directory "/usr/local/folder">
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。
5、selinux的問題
如果依然是403,那就是selinux在作怪了,於是,你可以把你的目錄進行一下selinux權限設置。
今天我遇到的就是這個問題了。
#chcon -R -t httpd_sys_content_t /usr/local/site
#chcon -R -t httpd_sys_content_t /usr/local/site/test
網上資料說不過,這一步大多不會發生。但我的問題確實是,可能跟系統有關,或者直接停掉selinux
6、wsgi的問題
我的虛擬主機配置爲:
<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報錯:
client denied by server configuration: /srv/lxyproject/wsgi/django.wsgi
解決辦法:
修改<Directory /srv/lxyproject/wsgi/>中Allow from all
爲:Require all granted
。
這個問題是因爲版本的原因,
我的httpd版本爲:
[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。
<Directory /home/aettool/aet/apache>
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
</Directory>