Toshio Blog

Remote Debugger With Phpstorm

September 05, 2018

PhpStormでステップ実行でのデバッグをするための解説です。 度々忘れるので記事にします。

動作環境

  • Docker on Mac (Version: 18.06.0-ce-mac70 (26399))
  • PhpStorm (Version: PhpStorm 2018.1.6)

目次

  • Xdebugの設定

    • Xdebugのインストール
    • Xdebugのリモートデバッグ機能の設定
  • PhpStormの設定

    • server項目の設定
    • config項目の設定
    • デバッグ起動

Xdebugの設定

Xdebugをインストール

PHPが動作するコンテナのDockerfileの例

FROM php:7.2-fpm
... 省略
RUN pecl install xdebug && \
docker-php-ext-enable xdebug
... 省略
COPY ./xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
... 省略
EXPOSE 9000
view raw php-fpm-Dockerfile hosted with ❤ by GitHub

Xdebugのリモートデバッグ機能の設定

xdebug.iniの例

xdebug.remote_host=host.docker.internal
xdebug.remote_connect_back=0
xdebug.remote_port=9000
xdebug.idekey=PHPSTORM
xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.cli_color=0
xdebug.profiler_enable=0
xdebug.profiler_output_dir="~/xdebug/phpstorm/tmp/profiling"
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.var_display_max_children=-1
xdebug.var_display_max_data=-1
xdebug.var_display_max_depth=-1
view raw xdebug.ini hosted with ❤ by GitHub

以下のサイトに全ての項目ではありませんが、各項目の分かりやすい説明がありました。 https://qiita.com/castaneai/items/d5fdf577a348012ed8af

xdebug.remote_hostを少し解説します。

デバッグの制御を開始するには、PHP動作側とリモートをつなぐ必要があります。PHP動作側(つまりコンテナ)からホスト(Mac、PhpStorm)に接続したいわけです。

remote_hostに設定する値は、ホストのIPを名前解決する値を指定するわけですが、その値は host.docker.internalとしてdockerが用意してくれています。

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker for Mac.

https://docs.docker.com/docker-for-mac/networking/#i-cannot-ping-my-containers

こちらの値でも接続できるはずですが、そのうち使えなくなります。

  • docker.for.mac.localhost
  • docker.for.mac.host.internal

https://docs.docker.com/docker-for-mac/release-notes/#docker-community-edition-18030-ce-mac59-2018-03-26

PhpStormの設定

Server項目の設定

  1. Phpstorm -> Preferences -> Laguages & Frameworks -> PHP -> Server
  2. Nameは何でもよくて、
  3. Hostはブラウザからアクセスするホスト名を入力して、
  4. Use path mappings ~ にチェックを入れて、
  5. Project Filesの Absolute path on the serverにコンテナ内で配置されているパスを記入します。
  6. Apply -> OK

Config項目の設定

  1. RUN -> Edit Configurations…
  2. +
  3. PHP Remote Debug
  4. Nameは何でもよくて
  5. Serverは「server項目の設定」1で入力したNameが選択できるようになっています。それを選択。(写真ではxdebugとなっていますが、anynameと登録していたらanynameが選択できるようになっているはずです。)
  6. IDE Keyはxdebug.iniで入力したxdebug.idekeyを入力します。
  7. Apply -> OK

デバッグ起動

  1. RUN -> Debug
  2. 「Config項目の設定」3で入力した名前が選択できるようになっています。それを選択。
  3. 起動されたので、適当にブレイクポイントをセットして、ブラウザアクセスで確認です。

以上になります。


Written by Ta Toshio who lives and works in Saitama, Japan .You should follow him on Twitter