php - 权限被拒绝写入一个目录,但不写入另一个目录——两者具有相同的所有者/组/755

标签 php apache centos

这让我发疯。 httpd 作为用户 apache 运行。我在/var/www/html 中有两个目录——上传和照片。两者都有 group:owner of apache:apache。两者都是 755。上传可以从 php 写入——照片不是。

部分测试代码:

var_dump(touch('/var/www/html/photos/_test.log'));
var_dump(touch('/var/www/html/uploads/_test.log'));
var_dump(touch('/var/www/html/uploadsasdf/_test.log'));

和结果:

Warning: touch(): Unable to create file /var/www/html/photos/_test.log because Permission denied in /var/www/html/test.php on line 2
bool(false) 
bool(true) 
Warning: touch(): Unable to create file /var/www/html/uploadsasdf/_test.log because Permission denied in /var/www/html/test.php on line 4
bool(false)

我已经通过 shell 和 GUI 界面确认了权限。为了确定,我再次对所有内容进行了 chown 和 chmoded。我已将 uploads 目录重命名为其他名称,并将 photos 重命名为 uploads 以查看目录名称是否是此处的关键,但事实并非如此。它是目录本身。重命名的上传仍然可以使用新名称,而现在称为“上传”的照片目录则不能。

请注意,_test.log 在测试前不存在于文件夹中,因此这并不是说该文件具有错误的权限或其他任何内容。

有趣的是,如果我创建一个新目录,将它chown 到apache:apache,将它chmod 到777,我不能写入它,所以这里可能有更大的错误;但问题仍然存在:为什么上传目录有效?

有没有人见过这种行为?我是否遗漏了一些明显的东西?

在此先感谢您的帮助!

编辑以添加更多信息:

exec('whoami') 

“ Apache ”

var_dump(posix_getpwuid(fileowner('/var/www/html/')));
var_dump(posix_getpwuid(fileowner('/var/www/html/uploads/')));
var_dump(posix_getpwuid(fileowner('/var/www/html/photos/')));

所有“ Apache ”

所有文件都具有相同的 fileperms() 值。 但是,is_writable() 在除“上传”之外的所有方面都是错误的。

mkdir('var/www/html/test');

警告:mkdir():权限被拒绝

ls-alF

drwxr-xr-x.  2 apache apache 286720 Nov 22 15:17 photos/
drwxr-xr-x.  2 apache apache  81920 Nov 22 12:06 uploads/
drwxr-xr-x.  2 apache apache      6 Nov 22 10:31 uploadsasdf/

我调用了 clearstatcache();我已经重新启动了服务器。什么......地球上......?

最佳答案

由于您使用的是 CentOS 并且您已经尝试了其他所有方法,所以我猜测可能与 SELinux 相关。此问题的答案之一可能会有所帮助 Forbidden You don't have permission to access on this server. Centos 6 / Laravel 4

具体尝试这个来分析SELinux权限(ls -lZ)并暂时禁用SELinux:

If you're using CentOS it could be an issue with selinux. Check to see if selinux is enabled with 'sestatus'. If it is enabled, you can check to see if that is the issue (temporarily) using 'sudo setenforce 0'. If apache can serve the site, then you just need to change the context of the files recursively using 'sudo chcon -R -t httpd_sys_content_t' (you can check the existing context using 'ls -Z'.

关于php - 权限被拒绝写入一个目录,但不写入另一个目录——两者具有相同的所有者/组/755,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27081132/

相关文章:

javascript - PHP 文件的 jQuery AJAX url 不起作用

php - 不允许使用 jquery 方法

Apache 监听同一 SSL 虚拟主机的 2 个端口

php - header ("Location:/");重定向适用于本地主机,但不适用于远程服务器

ssh - 用于排除目录和文件的 Zip 命令

php - 在不丢失数据库用户的情况下更改哈希

php - 如何使用 file_get_contents 仅显示 <body></body> 内的内容

ruby-on-rails - 在 Rails(本地)开发环境中并排运行 HTTP 和 HTTPS

linux - Kubernetes kuberadm 不使用代理

centos - 坚持配置 Varnish