Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to open stream on file_put_contents in PHP on CentOS 7

I know this is a common bug with many threads but I'm tired of searching and I still haven't solved it. When I try to run the function file_put_contents I get the following error:

Warning: file_put_contents(test.txt): failed to open stream: Permission denied in /var/www/html/nurses/adminWriter.php on line 29

I've already done the most common answer, change the permissions of the nurses folder and the test.txt file to 777. That didn't work.

I'm currently on a fresh install of the latest CentOS 7 server.

I've found that if I run the php script through the console:

# php /var/www/html/nurses/adminWriter.php

It works just fine, I get the error only when running it through the browser, but I really need to run it this way.

Best Regards!

like image 499
danielnovais92 Avatar asked Feb 28 '15 19:02

danielnovais92


2 Answers

chcon -Rv --type=httpd_sys_rw_content_t /dir

This may help,rather than turn off the selinux.

like image 170
algking Avatar answered Nov 05 '22 09:11

algking


Ok, I found the issue: selinux

Here's the solution: http://kb.sp.parallels.com/en/11142

In case the page goes down:

Symptoms

After you change the default directory to store website content (say, to "/home"), you are unable to access the content of subdomains and additional websites over the Web.

Cause

This problem occurs if SELinux works in "Enforcement" mode. In this mode, webroot directories for websites and subdomains inside them have different permissions, and your web server fails to access them. Run this command:

# getenforce

The output should be: Enforcing

Resolution

Disable SELinux support: Open the file /etc/selinux/config in any editor and find this row:

SELINUX=<value>

Change it to:

SELINUX=disabled

If you cannot find this file, open the file /boot/grub/grub.conf in any editor and add the parameter selinux=0 to the Grub Boot Loader:

title SE-Linux Test System
root (hd0,0)
kernel /boot/vmlinuz-2.4.20-selinux-XXXXXXXXXX ro root=/dev/hda1 nousb selinux=0
#initrd /boot/initrd-2.4.20-selinux-XXXXXXXXXX.img

Reboot the server to apply the changes. If you do not want to reboot, run this command:

# setenforce 0
like image 30
danielnovais92 Avatar answered Nov 05 '22 07:11

danielnovais92