Why do errors in PHP scripts run via the command line do not appear in the apache error log?

4

It is important to note that errors occurring in PHP script execution via browser are registered without any problem in the file: /var/log/apache2/error.log .

The problem only happens when scripts are executed via the command line.

The sequence I performed is as follows:

1 - Clear Apache error log file:

root@debian:/# echo > /var/log/apache2/error.log
root@debian:/#

2 - Test whether the Apache log file has actually been cleaned:

root@debian:/# cat /var/log/apache2/error.log

root@debian:/#

Ok - The file is empty.

3 - Execution of a PHP script via the command line:

root@debian:/#php script.php
PHP Fatal error:  Call to a member function setWhere() on null in /var/www/html/script.php on line 38
root@debian:/#

4 - Query the Apache log file:

root@debian:/# cat /var/log/apache2/error.log

root@debian:/#

The problem: The log file is still empty.

My question: Why do errors in PHP scripts run via the command line not appear (or are not appearing) in the apache error log? How can I log these errors in the file ( /var/log/apache2/error.log )?

Thanks in advance for your help!

    
asked by anonymous 03.07.2016 / 02:15

1 answer

6

One thing is that the command line will not be going through Apache, by itself it is expected that Apache will not log PHP errors.

The php.ini directive that controls the log path of errors is this:

error_log = /var/log/php_errors.log

Remember that even with everything set up in PHP to accomplish the things you need, nothing guarantees that the PHP command line is using the same php.ini .

According to PHP manual , if you want to determine at the time of execution what is the .ini to be used, you have the -c <path>|<file> directive. Example:

php -c /etc/php/custom.ini meuscript.php

Depending on what you want to do, simply use the shell redirect:

php meuscript.php 1> out.log 2> error.log

An important point in any of the paths chosen is to make sure that the user running PHP from the command line is allowed to write to the log file. (and I hope you're using root just for testing and on a non-production machine).


This post has some things that might help fine tuning settings:

  

Why to use error_reporting with display_errors and display_startup_errors?

    
03.07.2016 / 02:34