Home  >  Article  >  Backend Development  >  How to set php permissions in apache

How to set php permissions in apache

藏色散人
藏色散人Original
2020-09-12 10:46:044566browse

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.

How to set php permissions in apache

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(&#39;/home/djameson/test.txt&#39;, &#39;a&#39;); 
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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn