6

I have a PHP script running just fine through Apache, but it fails when running in CLI, so I want to find out what is happening.

For this, I'd like to see the error log, but the CLI error log I set is not working. I have set this in the proper php.ini file, which is confirmed when I obtain the error log details through the command line:

php -i | grep error

Result:

display_errors => Off => Off
display_startup_errors => Off => Off
error_append_string => no value => no value
error_log => /var/log/php_error_log => /var/log/php_error_log
error_prepend_string => <font color=ff0000> => <font color=ff0000>
error_reporting => 30711 => 30711
html_errors => Off => Off
ignore_repeated_errors => Off => Off
log_errors => On => On
log_errors_max_len => 1024 => 1024
track_errors => Off => Off
xmlrpc_error_number => 0 => 0
xmlrpc_errors => Off => Off
suhosin.disable.display_errors => Off => Off
suhosin.sql.bailout_on_error => Off => Off

So error_log and log_errors are both set. However, no logs are actually saved. Permissions are in place. So what can it be?

I've read the question PHP CLI won't log errors, but I could not find a solution in there.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • did you make sure the user running php has write acccess to `/var/log/php_error_log`? –  Jan 21 '12 at 17:19
  • It did have access to write, but the problem turned out to be that there was a redirect taking place, which simply did not show any output in CLI nor any error. –  Jan 27 '12 at 13:48
  • A shell redirect should not affect PHP's logging behaviour. can you post `ls -al /var/log/php_error_log` ? – hek2mgl Dec 14 '12 at 17:33
  • Related to the canonical: *[Where does PHP store the error log? (PHP 5, Apache, FastCGI, and cPanel)](https://stackoverflow.com/questions/5127838/)* (despite the over-specific title) – Peter Mortensen Oct 06 '21 at 16:18

2 Answers2

12

PHP via CLI and HTTP request both use a different php.ini configuration. In your case I am suspicious, whether you are dealing with the correct php.ini file used by CLI PHP. Try executing:

php -i >> info.txt

This will save your phpinfo() into a local file, info.txt. Then open the file and find the setting "Loaded Configuration File" - which should point to the absolute path to the loaded php.ini file.

If this is all OK, then simply run the following CLI command to test whether function error_log works as expected. Execute via CLI:

php -r "error_log('test error', 3, './errors.log');"

And you should find in the same directory the file errors.log with the specified test error. If you cannot find it then you probably need to update your php.ini settings or set the correct write permissions, process or file owner.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lubosdz
  • 4,210
  • 2
  • 29
  • 43
  • 1
    If he is running the command from command line, the I suspect he is using the CLI configuration – sitilge Jun 13 '15 at 21:02
  • sure, but what if he does not know which is proper php.ini and he is trying to configure wrong one? I did the same mistake myself and took me a while to figure it out:-) Instead of giving advices on configuring well documented php directives, he should check whether he's modifying proper php.ini file. – lubosdz Jun 13 '15 at 21:10
  • tks for the answer, just missing semicolon at the end of the test error line. – lisandro Mar 18 '19 at 13:59
1

The logfile is probably already created by the PHP interpreter (mod_php or php_fpm, etc.) with a different user (www-data?). Therefore if you run your script as anyone else it may not have write access to the file or may not even have access to the path. You may try running it as a different user

sudo -u www-data php your_script.php
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tomáš Fejfar
  • 11,129
  • 8
  • 54
  • 82
  • Nice yeah I had permission errors on my log because I created them manually. BUT if you just put in the location in php.ini, PHP will create the files for you (which I didn't know) -- way easier! – sij_a Nov 05 '15 at 15:05