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

Asked

Viewed 107 times

4

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

The problem only happens when scripts are executed via 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 really been cleaned:

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

root@debian:/#

Ok - The file is empty.

3 - Execution of a PHP script via 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 - Consult the Apache log file:

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

root@debian:/#

The problem: the log file remains empty.

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

Thanks for your help!

1 answer

9


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

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

error_log = /var/log/php_errors.log

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

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

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

Depending on what you want to do, just use the shell redial:

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

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


This post has some things that can help in fine tuning of the settings:

Why use error_reporting with display_errors and display_startup_errors?

  • 1

    It worked perfectly: php -c /etc/php/custom.ini meuscript.php

  • 3

    I’m glad it worked out. If you want to automate your PHP, remember to ensure that the crontab user or the script that calls PHP has access to the file.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.