Log PHP errors to the separate file

In this post you will find how to configure PHP to log errors and warnings to the separate log file – error_php. You can place error_php wherever you want, but I will suggest to choose the /tmp directory. Why not /var/log/httpd directory where Apache keeps log files? Because anyone who can write to the directory where Apache is writing a log file can almost certainly gain access to the uid that the server is started as, which is normally root. More about logs and security can be read on Apache: Log Files

Contents
  1. Set error log path in /etc/php.ini
  2. Restart Apache

1. Set error log path in /etc/php.ini
Open /etc/php.ini file, find line with error_log and set absolute path to the error_php file. Lines started with “;” are comments, so be sure to remove “;” if there is any.

# php.ini
error_log = /tmp/error_php

Next find log_errors line in php.ini file. Default value is “On” so logging already should be enabled. If not, please write “On”.

# php.ini
log_errors = On

2. Restart Apache
Now you have to restart Apache server, and error_php file should appear in /tmp directory.

# tip 1: restart Apache 
bash> /etc/init.d/httpd restart

# tip 2: restart Apache 
bash> service httpd restart

As you can see, in two steps PHP errors and warnings can be redirected to the separate file.

Categories PHP

Leave a Comment