phpapachephp-7apache2.4xhprof

PHP Fatal error: Unknown: Failed opening required '/home/user/pathto/header.php' after adding it as auto_prepend_file


My goal is to configure php profiling for local development website in Kubuntu 16.04.

Installed tideways according to docs and checked it's installed correctly with:

php --ri tideways_xhprof

Created header.php with following contents

<?php 

tideways_xhprof_enable();

Added reference to it to php.ini

auto_prepend_file = "/home/user/pathto/header.php"

Restarted apache2

And getting the below errors in apache error log:

[Sat Jan 27 17:54:24.233604 2018] [:error] [pid 15976] [client 127.0.0.1:42054] PHP Warning: Unknown: failed to open stream: Permission denied in Unknown on line 0 [Sat Jan 27 17:54:24.233653 2018] [:error] [pid 15976] [client 127.0.0.1:42054] PHP Fatal error: Unknown: Failed opening required '/home/user/pathto/header.php' (include_path='.:/usr/share/php') in Unknown on line 0

Tried adding directive

php_value auto_prepend_file /home/user/pathto/header.php 

to Directory block of the website in apache2.conf, but the same error pops.

What's wrong? What permissions are wrong? Regards.


Solution

  • Linux uses a permissions model that incorporates users that can belong to groups, and files and directories that can be assigned to those users and groups. By default, when you install Apache and PHP on Ubuntu, you end up with a new user for Apache named "www-data". Anytime Apache runs and needs to access the file system, it is not unlike any other user, and the operating system requires the same permissions that it would any other user.

    So technically, if you wanted PHP scripts in your user's home directory, you'd have to somehow give Apache's www-data user the permission to access files there.

    When I set up a new server, I'll normally add myself to the www-data group:

    # add user brian to the www-data group
    sudo usermod -a -G www-data brian
    

    This makes managing files easier for me (once I complete the next steps), as I don't need to use sudo to make changes to files.

    I will let www-data own everything under /var/www

    # Change all files at /var/www recursively to be owned by www-data
    sudo chown -R www-data:www-data /var/www
    

    Make it so new files created under /var/www end up being owned by www-data:

    #set the gid on any new dir inside /var/www
    sudo chmod 2755 /var/www/html
    

    Then set myself as the owner, instead of www-data:

    # Be the owner of all www
    sudo chown -R brian:www-data /var/www
    

    Notice that at no time was I giving permissions outside of /var/www, but this makes managing files and directories inside /var/www easier, so you don't feel the need to put PHP files in your home directory.