我不能授予apache写入文件的权限,我做错了什么


I cannot grant apache permissions to write to a file, what am I doing wrong?

我正试图向主文件夹中的一个文件授予apache权限,以便php页面可以向该文件写入日志数据。下面是我在bash shell中为实现这一目标所做的工作,我不明白为什么这不起作用:

[root@myserver logs]# mkdir apachelogs
[root@myserver logs]# touch apachelogs/log.log
[root@myserver logs]# chown -R apache:apache apachelogs
[root@myserver logs]# chown -R apache:apache apachelogs/log.log
[root@myserver logs]# chmod 770 apachelogs
[root@myserver logs]# su apache
bash-4.1$ cd apachelogs
bash: cd: apachelogs: Permission denied

因此,我刚刚授予了apache所有权、读、写、执行权限,但很明显,apache仍然无法访问该目录,当我的php脚本运行以下代码行时,这一点得到了验证:

echo exec(whoami)."'n";
file_put_contents("/home/chilinut/logs/apachelog/log.log","test",FILE_APPEND);

输出是(毫不奇怪)

apache
E_WARNING: file_put_contents(/home/chilinut/logs/apachelog/log.log): 
failed to open stream: Permission denied

我在这里错过了什么?我不想给文件夹777。我宁愿有644这样的。我正在使用CentOS release 6.4 (Final)

感谢阅读!

Dude,

这显然是文件/home/chilinut/logs/apachelog/log.log的父目录没有用户apache权限的情况。

您还必须为用户apache的父目录授予写、读权限。在您的情况下尝试以下

chown chilinut:apache /home/chilinut/
chown -R chilinut:apache /home/chilinut/*
chmod g+rw /home/chilinut/
chmod -R g+rw /home/chilinut/*

现在切换到apache用户并尝试执行它。我尝试了一个示例脚本,并且与您的脚本做了相同的操作。

enter code# cat test.sh 
echo | exec whoami ;
echo test >> /home/testleo/public_html/apachelogs/log.log;

在我这边工作得很好。

当有疑问时,请求助于宣扬良好实践的良好来源:)。在这种情况下,我将使用symfony设置说明作为指导。

$ APACHEUSER=`ps aux | grep -E '[a]pache|[h]ttpd' | grep -v root | head -1 | cut -d'  -f1`
$ sudo chmod +a "$APACHEUSER allow delete,write,append,file_inherit,directory_inherit" apachelogs/
$ sudo chmod +a "`whoami` allow delete,write,append,file_inherit,directory_inherit" apachelogs/

您可以在此处找到参考资料:http://symfony.com/doc/current/book/installation.html#configuration-并设置

是的,这些是为apache获得写入symfony的app/logs和app/cache文件夹的正确权限的说明,但同样的权限也可以应用于任何文件夹:)。

您可能没有访问父目录的权限?

。。。为了让你的echo exec(whoami)."'n";工作,做一个chmod 777 apachelogs -R,然后从那里开始。我想这是一个不同于apache的用户。。。