PHP 写入权限被拒绝

PHP write permission denied

我是 运行 CentOS 7 上的服务器。 用户 apache | Apache 组对 /var/www/html 具有 775 权限 当我执行要在 root dir (/var/www/html/) 中创建新文件的 PHP 文件时,出现以下错误 Warning: file_put_contents(file.bin): failed to open stream: Permission denied in /var/www/html/file.php

PHP代码:

<?php
ini_set('display_errors', 'on');
$file = 'file.bin';
$content = "Content";
file_put_contents($file, $content);
?>

我会用更多数据更新问题,但由于我是新手,所以我对调试服务器错误了解不多。

我尝试了什么:

很有可能,问题出在 selinux。 如果您使用命令(从 root)关闭 selinux

setenforce 0

一切正常,您需要使用命令(从 root)打开 selinux

setenforce 1

和运行命令:

chcon -R -t httpd_sys_rw_content_t /your/path

一切都必须正常。如果此方法没有帮助,我建议您阅读答案 。有一个更灵活的 selinux 设置的答案(使用 sudo semanage fcontext)。