wodby/drupal-php

XDebug doesn't work on MAC

fortis opened this issue · 23 comments

XDebug doesn't work on MAC

Works for me:
`
That's how I do it:

  • sudo ifconfig lo0 alias 10.254.254.254 (create an alias for your mac loopback interface)
  • xdebug.remote_host=10.254.254.254 in your xdebug.ini
  • configure your ide as usual.
    `

https://forums.docker.com/t/ip-address-for-xdebug/10460/21

You could try another solution. Just uncomment XDEBUG_CONFIG: xdebug.remote_autostart=1 in the compose file. Also, do not forget to pull all images.

The following way is more convenient because you don't need to manage IPs. It's possible due to xdebug.remote_connect_back = 1 option. More info about this option.

I'm not sure about Mac and Win, tested in Linux only.

I tried, but it doesn't work on recent docker release (it worked with docker-machine).
Also we should specify server_name in nginx configuration, now it's empty and path mapping doesn't work on PhpStorm Zero-configuration Debugging https://confluence.jetbrains.com/display/PhpStorm/Zero-configuration+Web+Application+Debugging+with+Xdebug+and+PhpStorm
1808021507

1809061507

i will provide PR soon

Added PR, but xdebug works only with lo0 alias for now.
I will try to find solution with remote connect back.

@PavelPrischepa as I see it, xdebug.remote_connect_back = 1 is already in?

And I didn't see there is comented option XDEBUG_CONFIG: in docker-compose.yml? (although it already seems there?)

However, what I am curious about is: if PHP is bound to 9000 port, how can Xdebug use the same? As php-fpm is not on socket, Xdebug should be configured to use other port, 9090 or whatever, just not the same.

Additionally, it seems that drupal7.conf is just too restrictive, I could not use PHPStorm to validate Xdebug

Hi @macmladen,

And I didn't see there is comented option XDEBUG_CONFIG: in docker-compose.yml

This approach is deprecated. You should check the updated doc about Xdebug configuration.

if PHP is bound to 9000 port, how can Xdebug use the same?

Xdebug doesn't bound any ports. It sends a debug info to HTTP Client IP:9000. Usually, it's your local machine where PhpStorm (or something else) bounds the port 9000. PHP-FPM bounds port 9000 too, but in the container, so they don't interfere with each other.

@fortis I'd love if you can share the exact steps. I tried with loopback but that produced the same results and it is expected due to xdebug.remote_connect_back = 1 (as far as I can understand, @PavelPrischepa is right about that, it should be the same without need for explicit IP).

@PavelPrischepa I am not sure this is Mac specific. I tried to follow everything in strict order and then also tried to manipulate configuration to get that through but I failed.

So I did fetch latest docker-compose.yml and merged everything from that into my configuration.

docker-compose

After that, I pulled latest images with docker-compose pull

Everything was started OK by docker-compose up -d

docker-ps

NOTE: take a look at port allocation, port 9000 is reserved for php container. Also in docker discussion @fortis mentioned they also warn about ports. And that all makes sense to me as all containers are bound together on local host so container as process can listen only on available ports just like any other process on local system. Similarly, exposing nginx 9000 port as 9090 port fails as port is already in use by php container.

But when I try to validate, I get:

phpstorm-dxebug-validate

As drupal7.conf is very restrictive, that doesn't surprise me. No document is readable, not CHANGELOG.txt but also not other .php except index.php, install.php, cron.php or update.php.

It works in Linux

I have also a Linux machine (Linux Mint Debian Edition, Betsy 2) where I work and experiment in Linux and I can confirm that it is working there just out of the box.

I used the same docker-compose.yml file, used drush to pull drupal and just opened directory in PHPStorm as I do for any project.

Without any setting and tweaking, I just set breakpoint in index.php and as soon as I reloaded a page, PHPStorm informed me on incoming connection and it worked as expected. I even did not initiate xdebug in browser (firefox) it fired instantly.

On the other hand, validation did not work either because of nginx drupal configuration so no surprise there.

...and also on Mac

Then I made exact same steps on Mac and, of course, it did not work.

I entered php container,

/var/www/html # vi /etc/php5/conf.d/xdebug.ini 

added and changed

xdebug.remote_host = 10.254.254.254
xdebug.remote_connect_back = 0

