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

14,933

Solution 1

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.

Solution 2

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

Solution 3

You may not have permissions to the parent directories?

Share:
14,933
chiliNUT
Author by

chiliNUT

Steam big picture mode sucks. Maybe if valve spent more than 5 minutes implementing it, it would work better.

Updated on June 26, 2022

Comments

  • chiliNUT
    chiliNUT almost 2 years

    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!