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:
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 = 1Set the debug port (9000 is default; change if you have port conflicts):
xdebug.remote_port = 9000Define your IDE key (match this to your IDE's settings, e.g.,
PHPSTORMfor JetBrains IDEs,VSCODEfor Visual Studio Code):xdebug.idekey = PHPSTORMAllow connections from your local machine (safe for development environments):
xdebug.remote_host = 127.0.0.1Or use
xdebug.remote_connect_back = 1to 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/xdebugCreate 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=PHPSTORMto your web app's URL (replacePHPSTORMwith your IDE key) to start a debug session.
Performance Profiling
To analyze your code's performance:
- Enable profiling in the Xdebug config:
xdebug.profiler_enable = 1 - Restart Apache, then visit your web page or run your script. Xdebug will generate a
.cachegrindfile in thetrace_output_diryou set. - Install
qcachegrindto visualize the profiling data:sudo apt install qcachegrind - Open the
.cachegrindfile with qcachegrind to see execution times, function calls, and identify bottlenecks.
内容的提问来源于stack exchange,提问作者ilernet

