Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...