Xdebug: [Step Debug] Could not connect to debugging client

40,973

Solution 1

PHP 7.4
Docker
PHPStorm 2020.1
Xdebug 3.1.0

Install Xdebug in your docker container using Dockerfile

RUN pecl install xdebug-3.0.1 && docker-php-ext-enable xdebug

Configure php.ini with following:

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

Go to PHPStorm - Settings - PHP - Debug - Xdebug and set the port to 9003 (by default)

PHPStorm

That's it (:

If you want to enable/disable debugger only when you need it: just install a browser extension called "Xdebug helper", select "Debug" and remove "xdebug.start_with_request = yes" from php.ini

[xdebug]
xdebug.mode = debug
xdebug.discover_client_host = 1

Solution 2

I will start saying big thanks to @LazyOne who spent some time helping me on this one until we make it to work. Here is how the config looks like for me currently and it is working fine:

zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so
xdebug.mode=debug
xdebug.client_port=9005

You need also to update the Xdebug port at File | Settings | Languages & Frameworks | PHP | Servers to reflect the new one but also enable the option to listen on Xdebug3 incoming connections. (I believe it is enabled by default in PhpStorm 2020.3)

enter image description here

That is the setup for a backend project where no browser is in the middle, I have not tried but for those, you might need:

xdebug.start_with_request=yes

And also have File | Settings | Languages & Frameworks | PHP | Servers well configured.

Note: We found the host had enabled IPv6 and I disabled it and in addition, added the following setting to the IDE through Help > Edit Custom VM options: -Djava.net.preferIPv4Stack=true. After added the IP4 setting to the IDE I haven't tried re-enabling IPv6 and see if Xdebug 3 still working

Solution 3

What worked for me was to change start_with_request from yes to trigger.

This worked for me:

xdebug.mode=debug
xdebug.start_with_request=trigger
xdebug.client_port=9003

Solution 4

PHP 7.3
Docker (for Mac)
PhpStorm 2021.1


You may not need to install it via PECL (which took a long time to build and didn't work for me).

All I did was adding php7.3-xdebug to my apt-get install command and configure the port mapping correctly as follows:

In Dockerfile add: RUN apt-get install -y php7.3-xdebug

In docker-compose.yml I mapped an extra_host (this was the secret sauce for me):

services:
  web:
    extra_hosts:
      - "host.docker.internal:host-gateway"

In php.ini:

[xdebug]
xdebug.mode=debug
xdebug.client_host=host.docker.internal

;optionals: (uncomment if you need them)
;xdebug.start_with_request=yes
;xdebug.discover_client_host=1

In PhpStorm I just started listening to port 9003 and configured the server mapping to my needs.

References:

Solution 5

I created a very simple configuration that allows me to use Xdebug with any PHP version with minimal effort (v2: 5.6-7.1, v3: 7.2+). All I need to do is configure PhpStorm and docker-compose.yml in three places and I can debug.

Configuration:

gander/dev @ xdebug2.ini:

zend_extension=xdebug.so
; https://2.xdebug.org/docs/all_settings
; ------------------------------------
; Enables Step Debugging
xdebug.remote_enable=1
; ------------------------------------
; Address where IDE listening for incoming debugging connections
xdebug.remote_host=host.docker.internal
; ------------------------------------
; Port where IDE listening for incoming debugging connections
xdebug.remote_port=9003
; ------------------------------------
; Color var_dumps when in CLI
xdebug.cli_color=1
; ------------------------------------

gander/dev @ xdebug3.ini:

zend_extension=xdebug.so
; https://xdebug.org/docs/all_settings
; ------------------------------------
; Enables Step Debugging
xdebug.mode=debug,develop
; ------------------------------------
; Address where IDE listening for incoming debugging connections
xdebug.client_host=host.docker.internal
; ------------------------------------
; Port where IDE listening for incoming debugging connections
xdebug.client_port=9003
; ------------------------------------
; Color var_dumps when in CLI
xdebug.cli_color=1
; ------------------------------------

gander/dev @ docker-compose.yml:

version: '3.7'

services:
#...
  dev74:
    hostname: 'dev-74'
    container_name: 'dev_74'
    image: 'gander/dev:7.4'
    volumes:
      - './app/xdebug3:/www/localhost'
    working_dir: '/www/localhost/public'
    ports:
      - '8074:80'
    extra_hosts:
      - "host.docker.internal:host-gateway"
    environment:
      PHP_IDE_CONFIG: "serverName=dev.74"
#...

Run via CLI:

XDEBUG_SESSION=1 XDEBUG_CONFIG=1 php script.php

or:

docker-compose exec dev74 bash -c 'XDEBUG_SESSION=1 XDEBUG_CONFIG=1 php index.php'

Screenshots:

xdebug port configuration validation

Share:
40,973
ReynierPM
Author by

ReynierPM

A passionate programmer and web developer with a background in front-end and back-end development, which is what he's been doing for over eight years. I had experience in web development using PHP (mostly), MySQL and JavaScript. I follows two major principles everyday work: beauty and simplicity. I believes everyone should learn something new every day. While I'm not working, I spends time coding personal projects, learning, watching screen casts, blogging, etc. Some specific areas of interest for me include cloud computing and anything related to web development among other like system and database administration.

Updated on October 06, 2021

Comments

  • ReynierPM
    ReynierPM over 2 years

    I would like to try Xdebug 3.0.0RC1 to explore what has changed and the new features that come with it. I am also using the latest PhpStorm 2020.3 EAP which supports Xdebug 3 with no major config needed. Below is my PhpStorm config for the Debugger:

    enter image description here

    And here is the configuration I have tried for Xdebug3:

    zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so
    xdebug.mode=debug
    xdebug.start_with_request=yes
    xdebug.client_host=host.docker.internal # here I tried several combinations like: "localhost", "127.0.0.1", "172.17.0.1"
    xdebug.client_port=9001 # here I tried several ports 9003 included with no success
    

    I have also tried not adding the client_host/client_port setting at all and still failing.

    I am getting this error:

    Script php bin/console doctrine:cache:clear-metadata returned with error code 255
    !!  [17-Nov-2020 15:24:40 UTC] Xdebug: [Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9001 (through xdebug.client_host/xdebug.client_port) :-(
    !!  [17-Nov-2020 15:24:41 UTC] PHP Fatal error:  Method class@anonymous::__toString() must not throw an exception, caught Symfony\Component\DependencyInjection\Exception\AutowiringFailedException:  in /var/www/vendor/symfony/symfony/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php on line 233
    

    Some info about my environment:

    • Fedora 33
    • Docker version 19.03.13, build 4484c46d9d
    • PhpStorm 2020.3 EAP Build #PS-203.5784.36

    It is curious (because apparently host.docker.internal is "not" supported by the Docker version I am using and yet it works) and weird at the same time that the following configuration does work with Xdebug 2 even having the debugger listening for incoming connections all the time:

    enter image description here

    zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so
    xdebug.remote_autostart=0
    xdebug.remote_enable=1
    xdebug.remote_host=host.docker.internal
    xdebug.remote_port=9000
    

    What I am missing here?

    Note: I already applied the solution provided by the Xdebug developer here.