Home > Net >  PHP write permission denied
PHP write permission denied

Time:03-03

I'm running a server on CentOS 7. User apache | Group apache has 775 permissions on /var/www/html When I execute a PHP file that wants to create a new file in root dir (/var/www/html/) I get the following error Warning: file_put_contents(file.bin): failed to open stream: Permission denied in /var/www/html/file.php

PHP code:

<?php
ini_set('display_errors', 'on');
$file = 'file.bin';
$content = "Content";
file_put_contents($file, $content);
?>

I will update the question with more data but since I'm new I don't know too many things about debugging server errors.

What I tried:

  • For executing scripts (php..) I saw the user to be apache so I gave 'apache' ownership on /var/www/html/

  • tried 777 on ./html (and reversed action after it didn't work to 755)

  • Also tried cd /var/www/html chmod -R 775 . (from comments)

CodePudding user response:

With a high degree of probability, the issue is with selinux. if you turning off selinux with the command (from root) :

setenforce 0

and everything will work, you need to turning on selinux with the command (from root):

setenforce 1

and run the command:

chcon -R -t httpd_sys_rw_content_t /your/path

Everything must work. If this method does not help, I advise you to read answers in this question. There is an answer with a more flexible selinux settings (with using sudo semanage fcontext).

  • Related