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

apache, bash, centos, permissions, php

Solution

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.

Problem

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!

Original source