How to set php permissions in apache: 1. Grant permissions to the 0777 file itself; 2. Change the ownership to the Apache user "www-data" and grant the owner write permissions; 3. Add the user to " www-data" group, and then group write permissions.
Recommended: "PHP Video Tutorial"
Specific questions:
How to give Apache permission to write to the 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 got
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 get Apache to write to my home directory? The server is running on Fedora 20.
Solution:
Since your files are located in your home directory, I recommend using one of the following methods.
Grant 0777 permissions to the file itself.
chmod 0777 /home/djameson/test.txt
Change ownership to Apache user www-data and grant write permission to the owner.
Sudo chown www-data:www-data /home/djameson/test.txt chmod 0744 /home/djameson/test.txt
Add your user to www-data group or vice versa add www-data user to your group. Then group write permissions.
Sudo usermod -a -G www-data djameson chmod 0764 /home/djameson/test.txt
Note: I assume the Apache username and group name are www-data and www-data respectively. You have to change the server Apache username/group name accordingly.
The above is the detailed content of How to set php permissions in apache. For more information, please follow other related articles on the PHP Chinese website!