apache 403 forbidden怎么解決?
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> ????# ????#?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,檢查目錄配置
可以設置為Allow from all或者Require all granted來處理。
不要修改
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> ????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
網上資料說不過,這一步大多不會發生。但我的問題確實是,可能跟系統有關,具體原理還不是很懂。
6、wsgi的問題
我的虛擬主機配置為:
<virtualhost> WSGIScriptAlias?/?/srv/lxyproject/wsgi/django.wsgi Alias?/static/?/srv/lxyproject/collectedstatic/ ServerName?10.1.101.31 #ServerName?example.com #ServerAlias?www.example.com <directory> Options?Indexes??FollowSymLinks ????AllowOverride?None ????Require?all?granted </directory><directory></directory> ????Allow?from?all ErrorLog???/etc/httpd/logs/lxyproject.error.log LogLevel?warn </virtualhost>
我訪問
log報錯:
client?denied?by?server?configuration:?/srv/lxyproject/wsgi/django.wsgi
解決辦法:
修改
這個問題是因為版本的原因,
我的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> ??<ifversion> ???Order?allow,deny ???Allow?from?all ??</ifversion> ??<ifversion>=?2.3> ???Require?all?granted ??</ifversion></directory>