[頂]解決SELinux對網站目錄權限控制的不當的問題 20

前言:本文主要介紹了因爲SELinux對網站目錄權限控制的不當而引起網站無法正常操作和訪問的問題。

正文開始:今天下午閒着沒有事做於是突然興起想嘗試安裝下Drupal。以前用Wordpress做博客久了,總想着嚐嚐新。

按照Installtion Guide提示的安裝步驟進行操作如下:

  1. wget http://drupal.org/files/projects/drupal-7.12.tar.gz 
  2. tar -zxvf drupal-7.12.tar.gz 
  3. mv drupal-7.12 /var/www/html/home_start
  4. cd /var/www/html/home_start
  5. cp sites/default/default.settings.php sites/default/settings.php 
  6. chmod a+w sites/default/settings.php 
  7. chmod a+w sites/default 
  8. mysqladmin -u username -p create databasename #用正確的字符串代替username和databasename

一切準備就緒!就等着進網站目錄進行下一步配置了!

不幸的是,在瀏覽器中打開網站的目錄後就遭遇了httpd的403拒絕訪問提示。

這很顯然,根據以往的經驗可以判斷是目錄權限存在問題。

爲了驗證這一說法,我們可以檢查httpd的錯誤日誌。默認情況下日誌就存在在/var/log/httpd/目錄中。

  1. [root@localhost ~]# grep Permission /var/log/httpd/error_log 
  2. [Tue Apr 10 09:07:04 2012] [error] [client 127.0.0.1] (13)Permission denied: access to /start denied 
  3. [Tue Apr 10 09:07:50 2012] [error] [client 127.0.0.1] (13)Permission denied: access to /start/ denied 
  4. [Tue Apr 10 09:08:07 2012] [error] [client 127.0.0.1] (13)Permission denied: access to /start/ denied 
  5. [Tue Apr 10 09:10:06 2012] [error] [client 127.0.0.1] (13)Permission denied: access to /start/ denied 
  6. [Tue Apr 10 09:11:08 2012] [error] [client 127.0.0.1] (13)Permission denied: access to /start/ denied 
  7. [Tue Apr 10 09:11:17 2012] [error] [client 127.0.0.1] (13)Permission denied: access to /start denied 
  8. [Tue Apr 10 09:11:34 2012] [error] [client ::1] (13)Permission denied: access to /start denied 
  9. [Tue Apr 10 09:13:35 2012] [error] [client ::1] (13)Permission denied: access to /start denied 
  10. [Tue Apr 10 09:13:51 2012] [error] [client ::1] (13)Permission denied: access to /start/site/default/ denied 
  11. [Tue Apr 10 09:13:57 2012] [error] [client ::1] (13)Permission denied: access to /start/sites denied 
  12. [Tue Apr 10 09:14:51 2012] [error] [client ::1] (13)Permission denied: access to /start/install.php denied 
  13. [Tue Apr 10 09:18:57 2012] [error] [client ::1] (13)Permission denied: access to /start/install.php denied 
  14. [root@localhost ~]#  

再檢查網站目錄和文件的權限。爲方便起見直接用-lZ選項。用於顯示詳細信息和SELinux權限信息

  1. [root@localhost html]# ls -lZ 
  2. -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 archive.html 
  3. drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 blog 
  4. drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 blog_backup 
  5. -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 blog.htm 
  6. -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 blog.html 
  7. drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 css 
  8. drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 home_page 
  9. drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 home_start #問題行
  10. drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 images 
  11. -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.htm 
  12. -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 index.html 
  13. -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 info_php.php 
  14. drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 js 
  15. -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 log 
  16. drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 php #以前的遺留問題
  17. drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 phpMyAdmin-3.4.10.1-all-languages 
  18. drwxr-xr-x. root root unconfined_u:object_r:httpd_sys_content_t:s0 PSDs 

顯然上面顯示的結果中的09、16兩行的權限與其他網站目錄不同。

再查看selinx的工作狀態,判斷是不是SELinux引起的。

  1. [root@localhost httpd]# sestatus  
  2. SELinux status:                 enabled 
  3. SELinuxfs mount:                /selinux 
  4. Current mode:                   enforcing 
  5. Mode from config file:          enforcing 
  6. Policy version:                 24 
  7. Policy from config file:        targeted 

這就是導致網站權限不正確的原因。

我猜測可能是在selinux啓用時對目錄或文件進行操作導致的。因爲最近我對SELinux進行了升級(以前沒有遇到)。

PS:最新的原因分析請參見《對SELinux權限發生變化的解釋》一文。

所以使用chcon更改SELinux權限以及顯示結果如下:

setenforce 0 #必須暫時停止SELinux,否則可能導致操作失敗。 
chcon -t httpd_sys_content_t -R /var/www/html/home_start/ #R參數是遞歸操作的意思



經過修改就會發現SELinux的對應權限已經和其他目錄相同了!都是
httpd_sys_content_t。

  1. [root@localhost html]# setenforce --help 
  2. usage:  setenforce [ Enforcing | Permissive | 1 | 0 ] 
  3. [root@localhost html]# setenforce 0 
  4. [root@localhost html]# cd 
  5. [root@localhost ~]# ls /var/www/html/ -Z 
  6. ……
  7. drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 home_start 
  8. ……
  9. [root@localhost ~]# chcon -t httpd_sys_content_t -R /var/www/html/home_start/ 
  10. [root@localhost ~]# ls /var/www/html/home_start/ -Z 
  11. ……
  12. -rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 authorize.php 
  13. ……

然後,再次打開瀏覽器輸入地址,驗證能否訪問,如果可以訪問就可以進行下一步配置了!

最後,還是那句話,遇到問題一定要仔細分析,儘可能的通過自己的努力和分析發現問題所在,這樣才能體會到解決問題的快樂!

本文出自 “通信,我的最愛” 博客,請務必保留此出處http://dgd2010.blog.51cto.com/1539422/831098

發佈了120 篇原創文章 · 獲贊 9 · 訪問量 57萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章