Enable logging in WordPress and PHP for Apache on Debian/Ubuntu

I was working on my custom theme for this blog and was running into an issue involving the Jetpack plugin for WordPress. After spending a couple of hours tinkering around, I determined that the cause appears to be related to Scotch Box (no issues when executing the same steps using VCCW).

I opened an issue with the Scotch Box repo and am gathering log data from both WordPress and PHP. After struggling to find detailed instructions for how to enable and access error logs for both WordPress and PHP, I wrote the below to help future me and others who are also struggling.

Enabling Logging in WordPress

Reference: Debugging in WordPress (Automattic)

Add the following lines to your wp-config.php file:

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);

If you are debugging Jetpack on a localhost, add this line to disable modules that require Jetpack to connect to WordPress.com:
define( 'JETPACK_DEV_DEBUG', true );

Enable Logging in PHP

Reference: PHP: error_reporting

The first step is to determine the location of the PHP binary your install of Apache is using. The simplest way to do this is to create a file and placing it in the root of your web hosting directory (e.g. /var/www). This file should contain:

<?php echo phpinfo(); ?>

Navigate to that URL (http://localhost/phpinfo.php) in your browser. You will see the a list of configuration values for the PHP instance that Apache is using.

PHP information for Apache
PHP information for Apache

Look for the line Configuration File (php.ini) Path. This is the file that must be modified to include the logging options. Most of the configuration fields are already specified in this file, so their values just need to be modified.

Open this file and modify the following lines to appear as below:

error_reporting = E_ALL | E_STRICT
display_errors = On
log_errors = On
error_log = /var/log/php_errors.log

Once the configuration is modified, the log file will need to be created. On Scotch Box, the /var/log directory is restricted, so the web user may be prevented from logging to this location by default. If hosting on Scotch Box or Vagrant, make sure to vagrant ssh first.

sudo touch /var/log/php_errors.log
sudo chown www-data: /var/log/php_errors.log
sudo chmod +rw /var/log/php_errors.log

Restart Apache for the changes to take effect.

$ sudo service apache2 restart

Accessing Logs

When either WordPress or the PHP interpreter throw errors while serving your site, they should now get recorded in the log file. You can use tail or your favourite text editor to view the contents and troubleshoot your issue.

You can find these error logs at these locations:

WordPress: /var/www/public/wp-content/debug.log
PHP: /var/log/php_errors.log

Leave a Reply

Your email address will not be published. Required fields are marked *