Skip to content

PHP error reporting in production: what to log, what to hide

Configure PHP error reporting, display_errors, and log_errors correctly in production to surface actionable data without leaking internals to users.

Misconfigured PHP error reporting is one of the most common information-disclosure vectors in production PHP: stack traces, database credentials in exception messages, and absolute filesystem paths all show up in browser output when display_errors is left on. The right production posture is aggressive logging to disk with zero output to the client. This article is the reference for getting that configuration right and verifying it holds under PHP-FPM, .user.ini, and OPcache restarts.

How to verify

Confirm the effective error configuration — not what php.ini says, but what PHP actually uses at runtime:

# Check FPM pool's effective ini values
php-fpm8.3 -i | grep -E 'display_errors|log_errors|error_reporting|error_log'

# Check CLI (often different from FPM)
php -i | grep -E 'display_errors|log_errors|error_reporting|error_log'

# Confirm no .htaccess or .user.ini overrides are present
find /var/www/app -name '.user.ini' -exec grep -l 'display_errors\|error_reporting' {} \;
find /var/www/app -name '.htaccess' -exec grep -l 'php_flag\|php_value' {} \;

# Trigger a notice and verify it appears in the log, not in HTTP response
php -r 'trigger_error("test-error-$(date +%s)", E_USER_NOTICE);'
tail -5 /var/log/php8.3-fpm.log

What’s happening

PHP has two orthogonal error switches: error_reporting controls which error levels are generated, and display_errors / log_errors control where they go. You can generate E_ALL and send none of it to the browser — that is the correct production setup.

The error levels that matter most in production:

LevelCodeBehavior
E_ERROR1Fatal, script stops
E_WARNING2Non-fatal, execution continues
E_NOTICE8Often sloppy code, not always bugs
E_DEPRECATED8192Will break in a future PHP version
E_STRICT2048Merged into E_ALL since PHP 7

error_reporting = E_ALL is correct in production — it generates records for all levels so they are available for diagnosis. The mistake is coupling it with display_errors = On, which outputs them to the HTTP response.

display_errors = stderr is a subtly dangerous setting: it sends errors to STDERR, which PHP-FPM may route to the web server’s error log or to the response body depending on how your server was compiled and configured. Only use Off in production.

PHP-FPM pools can override php.ini settings with php_admin_value and php_admin_flag. The admin variants cannot be overridden by ini_set(), php_value, or .user.ini. This is the mechanism to enforce settings globally without relying on application-level code.

The procedure

  1. Set the global php.ini baseline.

    ; /etc/php/8.3/fpm/php.ini
    error_reporting = E_ALL
    display_errors = Off
    display_startup_errors = Off
    log_errors = On
    error_log = /var/log/php/php8.3-error.log
    log_errors_max_len = 4096
    ignore_repeated_errors = Off
    ignore_repeated_source = Off
    html_errors = Off

    html_errors = Off prevents PHP from wrapping error messages in <b> tags that render as bold text when they leak into a response — belt-and-suspenders protection.

  2. Enforce the settings at the FPM pool level so they cannot be overridden.

    ; /etc/php/8.3/fpm/pool.d/app.conf
    php_admin_flag[display_errors] = Off
    php_admin_flag[log_errors] = On
    php_admin_value[error_reporting] = 32767
    php_admin_value[error_log] = /var/log/php/php8.3-app-fpm.log

    The numeric value 32767 is E_ALL as an integer. Using the constant name here is not valid in pool config — you must use the integer.

  3. Create and permission the error log file before restarting FPM.

    sudo mkdir -p /var/log/php
    sudo touch /var/log/php/php8.3-app-fpm.log
    sudo chown www-data:www-data /var/log/php/php8.3-app-fpm.log
    sudo chmod 640 /var/log/php/php8.3-app-fpm.log
    sudo systemctl restart php8.3-fpm
  4. Configure logrotate for the PHP error log.

    # /etc/logrotate.d/php8.3-app
    /var/log/php/php8.3-app-fpm.log {
        daily
        missingok
        rotate 14
        compress
        delaycompress
        notifempty
        create 640 www-data www-data
        postrotate
            /bin/kill -USR1 $(cat /run/php/php8.3-fpm.pid 2>/dev/null) 2>/dev/null || true
        endscript
    }

    PHP-FPM respects USR1 to reopen log files without restarting workers.

  5. Set a custom error handler in application code for structured logging.

    For applications that need JSON-structured error logs (to feed into Loki, Elasticsearch, or Datadog):

    set_error_handler(function (int $errno, string $errstr, string $errfile, int $errline): bool {
        $entry = json_encode([
            'ts'    => date('c'),
            'level' => $errno,
            'msg'   => $errstr,
            'file'  => $errfile,
            'line'  => $errline,
        ]);
        error_log($entry);
        return true; // suppress PHP's own log entry for this error
    });
    
    set_exception_handler(function (Throwable $e): void {
        $entry = json_encode([
            'ts'    => date('c'),
            'class' => get_class($e),
            'msg'   => $e->getMessage(),
            'trace' => $e->getTraceAsString(),
        ]);
        error_log($entry);
        http_response_code(500);
        // Return a generic message — never the exception itself
        echo json_encode(['error' => 'Internal server error']);
        exit(1);
    });
  6. Verify that errors do not appear in HTTP responses.

    # Generate a deliberate notice
    curl -s "https://example.com/test-notice.php" | grep -i 'notice\|warning\|fatal\|error'
    # Should return empty — no PHP error text in the response body

Common pitfalls

  • .user.ini files override pool config when set via php_value (not php_admin_value). Any .user.ini in the document root or a parent directory can set display_errors = On and override your php.ini. Scan for them and remove them, or use php_admin_flag which .user.ini cannot override.
  • display_errors = stderr routes errors to the web server log in some setups. On a busy server this floods Nginx or Apache’s error log and can cause log rotation issues. Use Off and the dedicated error_log path.
  • Error logs containing exception messages can leak credentials. Database connection strings, API keys passed as arguments, and user-supplied data can all appear in stack traces. Audit your exception hierarchy and strip sensitive fields before logging.
  • log_errors_max_len = 0 disables the length cap, which can blow up log files. Very long stack traces from deep call stacks or large payloads in exceptions can write megabytes per error. Keep the cap at 4096 bytes and ensure your exception handler truncates before calling error_log().
  • FPM slow log and error log are different files. The slowlog directive in the pool config captures requests exceeding request_slowlog_timeout — it is not the PHP error log. Make sure you are tailing the right file when debugging.

PHP error reporting configuration seems trivial but the failure modes are serious — from user-visible stack traces to log files that grow without bound. Stack Harbor’s managed operations service includes runtime configuration audits that verify error handling, log rotation, and exception instrumentation across all your PHP environments.