Search code examples
phpvisual-studio-codedebuggingxdebugvscode-debugger

How to debug PHP using VSCode IDE in Ubuntu


I need to debug PHP code using VS Code IDE. We can debug PHP using Xdebug. So I tried a lots to setup Xdebug for debugging but I failed to use Xdebug using VS Code IDE.

PHP is 8.1
Ubuntu 22.04.1 LTS 

Please help me how can I debug.


Solution

  • Step 1:- Install PHP Debug Extension in VS Code IDE.

    Step 2:- Copy Full output of Phpinfo(). run below Command in CLI for get output of phpinfo().

    php -r "phpinfo();"
    

    Step 3:- open Xdebug wizard Page & paste copied full output of phpinfo() in the form field. after the submitting form you will get step by step Instructions related to setup Xdebug in your System.

    In my system currently I used Php 8.1. see below Snapshot of Xdebug Wizard Instructions.

    Xdebug Wizard Instruction for Php 8.1 in Ubuntu Follow above Instruction which one you will getting from Xdebug Wizard

    Note that, above Wizard Instructions is as per my server configuration. In your case it may differ than my above snapshot. So, you must need to follow Step 2 & 3.

    Step 4:- add below Configuration in your php.ini file.

    [xdebug]
    xdebug.mode = debug
    xdebug.start_with_request = yes
        
    ;Note:- Below Xdebug Path is depends on your OS & your PHP Server. 
    ;So you need to write path of xdebug.so (in Linux) 
    ;OR path of xdebug.dll (in windows) 
    zend_extension=/usr/lib/php/20210902/xdebug.so 
    

    for find Extension Directory for your server (Xampp, Lampp etc. all) then, simply Run php -r "phpinfo();" command then find out extension_dir as like below snapshot

    php extension_dir path using cli

    OR write <?php phpinfo() ?> in your php file then run that php file & found out extension_dir in your output page of phpinfo.

    Step 5:- Restart your Server (Apache).

    Step 6:- Install Browser Extension Initiation. then start debugger in your Vs Code IDE see below snapshot.

    start debug mode in VS code to listen for Xdebug

    Now, All Done.


    for referece I share other source urls

    refer Xdebug Docs refer for Windows refer this for Linux

    But, before starts debugging your code, if you need to check Xdebug is working proply or not then Follow below Steps. otherwise you can ignore below steps.

    In Xdebug 3+. there is xdebug_info() method exits which return info of xdebug extension.

    write <?php xdebug_info(); ?> in your php file & then run this php file.you will get output as like below snapshot

    enter image description here

    & for check Xdebug works properly for CLI run php -r "xdebug_info();" in cli.

    enter image description here