
Trong bài viết này, chúng ta sẽ học cách Debug PHP với Xdebug trong VS Code từ A–Z.
1. Cài đặt Xdebug
Trên Linux (Ubuntu/Debian)
Trên Windows / macOS
-
Kiểm tra version PHP:
php -v
hoặc
php -i | find "php.ini"
-
Vào trang https://xdebug.org/wizard, copy toàn bộ kết quả
phpinfo()
rồi paste vào đó.
Nó sẽ gợi ý đúng file .dll / .so cho PHP version của bạn. -
Tải file Xdebug
.dll
(Windows) hoặc.so
(Linux/macOS) theo hướng dẫn. -
Thêm vào
php.ini
(thường nằm trong/etc/php/8.x/cli/php.ini
hoặcphp.ini
trong XAMPP/WAMP/MAMP):zend_extension=xdebug xdebug.mode=debug xdebug.start_with_request=yes xdebug.client_host=127.0.0.1 xdebug.client_port=9003
⚠️ PHP 8.x (Xdebug 3.x) mặc định dùng cổng 9003, không còn 9000 nữa.
2. Cấu hình VS Code

Cài extension PHP Debug (by Felix Becker).
Tạo file launch.json
Trong VS Code:Run and Debug
→ create a launch.json file
→ chọn PHP.
Sẽ có file .vscode/launch.json
như sau:
{ "version": "0.2.0", "configurations": [ { "name": "Listen for Xdebug", "type": "php", "request": "launch", "port": 9003, "log": true } ] }
3. Debug
-
Đặt breakpoint trong code PHP.
-
Chạy web server (Apache/Nginx/PHP built-in server):
php -S localhost:8000 -t public
-
Trong VS Code →
Run and Debug
→ chọn Listen for Xdebug → Start Debugging. -
Mở trình duyệt và truy cập
http://localhost:8000
. -
Code sẽ dừng ở breakpoint.
4. Kiểm tra Xdebug đã bật chưa
php -m | grep xdebug
Hoặc trong web, tạo file info.php
:
Tìm block Xdebug.