which is very important because when xdebug.remote_connect_backxdebug.remote_connect_back is active, xdebug.remote_host is ignored (as is explained in Xdebug documentation).

After this change you need to exit the container and restart service:

/var/www/html # exit
mladen@Buk4 $ docker-compose restart

It will not validate in PHPStorm but it will react on breakpoint.

So this works on Mac with these changes although I have no explanation why it does not work like on Linux.

IMPORTANT

This will work only while containers are not recreated (machine rebooted or containers removed with docker-compose down).

In that case, you have to repeat the change and restart.

@macmladen

NOTE: take a look at port allocation, port 9000 is reserved for PHP container.

Yes, it bounded by PHP-FPM on the IP of the PHP container in a subnetwork of the current docker-compose project. So, it does not bound on the localhost of the host machine.

XDebug does not bound 9000, here is a nice figure about it.

xdebug.remote_host = 10.254.254.254
xdebug.remote_connect_back = 0

It's great solution. Thank you.

This will work only while containers are not recreated

Try the following way to make changes persistent. You can override xdebug settings via the env var XDEBUG_CONFIG in the PHP container. Like that:

  php:
    image: wodby/drupal-php:7.0 # Allowed: 7.0, 5.6.
    environment:
      PHP_SITE_NAME: dev
      PHP_HOST_NAME: localhost:8000
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      PHP_XDEBUG_ENABLED: 0 # Set 1 to enable.
      XDEBUG_CONFIG: "remote_host = 10.254.254.254 remote_connect_back = 0"
    volumes:
      - ./:/var/www/html

More deatils and examples:

I figured out the port thing, actually port 9000 for Xdebug is on nginx part so they manage to live along.

I tried solution you suggested before but I did not get that to work.

Here is configuration

  php:
    image: wodby/drupal-php:5.6 # Allowed: 7.0, 5.6.
    environment:
      PHP_SITE_NAME: dev
      PHP_HOST_NAME: localhost:8000
      PHP_DOCROOT: drupal # Relative path inside the /var/www/html/ directory.
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      PHP_XDEBUG_ENABLED: 1 # Set 1 to enable.
      XDEBUG_CONFIG: "remote_host = 10.254.254.254 remote_connect_back = 0"
    volumes:
      - ./:/var/www/html
      - ./docker-runtime/drush:/root/.drush

And here is what PHP says:

screenshot 2016-09-23 09 58 33

Lower in Environment section there are options:

screenshot 2016-09-23 10 23 48

And in _SERVER section there are options:

screenshot 2016-09-23 10 04 24

And xdebug is not triggering session.

My solution

I wrote small script so when I start the docker, enter the php, execute script, restart and xdebug is listening and PHPStorm reacting (with ifconfig previously set).

#!/usr/bin/env sh

# https://github.com/Wodby/drupal-php/issues/1
# Fix xdebug in conatiner php:
#   - disable xdebug\.remote_connect_back = 0 so it listen to particular host
#   - declare host xdebug.remote_host = 10.254.254.254
# Mac loopback interface should be patched to listen
#   sudo ifconfig lo0 alias 10.254.254.254
sed  -i \
     -e "s/^xdebug\.remote_connect_back = 1$/xdebug.remote_connect_back = 0/" \
     -e "/xdebug.remote_host = 10.254.254.254/d" \
     -e "$ a xdebug.remote_host = 10.254.254.254" \
     /etc/php5/conf.d/xdebug.ini

but custom xdebug.ini sounds like a better solution as in wodby/docker4drupal#27

However, configuration in docker-compose.yml is the best solution but this way it doesn't seem to work.

+1 It's not working

Yes, it's not working because of remote_connect_back = 1 in the xdebug config file. It's not overriding via env var.

So, I've implemented the following env vars for xdebug configuration:

      PHP_XDEBUG_AUTOSTART: 1
      PHP_XDEBUG_REMOTE_CONNECT_BACK: 1
      PHP_XDEBUG_REMOTE_HOST: "localhost"

P.S. Don't forget to pull the PHP image.

@PavelPrischepa I can confirm that this solved the issue for Mac users, Xdebug is working in PHPStorm as expected once PHP container has

      PHP_XDEBUG_REMOTE_CONNECT_BACK: 0
      PHP_XDEBUG_REMOTE_HOST: "10.254.254.254"

and of course on Mac, locally local loopback has

sudo ifconfig lo0 alias 10.254.254.254

@fortis Please change title as MAC refers to media access control address (MAC address), use macOS or Mac OS X ;)

Hi all, thanx for good work.
Could you add all steps for configurate xdebug on MacOS, to Readme txt of docker4drupal project?

I think the best approach would be to have explanation in README.md and all lines activated but commented out:

...
  php:
    image: wodby/drupal-php:7.0 # Allowed: 7.0, 5.6.
    environment:
      PHP_SITE_NAME: dev
      PHP_HOST_NAME: localhost:8000
#      PHP_DOCROOT: public # Relative path inside the /var/www/html/ directory.
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      # PHP_XDEBUG_ENABLED: 1
      # PHP_XDEBUG_AUTOSTART: 1
      # PHP_XDEBUG_REMOTE_CONNECT_BACK: 0         # This is needed to respect remote.host setting bellow
      # PHP_XDEBUG_REMOTE_HOST: "10.254.254.254"  # You will also need to 'sudo ifconfig lo0 alias 10.254.254.254'
    volumes:
      - ./:/var/www/html
...

That way, user has only to uncomment what is needed and still have proper line included with least effort.

I created a pull request in wodby/docker4drupal#45

I've merged the PR wodby/docker4drupal#45.

I'm going to close the issue due no activity.

Hi there,

Sorry to reopen this issue but I'm not able to make PHPStorm connecting to the Xdebug like you do guys. I have tested several times but it's always the same results with my current project, it does not work. I'm using the latest version of the docker4drupal code. I have followed the Readme description and defined.

To ensure it does not come from my project, I have re-started from zero. Cloned docker4drupal, created an application folder inside and unzip the latest Drupal file. I have updated docker-compose.yml to map my application directory to /var/www/html, followed the special Xdebug Mac instruction and added the netword alias

version: "2"

services:
  mariadb:
    image: wodby/drupal-mariadb
    environment:
      MYSQL_RANDOM_ROOT_PASSWORD: 1
      MYSQL_DATABASE: drupal
      MYSQL_USER: drupal
      MYSQL_PASSWORD: drupal
#    command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci # The simple way to override the mariadb config.
    volumes:
      - ./docker-runtime/mariadb:/var/lib/mysql
#      - ./docker-runtime/mariadb-init:/docker-entrypoint-initdb.d # Place init .sql file(s) here.

  php:
    image: wodby/drupal-php:7.0 # Allowed: 7.0, 5.6.
    environment:
      PHP_SITE_NAME: Drupal-dev
      PHP_HOST_NAME: localhost:8008
      PHP_SENDMAIL_PATH: /usr/sbin/sendmail -t -i -S mailhog:1025
      PHP_XDEBUG_ENABLED: 1
      PHP_XDEBUG_AUTOSTART: 1
      PHP_XDEBUG_REMOTE_CONNECT_BACK: 0         # This is needed to respect remote.host setting bellow
      PHP_XDEBUG_REMOTE_HOST: "10.254.254.254"  # You will also need to 'sudo ifconfig lo0 alias 10.254.254.254'
#   PHP_DOCROOT: public # Relative path inside the /var/www/html/ directory.
    volumes:
      - ./application:/var/www/html

  nginx:
    image: wodby/drupal-nginx
    environment:
      NGINX_SERVER_NAME: localhost
      NGINX_UPSTREAM_NAME: php
#      NGINX_DOCROOT: public # Relative path inside the /var/www/html/ directory.
      DRUPAL_VERSION: 7 # Allowed: 7, 8.
    volumes_from:
      - php
    ports:
      - "8008:80"

  mailhog:
    image: mailhog/mailhog
    ports:
      - "8002:8025"

I have added a breakpoint into the index.php of Drupal, enabled PHPStorm debugger and loaded the index.php. On the browser I have been redirected to the install.php page (which is normal) but PHPStorm did not displayed to me the Incoming connections modal.

Have you guys done a specific configuration in PHPStorm to make it works ?

Sometime just changing one things can make it works... Just changed the php version of the PHP container and PHPStorm succeeded to intercept the connection.

From
image: wodby/drupal-php:7.0
to
image: wodby/drupal-php:5.6

I think something does not work with the version 7.0. Is it possible that Env variables are not used ? I can see them when running a phpinfo()...

Can not make it work in Linux.
I open a new issue.