Apache-2.2

SELinux 干擾 Apache / PHP

  • November 7, 2014

在我安裝的 CentOS 7 中,SELinux 預設是啟用的。這會阻止 Apache 正確讀取標準 /var/www/html 文件根目錄中的 PHP 文件(在顯示包含 PHP 腳本的網頁時,瀏覽器是空白的)。當我禁用 SELinux 時,頁面正常顯示。

是否有某種方法可以設置 SELinux 以允許 Apache 從文件根目錄訪問 PHP 文件?我寧願不完全禁用 SELinux,因為 CentOS 顯然認為它是一個可取的安全添加。

我做的SELinux不多,但你可以試試

semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html(/.*)?'

restorecon -R -v /var/www/html/

這允許 Apache 在該目錄中執行 PHP 腳本,並在重新啟動後持續存在。

如果您使用 MySQL,您可能必須為此做同樣的事情。SELinux:讓 Apache 在 CentOS 上與 MySQL 對話可能會有所幫助

執行audit2allow < /var/log/audit/audit.log確認 httpd 被 SELinux 阻止(請參閱此連結)。解決方案是使用以下步驟創建和應用策略模組:

  1. 以 root 身份執行命令audit2allow -a -M my_httpd(將“my_httpd”替換為您喜歡的任何名稱)。
  2. 再次以 root 身份執行命令semodule -i my_httpd.pp以安裝模組。

在我按照這些步驟操作之後,Apache 能夠毫無困難地在我的伺服器上執行 PHP 腳本。重新啟動伺服器不會破壞更改。

模組文件(my_httpd.te)的內容:

module my_httpd 1.0;
require {
   type admin_home_t;
   type httpd_t;
   class file { read getattr open };
}
#============= httpd_t ==============
allow httpd_t admin_home_t:file { read getattr open };

引用自:https://serverfault.com/questions/642353