Skip to content
Advertisement

How to log errors in the same directory of the script

What the question is about: need each script to log its errors in the same directory that the script is located, need to achieve this through pure configuration. By script I am not referring to included files, I’m referring to the files of the executing web pages.


Example 1

A syntax error of:

/var/www/html/index.php

Has to log to:

/var/www/html/error.log

Example 2

A syntax error of:

/var/www/html/foobar/page.php

Has to log to:

/var/www/html/foobar/error.log

I did some configuration attempting to achieve the solution, but it doesn’t work, the log file isn’t being created (at least not in the directory of the script).

I used phpinfo() to check the location of php.ini, it’s /etc/php.ini. In this file I configured:

log_errors = On
error_log = error.log

Then I just restarted the whole system with /sbin/shutdown -h now (restarted the computer), then I checked phpinfo() and confirmed that the changes are made.

What I researched is that when the value of the error_log directive isn’t an absolute path, but instead only a file name, then the location of the log file will be the same directory of the script which generates the error, and will have that file name.

So, if the log file doesn’t exist in the script’s location, then it should be created there when an error occurs, but for some reason it’s not being created. To see if it’s a permission problem I manually created the log file and gave it full permissions (rwxrwxrwx), but still nothing is logged into this file when I reproduced the error. Finally I also tryed full permissions (rwxrwxrwx) for the directory containing the log file, but the problem persists.

So what’s the cause and solution?


System information: GNU/Linux CentOS with Apache.

Note: This is not a duplicate of error_log in the same directory as included files?, this question has nothing to do with included files.

Note 2: This is not a duplicate of PHP Parse/Syntax Errors; and How to solve them?, the syntax error is obviously just a random example, the question is not about solving syntax errors.

Advertisement

Answer

The cause of the problem was SELinux restricting Apache from reading or writing in the files or directories.

Solutions

If the error log file already exists: in this case an alternative is to change the SELinux context of the file to the type httpd_sys_rw_content_t (instead of httpd_sys_content_t or other):

chcon -t httpd_sys_rw_content_t /var/www/html/error.log

If the error log file doesn’t exist and has to be created by Apache: in this case an alternative is to change the SELinux context of the directory:

chcon -t httpd_sys_rw_content_t /var/www/html

Source: https://wiki.centos.org/HowTos/SELinux

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement