Skip to content

Using Xdebug safely in production: step debugging without killing performance

How to install Xdebug on a production server, gate it behind a trigger so it only activates per-request, and avoid the overhead that makes it dangerous by default.

Xdebug in its default configuration degrades PHP performance by 30–400% depending on the operation. Running it permanently on a production server is not a reasonable option — but there are legitimate use cases for attaching a debugger to a production environment: reproducing a bug that only manifests under real data, profiling a specific transaction, or capturing a stack trace for an intermittent fatal. This article covers Xdebug’s trigger mode, which activates debugging only when a specific header or cookie is present, giving you the capability without the permanent overhead.

How to verify

Check whether Xdebug is loaded and which mode it is operating in:

php -v | grep -i xdebug
php -m | grep -i xdebug
php -r 'var_dump(phpversion("xdebug"));'

# Check mode and whether it is step-debug only on trigger
php -r 'var_dump(ini_get("xdebug.mode")); var_dump(ini_get("xdebug.start_with_request"));'

# From the FPM pool perspective
php-fpm8.3 -i | grep -E 'xdebug\.(mode|start_with_request|trigger_value)'

Confirm Xdebug is not active for a normal request:

curl -s "https://example.com/info.php" | grep -c 'XDEBUG_SESSION'
# Should be 0 for a trigger-mode Xdebug with no trigger present

What’s happening

Xdebug 3 introduced a mode setting that replaces the older xdebug.remote_enable and xdebug.profiler_enable knobs. The valid modes are: off, develop, coverage, debug, gcstats, profile, trace. You can combine modes with commas. The mode off installs the extension with zero runtime cost — useful when you want the extension available but not consuming resources.

The xdebug.start_with_request setting controls when a session starts:

  • yes — starts for every request (never in production)
  • no — never starts (mode off equivalent for step debugging)
  • trigger — starts only when the XDEBUG_TRIGGER cookie, GET parameter, or POST parameter is present

In trigger mode, Xdebug checks for the trigger value on every request — the overhead of that check is negligible (a few microseconds). When the trigger is absent, the extension takes the fast path and returns immediately.

For step debugging, the trigger causes Xdebug to open a TCP connection back to your IDE at xdebug.client_host:xdebug.client_port. This requires your IDE to be reachable from the server — either via SSH tunnel or a fixed IP. For profiling, the trigger causes Xdebug to write a cachegrind file to xdebug.output_dir.

The security concern is real: an attacker who sends the trigger cookie causes the server to open an outbound TCP connection. Use xdebug.trigger_value to require a specific secret value rather than accepting any value.

The procedure

  1. Install Xdebug from the correct package source.

    sudo apt install php8.3-xdebug
    # Or via PECL if the distro package lags:
    sudo pecl install xdebug
    # Confirm the .so path
    php -r 'echo ini_get("extension_dir") . "/xdebug.so\n";'
    ls -la $(php -r 'echo ini_get("extension_dir");')/xdebug.so
  2. Configure Xdebug in trigger mode for production.

    Create a dedicated ini file rather than editing the main xdebug.ini:

    ; /etc/php/8.3/mods-available/xdebug.ini
    zend_extension=xdebug
    
    ; Mode: debug for step debugging, profile for profiling, off to fully disable
    xdebug.mode = debug
    
    ; Only start a session when the trigger is present
    xdebug.start_with_request = trigger
    
    ; Require a specific secret — prevents accidental trigger activation
    xdebug.trigger_value = "changeme-production-secret-2025"
    
    ; Where Xdebug connects for step debugging
    xdebug.client_host = 127.0.0.1
    xdebug.client_port = 9003
    
    ; Profiling output directory (needs write permission by www-data)
    xdebug.output_dir = /var/log/xdebug
    xdebug.profiler_output_name = cachegrind.out.%p.%t
    
    ; Reduce the default stack depth to limit overhead
    xdebug.max_nesting_level = 256
  3. Establish an SSH tunnel to forward the debugger port from your machine to the server.

    Xdebug connects from the server back to your IDE. The IDE must be reachable from the server:

    # On your local machine — forward server port 9003 back to localhost:9003
    ssh -R 9003:localhost:9003 user@production-server.example.com

    While this tunnel is open, IDE breakpoints are reachable. Close the tunnel when done.

  4. Trigger a debug session per-request using the browser extension or curl.

    # Manual trigger via curl — step debugging session
    curl -s \
      -H "Cookie: XDEBUG_TRIGGER=changeme-production-secret-2025" \
      "https://example.com/api/endpoint"
    
    # Manual trigger via query string
    curl -s "https://example.com/api/endpoint?XDEBUG_TRIGGER=changeme-production-secret-2025"

    Browser extensions like Xdebug Helper (Chrome/Firefox) set the XDEBUG_TRIGGER cookie automatically when you click the debug icon.

  5. For profiling, switch to profile mode and retrieve the cachegrind file.

    ; Temporarily change the mode in the ini file:
    xdebug.mode = profile

    After restarting FPM, trigger a profiling request:

    curl -s \
      -H "Cookie: XDEBUG_TRIGGER=changeme-production-secret-2025" \
      "https://example.com/slow-page"
    
    # Retrieve the cachegrind file
    ls -la /var/log/xdebug/
    scp user@production-server.example.com:/var/log/xdebug/cachegrind.out.* ./

    Open the cachegrind file in QCacheGrind or Webgrind to identify the expensive call paths.

  6. Disable Xdebug when the task is complete.

    sudo phpdismod -v 8.3 -s fpm xdebug
    sudo systemctl reload php8.3-fpm
    php-fpm8.3 -i | grep xdebug
    # Should return nothing

Common pitfalls

  • xdebug.mode = debug with start_with_request = yes is performance suicide. Every request pays full step-debug overhead. Always combine debug mode with start_with_request = trigger on any server under real load.
  • Using an empty or default trigger_value is a security gap. Any browser extension set to debug mode will activate Xdebug on your production server. Set a strong secret and rotate it.
  • Xdebug and OPcache interact badly in coverage mode. Coverage tracking disables OPcache’s internal optimizations. Never run coverage mode in production; it is for test suites only.
  • The outbound TCP connection for step debugging will time out if your IDE is not listening. This causes a connection timeout pause on that specific request — typically 200 ms to a few seconds depending on xdebug.connect_timeout_ms. Keep tunnels closed when not actively debugging.
  • Cachegrind files grow large. A profiling run on a cold application with deep call stacks can generate 50–200 MB files. Set a quota on /var/log/xdebug and clean it up after each session.

Xdebug in trigger mode gives you production-grade introspection without the performance tax — but it requires discipline around secret rotation and tunnel hygiene. Stack Harbor’s managed operations service can set up secure remote debugging workflows, including bastion-host SSH tunneling and Xdebug lifecycle automation.