0

I'm using Apache and PHP. Webroot directory is /home/name/public_html

I want to include a file from /home/name/abc.php

include_one "/home/name/abc.php";

I got failed to open stream: Permission denied warning.

If i move the same file inside the webroot /home/name/public_html/abc.php

There is no error.

Apache User and Group has the permission to access the file /home/name/abc.php

I have another server with the similar configuration, it is working. Just want to know the possible reason.

I tried to run the PHP script directly in linux console, there is no permission issue. I guess the problem is in Apache configuration.

Tester
  • 798
  • 2
  • 12
  • 32
  • This is not a problem, it's a security feature. Why does the file need to be outside of the webroot directory? – random_user_name Jul 21 '16 at 18:51
  • 1
    php can read a file anywhere in the filesystem that it has the rights to REACH and READ. even if the file itself has read permissions, you still need access to its containing directory. – Marc B Jul 21 '16 at 18:51
  • 1
    @cale_b: because not everything should be inside the webroot in the first place. by definition, anything inside the webroot is available to the world. one single config error or server glitch and you cuold be serving up raw php code, including whatever back-end credentials are stored in that code. – Marc B Jul 21 '16 at 18:52

2 Answers2

0

You should verify that php is really executed as the apache user. Depending on your configuration it might be possible that php is running under a different user, e.g. if your Apache is set up to use php-fpm.

If the server is a linux system, putting this

<?php
echo "<pre>";
var_dump(posix_getpwuid());

into a file and accessing via the web browser should show you the user informations.

Eugene
  • 356
  • 3
  • 14
0

I found the way to solve this issue from here. I have SELinux running on my Centos 7 Virtual Server.

I need to grant httpd permission to read from /home dir using:

sudo setsebool httpd_read_user_content=1
Community
  • 1
  • 1
Tester
  • 798
  • 2
  • 12
  • 32