bash 我无法授予 apache 写入文件的权限,我做错了什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20157150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
I cannot grant apache permissions to write to a file, what am I doing wrong?
提问by chiliNUT
I am trying to grant apache permission to a file in my home folder, so that a php page might write log data to that file. Below find what I have done to accomplish this in my bash
shell, and I cannot figure out why this won't work:
我正在尝试向我的主文件夹中的文件授予 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
So I have just granted apache ownership, read, write, execute permission, yet clearly apache still does not have access to the directory, and this is verified when my php script runs this line of code:
所以我刚刚授予apache所有权,读,写,执行权限,但很明显apache仍然无法访问该目录,当我的php脚本运行这行代码时验证了这一点:
echo exec(whoami)."\n";
file_put_contents("/home/chilinut/logs/apachelog/log.log","test",FILE_APPEND);
The output is (not surprisingly)
输出是(不足为奇)
apache
E_WARNING: file_put_contents(/home/chilinut/logs/apachelog/log.log):
failed to open stream: Permission denied
What am I missing here?
I don't want to give the folder 777. I'd rather it have something like 644. I am using CentOS release 6.4 (Final)
我在这里错过了什么?我不想给文件夹 777。我宁愿它有类似 644 的东西。我正在使用CentOS release 6.4 (Final)
Thanks for reading!
谢谢阅读!
采纳答案by Leo Prince
Dude,
伙计,
This a clear case that the parent directory of the file /home/chilinut/logs/apachelog/log.logdoesn't have permission for the user apache.
这是一个明显的情况,文件/home/chilinut/logs/apachelog/log.log的父目录没有用户apache 的权限。
You have to give write, read permission for the user apachefor the parent directories also.Try the following in your case
您还必须为父目录的用户apache授予写、读权限。请在您的情况下尝试以下操作
chown chilinut:apache /home/chilinut/
chown -R chilinut:apache /home/chilinut/*
chmod g+rw /home/chilinut/
chmod -R g+rw /home/chilinut/*
Now switch to apache user and try to execute it. It will be fine. I have tried with a sample script and does the same as your script.
现在切换到 apache 用户并尝试执行它。没事的。我已经尝试过一个示例脚本,并与您的脚本执行相同的操作。
enter code# cat test.sh
echo | exec whoami ;
echo test >> /home/testleo/public_html/apachelogs/log.log;
Worked fine from my end.
从我的最后工作正常。
回答by Kirill Fuchs
When in doubt turn to good sources that preach good practices :). In this case I'll be using symfony setup instructions as a guide.
如有疑问,请转向宣扬良好做法的良好来源:)。在这种情况下,我将使用 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/
You can find the reference here: http://symfony.com/doc/current/book/installation.html#configuration-and-setup
你可以在这里找到参考:http: //symfony.com/doc/current/book/installation.html#configuration-and-setup
Yes, those are the instructions to get the right permissions for apache to write to symfony's app/logs and app/cache folders but the same can be applied to any folder :).
是的,这些是获取 apache 写入 symfony 的 app/logs 和 app/cache 文件夹的正确权限的说明,但同样可以应用于任何文件夹:)。
回答by ryrysz
You may not have permissions to the parent directories?
您可能没有访问父目录的权限?
回答by Michael D.
... to get your echo exec(whoami)."\n";
working do a chmod 777 apachelogs -R
and go from there. I'd guess that's a different user than apache...
...让你的echo exec(whoami)."\n";
工作做一个chmod 777 apachelogs -R
然后从那里开始。我猜那是与 apache 不同的用户...