解决CentOS7.1上LNMP环境404Filenotfound.


在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 是缺省的模式。此外,它还有一个 targetedmls 的修饰语。这管制 SELinux 规则的应用有多广泛,当中 targeted 是较宽松的级别。

Enforcing: 这个缺省模式会在系统上启用并实施 SELinux 的安全性政策,拒绝访问及记录行动

Permissive: 在 Permissive 模式下,SELinux 会被启用但不会实施安全性政策,而只会发出警告及记录行动。Permissive 模式在排除 SELinux 的问题时很有用

Disabled: SELinux 已被停用

在CentOS 7.1上使用sestatus命令查看当前SELinux运行状态。

 

SELinux status:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   enforcing
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max 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:                 enabled
SELinuxfs mount:                /sys/fs/selinux
SELinux root directory:         /etc/selinux
Loaded policy name:             targeted
Current mode:                   permissive
Mode from config file:          enforcing
Policy MLS status:              enabled
Policy deny_unknown status:     allowed
Max kernel policy version:      28

再使用
sudo systemctl start php-fpm.service
启动php-fpm服务,这样LNMP服务就正常了

 

相关内容