How to create a separate log file for PHP

Home > Search > How-to
  by

By default, Apache and PHP will log events in the same log file, which is either at /var/log/httpd/error.log or /var/log/apache2/error.log.  It is not ideal to have both Apache and PHP log errors in the same log file.  The following string of commands creates a directory to log PHP events and a log file just for PHP, so we can have log PHP events separate from the Apache events.

mkdir /var/log/php
chown root:apache /var/log/php or chown root:www-data /var/log/php
chmod 2750 /var/log/php
touch /var/log/php/error.log
chmod 0664 /var/log/php/error.log
  1. In Terminal, type nano /etc/selinux/targeted/contexts/files/file_contexts.local to edit the SELInux local contexts file using the nano editor. If you are unsure the location of the php.ini file on your Linux distribution, type find / -name file_contexts.local in the Terminal.Type
  2. Type the following line into the file: /var/log/php(/.*)? system_u:object_r:httpd_log_t:s0
  3. Press Ctrl O and press Enter (to save)
  4. Press Ctrl X (to exit the nano editor)

 

Run restorecon to update the SELinux context.

restorecon -Rv /var/log/php

 

The ls -lZ /var/log/php command can be used to verify that error.log file has SELinux type httpd_log_t.

 

The ls -lZ /var/log | grep php command can be used to verify that the /var/log/php directory has SELinux type httpd_log_t.

 

Modify the php.ini to list the directory where the error.log is located.

  1. In Terminal, type nano /etc/php.ini or nano /etc/php5/apache2/php.ini to edit the PHP initialization file.  If you are unsure the location of the php.ini file on your Linux distribution, type find / -name file_contexts.local in the Terminal.
  2. Press Ctrl W and type error_log = to go to the area of the php.ini that has the error log setting. Update the error log to be error_log = /var/log/php/error.log.
  3. Press Ctrl O and press Enter (to save)
  4. Press Ctrl X (to exit the nano editor)

 

Depending on the Linux distro being used, restart Apache or HTTPD.

service apache2 restart
service httpd restart

 

To verify that the PHP.ini file has updated to log errors to /var/log/php/error.log, load the phpinfo.php file in your Web browser. Search the phpinfo.php file for error_log, and verify the error_log is /var/log/php/error.log.

 

To test this to verify that PHP errors will be logged to /var/log/php/error.log instead of /var/log/httpd or /var/log/apache2, create a PHP file that includes a resource that does not exist.  As an example, the following PHP includes bogus.php.  There is no such file as bogus.php on the Web server.  This should produce an error in the PHP log.

<?php
include 'bogus.php';
echo 'There is no such file as bogus.php on the Web server.  Check the PHP error.log.';
?>

 

Use the tail command to view the PHP error.log.

tail /var/log/php/error.log

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments