← All posts

PHP: show errors in development, log them in production (display_errors, error_log, WP_DEBUG)

Blank page, a 500 error with no message, or file paths shown to your visitors? How to set display_errors, error_reporting and error_log for each environment, find the php.ini that's really loaded, enable WP_DEBUG without exposing debug.log, and catch errors in your own code.

PHP: show errors in development, log them in production (display_errors, error_log, WP_DEBUG)

Key points

  • In development, display every error; in production, display none of them but log them all.
  • A blank page or a 500 error with no message almost always means the error is in a log: you just need to know which one.
  • On WordPress, WP_DEBUG_LOG writes to wp-content/debug.log by default, which is reachable from the web: give it a path outside the web root.

“The site shows a blank page” and “there’s a 500 error” are two of the messages we receive most often. In both cases PHP has almost always explained the problem somewhere; it just didn’t display it, which is the right behaviour in production. The opposite happens too: we regularly come across live sites showing “Warning” messages with full server file paths, which looks unprofessional and helps attackers. Here’s how to set up PHP error display and logging properly for each environment, and how to find the error when the site goes down.

The four settings that matter

PHP error handling comes down to a few php.ini directives:

  • error_reporting: which kinds of errors PHP takes into account. Use E_ALL everywhere, production included: you don’t want to ignore anything, just choose where the information goes.
  • display_errors: whether errors are printed in the page. On in development, Off in production, no exceptions.
  • log_errors: whether errors are written to a log. Always On.
  • error_log: the log file. If it isn’t set, errors go to the web server’s or PHP-FPM’s log, depending on the setup.

So the two standard configurations look like this:

; DEVELOPMENT php.ini: see everything, straight away
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
error_log = /var/log/php/errors.log

; PRODUCTION php.ini: show visitors nothing, log everything
error_reporting = E_ALL
display_errors = Off
display_startup_errors = Off
log_errors = On
error_log = /var/log/php/errors.log

PHP ships two templates, php.ini-development and php.ini-production, that follow this logic. On a hand-built server, check which one was copied: we’ve found production servers running the development template more than once.

Finding the right php.ini and the right log

The classic trap: you edit a php.ini and nothing changes. A server can have several, one per PHP version and one per execution mode (command line, PHP-FPM, Apache module). Start by checking what’s actually loaded:

# Which php.ini is actually loaded, and which values are active?
php --ini
php -i | grep -E 'display_errors|error_reporting|log_errors|error_log'

# Careful: command-line PHP and PHP-FPM often use different php.ini files.
# For the website, temporarily create a file that prints the configuration:
#   <?php phpinfo();   (delete it as soon as you've read it)

# Follow the log live while you reproduce the problem
tail -f /var/log/php/errors.log

After editing php.ini, restart PHP-FPM (or Apache) for the change to apply. On shared hosting without access to that file, these settings usually live in the control panel, or in a .user.ini file at the site root when PHP runs as FPM or CGI; that file is only re-read periodically, so allow a few minutes before the change shows.

A word on ini_set('display_errors', '1') at the top of a script: handy for a quick test, but it only applies once the script is running. A syntax error in that same file, for instance, happens before that and won’t be displayed. That’s why the baseline belongs in PHP’s configuration, not in your code.

When the site returns a 500 with nothing in the PHP log, check the web server’s error log too (Nginx or Apache): a timeout, a broken .htaccess rule or a PHP-FPM worker killed for lack of memory shows up there, not in PHP’s log.

WordPress: WP_DEBUG without exposing your errors

WordPress adds its own layer of settings in wp-config.php. This is the configuration we use to debug a live site without showing visitors anything:

// wp-config.php, above the "That's all, stop editing!" line
define( 'WP_DEBUG', true );
// A path OUTSIDE the web root rather than wp-content/debug.log
define( 'WP_DEBUG_LOG', '/var/log/php/wordpress-debug.log' );
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );
  • WP_DEBUG turns on debug mode: WordPress also reports deprecated functions and incorrect use of its API.
  • WP_DEBUG_LOG set to true writes to wp-content/debug.log. That file sits in the public folder: unless the server blocks it, anyone can read it at /wp-content/debug.log, complete with server paths and sometimes sensitive data. Since WordPress 5.1 you can pass a path instead of true: put it outside the web root.
  • WP_DEBUG_DISPLAY set to false keeps errors out of the pages, backed up by ini_set in case PHP’s own display setting is switched on elsewhere.

Once you’ve found the problem, set WP_DEBUG back to false in production: debug mode can make the log grow very quickly on a site running older plugins. WordPress’s “There has been a critical error on this website” screen also emails the administrator with the file and line at fault and a recovery-mode link: often the fastest way to find the culprit plugin, provided the site’s emails actually go out.

In your own code: stop letting warnings slip through

In a custom PHP application, we always add an entry point that sets these rules and turns warnings into exceptions. A Warning: Undefined array key ignored today is often a data bug tomorrow; making it an exception forces you to deal with it.

<?php
// bootstrap.php: loaded first by the application
declare(strict_types=1);

$isDev = getenv('APP_ENV') === 'dev';

ini_set('display_errors', $isDev ? '1' : '0');
ini_set('log_errors', '1');
error_reporting(E_ALL);

// Turn warnings into exceptions: no more ignored "Warning"
set_error_handler(function (int $level, string $message, string $file, int $line): bool {
    if (!(error_reporting() & $level)) {
        return false; // error silenced with @: let PHP handle it
    }
    throw new ErrorException($message, 0, $level, $file, $line);
});

// Last safety net: any uncaught exception
set_exception_handler(function (Throwable $e) use ($isDev): void {
    error_log(sprintf('%s: %s in %s:%d', $e::class, $e->getMessage(), $e->getFile(), $e->getLine()));
    http_response_code(500);
    echo $isDev ? '<pre>' . htmlspecialchars((string) $e) . '</pre>'
                : 'Something went wrong. We have been notified.';
});

The exception handler is the last safety net: it logs the error with its context, returns a real 500 status (which matters for monitoring and search engines) and shows the visitor a neutral message. In development it displays the full trace.

The error_reporting() check respects the @ operator: some libraries use it deliberately, and turning those errors into exceptions would break them. Fatal errors (memory exhausted, time limit) don’t go through this handler; register_shutdown_function() combined with error_get_last() lets you log them too.

As the project grows, a logging library such as Monolog takes over: severity levels, multiple destinations, structured context. And a log only helps if someone reads it: set up rotation (logrotate) so it doesn’t fill the disk, and an alert, even a basic one, when the error count jumps after a release.

FAQ

Why does my PHP site show a blank page?

Because a fatal error occurred while display_errors is off. The message is in the log set by error_log, or failing that in the web server’s or PHP-FPM’s log. On WordPress, turn on WP_DEBUG_LOG to capture it.

Is leaving display_errors on in production really a problem?

Yes: the messages reveal file paths, table names and sometimes query fragments that help an attacker, and they can break page layout or JSON responses. Turn it off and log instead.

Should I delete debug.log after debugging?

Yes, if it lives in wp-content. Better still, never write it there: give WP_DEBUG_LOG a path outside the web root.

A 500 error nobody can explain, or a site showing its errors to visitors? We diagnose and stabilise WordPress sites and PHP applications, and set up monitoring that warns you before your customers notice.

Share LinkedIn Facebook X WhatsApp
</>

webtestudio

Web agency: custom WordPress sites, premium themes and SaaS software.

Discuss your project →