I cannot grant apache permissions to write to a fi

2019-05-11 08:24发布

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:

[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:

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)

Thanks for reading!

4条回答
劫难
2楼-- · 2019-05-11 09:16

... 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...

查看更多
放荡不羁爱自由
3楼-- · 2019-05-11 09:19

When in doubt turn to good sources that preach good practices :). In this case I'll be using symfony setup instructions as a guide.

$ 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

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 :).

查看更多
Anthone
4楼-- · 2019-05-11 09:21

You may not have permissions to the parent directories?

查看更多
贪生不怕死
5楼-- · 2019-05-11 09:22

Dude,

This a clear case that the parent directory of the file /home/chilinut/logs/apachelog/log.log doesn't have permission for the user apache.

You have to give write, read permission for the user apache for the parent directories also.Try the following in your case

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.

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

Worked fine from my end.

查看更多
登录 后发表回答