'How to give apache permission to write to home directory?

My server is in /var/www/html I have a php script in /var/www/html/fileio_test/io_test.php

<?php

$logging = <<< LOG
This is a test
LOG;

$testfile = fopen('/home/djameson/test.txt', 'a'); 
fwrite ($testfile, $logging);
fclose($testfile);

?>

When I try to run this script I get

Warning: fopen(/home/djameson/test.txt): failed to open stream: Permission denied in   /var/www/html/fileio_test/io_test.php on line 7

Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/html/fileio_test/io_test.php on line 8

Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/html/fileio_test/io_test.php on line 9

How do I let apache write to my home directory? The server runs on fedora 20.



Solution 1:[1]

As your file residing in your Home directory, I would suggest one of following approaches.

  1. Give 0777 permission to file itself.

    chmod 0777 /home/djameson/test.txt
    
  2. Change Ownership to apache user www-data and give owner-write permission.

    sudo chown www-data:www-data /home/djameson/test.txt
    chmod 0744 /home/djameson/test.txt
    
  3. Add your user to www-data group or vice-verse add www-data user to your group. And then group write permission.

    sudo usermod -a -G www-data djameson
    chmod 0764 /home/djameson/test.txt
    

NOTE : I am assuming apache user name & group name is www-data & www-data respectively. You must change accordingly your server apache username/group name.

Solution 2:[2]

By default, Apache on Ubuntu runs as www-data.

Let's assume your folder is located in /var/www/mysite.

You can do this:

chown -R www-data:www-data /var/www/mysite

chmod -R og-r

/var/www/mysite After doing this, www-data (the Web server) will have full access to the site's files, while other non-root users will have no access at all.

If you wish to allow select users to access the site, you can make the folder group-readable and add those users to the group www-data.

Set correct permissions on your apache files

Solution 3:[3]

My experience with this has never been good. Anyway, this issue generally has two sides: give apache (or that user) ownership permission, and also tell selinux that you are aware of this write request. Try this, it should work:

chown -R apache /full-path-to-target-folder/
chcon -R -t httpd_sys_rw_content_t /full-path-to-target-folder/

Also, ensure that you properly target the specific folder; not the whole home directory

Solution 4:[4]

In order for this file to be readable for both the user "djameson" as well as the webserver, you need to do 4 things:

  1. Add the user apache uses (www-data on most systems) to a user group on your system. You might want to create a new one.
  2. Add the user "djameson" to that group as well.
  3. Change owner permissons (chown)of the file, so that it is owned by the user django and the group you just set up.
  4. Make sure the file permissions (chmod) allow read permission by the group.

Solution 5:[5]

if some of your files are outside of www directory and you would like to provide access to files outside of apache environment you may use chmod -R o+rx

Above would give "other users(also include apache user)" permission to read/execute files

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kuldeep.kamboj
Solution 2 T.Todua
Solution 3 Ajowi
Solution 4 Flosi
Solution 5 Utsav Popli