You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 18.04 + PHP 7.2环境下Xdebug相关技术咨询

Hey there! Since you're running PHP 7.2 on Ubuntu 18.04 with Xdebug 2.6.0 already installed (great, I can see that from your php -v output), let's walk through configuring and using Xdebug effectively. Here's a step-by-step breakdown tailored to your setup:

Xdebug Configuration & Usage Guide (PHP 7.2 + Ubuntu 18.04)

1. Locate the Xdebug Configuration File

First, find where Xdebug's config file lives. Run this command to get the exact path:

php -i | grep xdebug.ini

Typically, it'll be something like /etc/php/7.2/mods-available/xdebug.ini (for global config) or /etc/php/7.2/apache2/conf.d/20-xdebug.ini (for Apache-specific settings).

2. Basic Configuration Tweaks

Open the config file with your preferred editor (I'll use nano here for simplicity):

sudo nano /etc/php/7.2/apache2/conf.d/20-xdebug.ini

Add or update these key settings (adjust based on your needs):

  • Enable remote debugging:

    xdebug.remote_enable = 1
    
  • Set the debug port (9000 is default; change if you have port conflicts):

    xdebug.remote_port = 9000
    
  • Define your IDE key (match this to your IDE's settings, e.g., PHPSTORM for JetBrains IDEs, VSCODE for Visual Studio Code):

    xdebug.idekey = PHPSTORM
    
  • Allow connections from your local machine (safe for development environments):

    xdebug.remote_host = 127.0.0.1
    

    Or use xdebug.remote_connect_back = 1 to auto-detect the client IP (handy if you're debugging from different devices on a local network).

  • Optional: Set up trace/profiling directories (for performance analysis):

    xdebug.trace_output_dir = /var/log/xdebug
    xdebug.profiler_output_dir = /var/log/xdebug
    

    Create the directory and set proper permissions:

    sudo mkdir /var/log/xdebug && sudo chown www-data:www-data /var/log/xdebug
    

After making changes, restart Apache to apply them:

sudo systemctl restart apache2

If you're using PHP-FPM instead, restart that service:

sudo systemctl restart php7.2-fpm

3. Verify Configuration

To confirm Xdebug is using your new settings, run:

php -i | grep Xdebug

You'll see a list of all Xdebug parameters—check that your updated values are present. Alternatively, create a simple phpinfo.php file with <?php phpinfo(); ?>, host it on your Apache server, and search for "Xdebug" to verify the config in a browser.

4. Common Use Cases

Debugging CLI Scripts

To debug a command-line PHP script, start your IDE's Xdebug listener first, then run the script with the Xdebug session environment variable:

XDEBUG_SESSION=PHPSTORM php your_script.php

Your IDE will catch the breakpoint hits automatically.

Debugging Web Applications

There are two easy ways to trigger Xdebug for web requests:

  • Browser Extension: Install an extension like Xdebug Helper (for Chrome/Firefox), set its IDE key to match your config, and toggle the debug mode on before visiting your web app.
  • URL Parameter: Append ?XDEBUG_SESSION_START=PHPSTORM to your web app's URL (replace PHPSTORM with your IDE key) to start a debug session.

Performance Profiling

To analyze your code's performance:

  1. Enable profiling in the Xdebug config:
    xdebug.profiler_enable = 1
    
  2. Restart Apache, then visit your web page or run your script. Xdebug will generate a .cachegrind file in the trace_output_dir you set.
  3. Install qcachegrind to visualize the profiling data:
    sudo apt install qcachegrind
    
  4. Open the .cachegrind file with qcachegrind to see execution times, function calls, and identify bottlenecks.

内容的提问来源于stack exchange,提问作者ilernet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:52:25