I have inherited a PHP application and need to investigate various issues. I am not a PHP programmer so bear with me.
I wrote a function to log messages to a custom log file.
<?php
function my_logger($log_msg)
{
error_log("USER INFO:::::",0);
error_log(get_current_user());
error_log(exec('whoami'));
file_put_contents('/var/www/html/myproject/ulogs/my-log.log', date('G:i:s') . ">>$ " . $log_msg . "n", FILE_APPEND);
}
After a lot of 500 errors and no information I discovered that the following line is being written to the error_log.log
PHP Warning:
file_put_contents(/var/www/html/myproject/ulogs/my-log.log): Failed to
open stream: Permission denied in
/var/www/html/myproject/app/lib/log_fns.php on line 11
I have put in a call to get_current_user and whoami which shows me:
[07-Apr-2021 14:26:04 UTC] USER INFO:::: [07-Apr-2021 14:26:04 UTC] root [07-Apr-2021 14:26:04 UTC] apache
I am calling this at the moment from a simple php page called at http:///test_log.php:
<?php
require("/var/www/html/myproject/app/lib/log_fns.php");
//phpinfo();
my_logger("This is a test message from test_log.php");
?>
I tried to chmod the files to 777 in the ./lib directory but was still getting this error. I have hard coded the path you can see above creating the ulogs directory.
I have tried combinations of chmod the ulogs directory to 777 and chown the directory to apache:apache but still get this error.
Any ideas where I can look next or how to solve?
TIA
2
Answers
Run this command
With this work you
file_put_contents
can generate or edit the file in this directoryThis happened to me and no changing of file/folder permissions worked. The issue in the end was SELinux, which is setup as enforcing by default on CentOS.
To disable it, edit
/etc/selinux/config
, and changeto
Reboot and it should work.