phpxdebug

Xdebug starts the connection and than closes automatically


I am using Visual Studio Code and Xdebug is by default installed in it with version v3.x.x.

Sharing configuration below,

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9001,   
            "stopOnEntry":true,        
            "log": true,
            "hostname": "localhost",     
            "pathMappings": {
                "/var/www/html": "${workspaceRoot}",
                "/app": "${workspaceRoot}/app"
            }
        }
    ]
}

php.ini,

xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.discover_client_host=1

test.php

<?php
echo "<script>alert('Welcome');</script>";
phpinfo();
?>

Now when I start the debugger at port 9001 on my local host (note no remote hosting is there). And on accessing it via, http://localhost:9001/test.php

It gives following comments on Debug console,

new connection 1 new connection 2

It gets started, but for some strange reason gives me, two new connection and they are printed only after I have access it via browser.

Now I wait for 5-10 seconds (meanwhile, browser keep loading, but nothing is visible and its still attempting to access test.php) I have placed Debugger point on all lines.

And suddenly after 10 seconds, its closes connection with below comments,

<- outputEvent
OutputEvent {
  seq: 0,
  type: 'event',
  event: 'output',
  body: { category: 'console', output: 'connection 2 closed\n' }
}

connection 2 closed

<- continuedEvent
ContinuedEvent {
  seq: 0,
  type: 'event',
  event: 'continued',
  body: { threadId: 2, allThreadsContinued: false }
}

<- threadEvent
ThreadEvent {
  seq: 0,
  type: 'event',
  event: 'thread',
  body: { reason: 'exited', threadId: 2 }
}

<- outputEvent
OutputEvent {
  seq: 0,
  type: 'event',
  event: 'output',
  body: { category: 'stderr', output: 'connection closed\n' }
}

**connection closed**

I have checked everywhere, but unable to to find answer to my problem. Request for help


Solution

  • Finally I was able to start Xdebug with Visual Studio code on XAMPP server. I am sharing my solution so that others don't face similar issues.

    1. Install XAMPP on Ubuntu

    2. Configure it to route it to code outside htdocs folder Follow this link, https://askubuntu.com/questions/64095/change-xampps-htdocs-web-root-folder-to-another-one

    3. Create index.php file and run, phpinfo();

    4. open browser and execute localhost/index.php

    5. Copy the text to https://xdebug.org/wizard

    6. The resultant will be a page with list of instrcutions, just follow them one by one and make sure Xdebug is successfully installed on Ubuntu

    7. Now the important piece, open file /opt/lampp/etc/php.ini (I was trying to configure my local Laravel php.ini in my code, that's where its failing to recognize) and paste following commands,

      xdebug.mode=debug

      xdebug.start_with_request=yes

      xdebug.client_port=9003

      xdebug.client_host = localhost

      zend_extension = "/opt/lampp/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so"

    8. Now go Visual Studio Code and open apply debugger points on your index.php

    9. If running and debugging is not yet configured (no launch.json has been created), VS Code will show that and you can configure it by Running. Leave launch.json settings default.

    10. Go to Run --> Start Debugging -> "Listen for Xdebug" and configure same for PHP for first time.

    That's it !