(gs):PHP Error Logging via .htaccess

  • This page was last modified on December 24, 2011, at 20:54.
The (mt) Community Wiki is a collaborative project. Any (mt) Media Temple customer or employee may contribute. Not all articles and/or content have been tested for accuracy by (mt) Media Temple.

For officially moderated and tested articles, be sure to visit our KnowledgeBase.

From (mt) Community Wiki

Contents


The following article will demonstrate how to enable some advanced PHP error logging functionality for a domain on your (gs) Grid-Service.

PHP Error Logging

The web server access and error logs do not always provide information which is verbose enough to determine the source of the error in a PHP script. Luckily, PHP provides some extremely verbose and configurable error handling facilities.

Instructions

To log PHP errors for the "example.com" website on a (gs) Grid-Service with a site number of 00000, add the following lines to the .htaccess file in the "domains/example.com/html/" folder:

# suppress PHP errors from displaying in the browser
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off

# log PHP errors to a file
php_flag log_errors on
php_value error_reporting 2047
php_value error_log "/home/00000/data/php_errors.example.com.log"
Be sure to replace example.com with your own domain and 00000 with your (gs) Grid-Service's site number. If you don't know your site number, browse to http://kb.mediatemple.net/questions/268/#gs for information on how to find it.
  • In the above code, the integer 2047 is used to denote the "E_ALL" level of error reporting in legacy PHP versions. Depending on the specific version of PHP you are running on that domain, the integer values may correspond differently to the error verbosity levels. 2047, however, should continue to function as "E_ALL" in newer versions of PHP.
  • It is best to keep the log in a directory which is not web-accessible like the "data" directory used in the above example code; some PHP errors may expose vulnerabilities or sensitive information that could be useful to a visitor with malicious intent.
  • Depending on the level of verbosity you have selected for your PHP error logging and the frequency of errors or warnings generated by your domain, the PHP error logging file could grow very large very quickly! Be sure to keep a close eye on the size of this file and to only have the error logging enabled if you are actively debugging potential site coding issues.

Using the Errors to Troubleshoot Code

You can access the log file wherever you configured it to be written. In the example above, the file can be accessed at /home/00000/data/php_errors.example.com.log, or "data/php_errors.example.com.log" via FTP. Errors will each be reported on a new line and will be timestamped. Here is an example error line:

[09-Dec-2011 18:58:57] PHP Fatal error:  Call to undefined function eccho() in /nfs/c00/h00/mnt/00000/domains/example.com/html/samplefile.php on line 2

From the above error sample, it is easy to see that the problem is that the "eccho" function does not exist. Since we know that "echo" is a function in PHP, it would be a safe bet that there was just a typo in the PHP code on line 2 of the "samplefile.php" file.

Relevant Links