我目前正在将我的 LAMP 从我的 Windows Server 迁移到运行 Debian 6 的 VPS.大多数一切正常,但是,其中一个 PHP 脚本未能写入其配置的日志文件.我不知道为什么,所以我写了一个新的、简单的、人为的 PHP 脚本来测试这个问题.
I'm currently migrating my LAMP from my Windows Server to a VPS running Debian 6. Most everything is working, however, one of the PHP scripts was failing to write to its configured log file. I could not determine why, so I wrote a new, simple, contrived PHP script to test the problem.
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
echo exec('whoami');
$log = fopen('/var/log/apache2/writetest/writetest.log', 'a');
if ($log != NULL)
{
fflush($log);
fclose($log);
$log = NULL;
}
?>
然而,它失败了:
www-data Warning: fopen(/var/log/apache2/writetest/writetest.log): failed to open stream: Permission denied in /var/www/_admin/phpwritetest.php on line 5
/var/log/apache2/writetest/writetest.log
设置为 chmod 777.www-data:www-data
所有.touch
创建的.
/var/log/apache2/writetest/writetest.log
to chmod 777. www-data:www-data
. touch
.我运行 strace
来验证哪个进程正在执行打开:
I ran strace
to verify which process was performing the open:
[pid 21931] lstat("/var/log/apache2/writetest/writetest.log", 0x7fff81677d30) = -1 EACCES (Permission denied)
[pid 21931] lstat("/var/log/apache2/writetest", 0x7fff81677b90) = -1 EACCES (Permission denied)
[pid 21931] open("/var/log/apache2/writetest/writetest.log", O_RDWR|O_CREAT|O_TRUNC, 0666) = -1 EACCES (Permission denied)
我检查了一下,pid 21931 确实是 www-data
下运行的 apache2 子进程之一.如您所见,我还在脚本中包含了 echo exec('whoami');
,以确认脚本正在由 www-data
运行.
I checked and pid 21931 was indeed one of the apache2 child processes running under www-data
. As you can see, I also included echo exec('whoami');
in the script which confirmed the script was being run by www-data
.
其他注意事项:
open_basedir
未设置Apache/2.2.16 (Debian) PHP/5.3.3-7+squeeze3 with Suhosin-Patch mod_ssl/2.2.16 OpenSSL/0.9.8o
2.6.32-238.19.1.el5.028stab092.2 #1 SMP Thu Jul 21 19:23:22 MSD 2011 x86_64 GNU/Linux
-rwxrwxrwx 1 www-data www-data 0 Sep 8 18:13 writetest.log
drwxr-xr-x 2 www-data www-data 4096 Sep 8 18:13 writetest
root
下,子进程运行在www-data
open_basedir
is not setApache/2.2.16 (Debian) PHP/5.3.3-7+squeeze3 with Suhosin-Patch mod_ssl/2.2.16 OpenSSL/0.9.8o
2.6.32-238.19.1.el5.028stab092.2 #1 SMP Thu Jul 21 19:23:22 MSD 2011 x86_64 GNU/Linux
-rwxrwxrwx 1 www-data www-data 0 Sep 8 18:13 writetest.log
drwxr-xr-x 2 www-data www-data 4096 Sep 8 18:13 writetest
root
, and the child processes under www-data
请记住,为了访问文件,所有父目录必须可以被 www-data 读取.您的 strace 输出似乎表明即使 accessing /var/log/apache2/writetest
也失败了.确保 www-data 对以下目录具有权限:
Remember that in order to reach a file, ALL parent directories must be readable by www-data. You strace output seems to indicate that even accessing /var/log/apache2/writetest
is failing. Make sure that www-data has permissions on the following directories:
/(r-x)
/var (r-x)
/var/log (r-x)
/var/log/apache2 (r-x)
/var/log/apache2/writetest (rwx)
/var/log/apache2/writetest/writetest.log (rw-)
这篇关于即使具有完全打开的权限,PHP fopen() 也会对文件失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!