解決CentOS 7.1上LNMP環境 404 File not found.,centoslnmp
在CentOS 7和7.1上搭建出來的LNMP運行環境,PHP網站根目錄設為/srv/www/default。
使用systemctl啟動php-fpm時,瀏覽器會報個404錯誤
File not found.
但是用root許可權手動使用php-fpm -D命令啟動卻一切正常。
仔細排查了幾遍,原來是SELinux在作怪。先看看SELinux的介紹:
安全增強式 Security-Enhanced Linux(SELinux)是一個在核心中實踐的強制存取控制(MAC)安全性機制。SELinux 首先在 CentOS 4 出現,並在後續的CentOS發行版中獲得重大改善。這些改善代表用 SELinux 解決問題的方法亦隨著時間而改變。SELinux 更能遵從最小許可權的理念。在預設的 enforcing 情況下,一切均被拒絕,接著有一系列例外的政策來允許系統的每個元素(服務、程式、使用者)運作時所需的訪問權。當一項服務、程式或使用者嘗試訪問或修改一個它不須用的檔案或資源時,它的請求會遭拒絕,而這個行動會被記錄下來。由於 SELinux 是在核心中實踐的,應用程式無須被特別編寫或重寫便可以採用 SELinux。當然,如果一個程式特別留意稍後所提及的 SELinux 錯誤碼,它的運作可能會更暢順。假若 SELinux 攔阻了一個行動,它會以一個標準的(至少是常規的)「拒絕訪問」類錯誤來彙報給該應用程式。然而,很多應用程式不會測試系統函數所返回的錯誤碼,因此它們也許不會輸出訊息解釋問題所在,或者輸出錯誤訊息。
SELinux 擁有三個基本的操作模式,當中 Enforcing 是預設的模式。此外,它還有一個 targeted 或 mls 的修飾語。這管制 SELinux 規則的應用有多廣泛,當中 targeted 是較寬鬆的層級。
Enforcing: 這個預設模式會在系統上啟用並實施 SELinux 的安全性政策,拒絕訪問及記錄行動
Permissive: 在 Permissive 模式下,SELinux 會被啟用但不會實施安全性政策,而只會發出警告及記錄行動。Permissive 模式在排除 SELinux 的問題時很有用
Disabled: SELinux 已被停用
在CentOS 7.1上使用sestatus命令查看當前SELinux運行狀態。
SELinux status: enabledSELinuxfs mount: /sys/fs/selinuxSELinux root directory: /etc/selinuxLoaded policy name: targetedCurrent mode: enforcingMode from config file: enforcingPolicy MLS status: enabledPolicy deny_unknown status: allowedMax kernel policy version: 28
可以看到當前是Enforcing模式在運行。
查看SELinux的設定檔
$ cat /etc/selinux/config# This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - No SELinux policy is loaded.SELINUX=enforcing# SELINUXTYPE= can take one of these two values:# targeted - Targeted processes are protected,# minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection.SELINUXTYPE=targeted
可以看到開機也是Enforcing模式在運行。使用setenforce命令切換到Permissive模式。
$ sudo setenforce 0$ sestatus SELinux status: enabledSELinuxfs mount: /sys/fs/selinuxSELinux root directory: /etc/selinuxLoaded policy name: targetedCurrent mode: permissiveMode from config file: enforcingPolicy MLS status: enabledPolicy deny_unknown status: allowedMax kernel policy version: 28
再使用
sudo systemctl start php-fpm.service
啟動php-fpm服務,這樣LNMP服務就正常了