If you want to debug your PHP code in VS Code, Xdebug is the best tool. This guide will help you set it up on Ubuntu in a few minutes.
Step 1: Check Your PHP Version
Open a terminal and run:
php -v
Enter fullscreen mode Exit fullscreen mode
Example output:
PHP 8.2.12 (cli)
Enter fullscreen mode Exit fullscreen mode
Remember your PHP version.
Step 2: Install Xdebug
Install Xdebug using:
sudo apt update
sudo apt install php-xdebug
Enter fullscreen mode Exit fullscreen mode
Or install the version-specific package if needed:
sudo apt install php8.2-xdebug
Enter fullscreen mode Exit fullscreen mode
Check that it is installed:
php -m | grep xdebug
Enter fullscreen mode Exit fullscreen mode
If you see xdebug, it is installed successfully.
Step 3: Configure Xdebug
Open the Xdebug configuration file:
sudo nano /etc/php/<your-php-version>/mods-available/xdebug.ini
Enter fullscreen mode Exit fullscreen mode
Replace <your-php-version> with your PHP version (for example, 8.2).
Add these settings:
zend_extension=xdebug.so
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.client_host=127.0.0.1
xdebug.client_port=9003
Enter fullscreen mode Exit fullscreen mode
Save the file.
Step 4: Restart PHP
If you use Apache:
sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode
If you use PHP-FPM:
sudo systemctl restart php<your-php-version>-fpm
Enter fullscreen mode Exit fullscreen mode
Step 5: Install the VS Code Extension
In VS Code:
- Open Extensions.
- Search for PHP Debug.
- Install the extension by Xdebug.
Step 6: Create launch.json
Create a .vscode/launch.json file with this content:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for Xdebug",
"type": "php",
"request": "launch",
"port": 9003
}
]
}
Enter fullscreen mode Exit fullscreen mode
Step 7: Start Debugging
- Open your PHP project in VS Code.
- Add a breakpoint.
- Press F5 to start listening.
- Run your PHP script or open your website.
VS Code will stop at your breakpoint.
Check That Xdebug Is Working
Run:
php -v
Enter fullscreen mode Exit fullscreen mode
If you see something like with Xdebug v3.x.x, everything is ready.
Conclusion
That’s it! You have successfully set up PHP Xdebug on Ubuntu with VS Code. You can now debug your PHP applications more easily and find issues faster.
답글 남기기