0

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.

mikl
  • 1,067
  • 1
  • 20
  • 34
  • Did you restart `apache`, `php`, `etc`? – l'L'l Jul 07 '17 at 22:46
  • @l'L'l yes I restarted the whole system, edited to point that out. – mikl Jul 08 '17 at 01:40
  • Relative path would normally correspond to the document root not the executing script root. – apokryfos Jul 08 '17 at 06:27
  • @mikl Document root is the root of the virtual host or the apache home directory, not the called script. Maybe you could create a small script and always prepend it using [auto_prepend_file](http://php.net/manual/en/ini.core.php#ini.auto-prepend-file) though. Something like `` – apokryfos Jul 10 '17 at 18:45
  • @apokryfos oh, sorry I got that wrong. OTOH the question is about solving it by pure configuration, I am pretty sure it is possible, since that's the way it was working in other server (than the one I am working now). – mikl Jul 10 '17 at 18:54
  • 1
    Well you can just check what the other server is doing. – apokryfos Jul 11 '17 at 05:10

2 Answers2

1

Remove the whole path from error_log directive except the file name.

error_log = var/www/html/error.log

to

error_log = error.log
0

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

mikl
  • 1,067
  • 1
  • 20
  • 34