Skip to content

Instantly share code, notes, and snippets.

@t-kuni
Last active March 14, 2023 14:22
Show Gist options
  • Save t-kuni/8cebbd61b43b2c8f6d334d9e54b5d4a2 to your computer and use it in GitHub Desktop.
Save t-kuni/8cebbd61b43b2c8f6d334d9e54b5d4a2 to your computer and use it in GitHub Desktop.
Xdebug 3系 + Docker + PhpStorm環境のxdebugの設定

Xdebug 3系 + Docker + PhpStorm環境のxdebugの設定

(Xdebug 2系は設定が異なるので注意。IDE側の設定も変わっている)

php.iniのxdebugの設定は以下の様にする。 最低限必要なのは以下の通り。

xdebug.mode=debug
xdebug.discover_client_host=true

PhpStormの設定

以下を忘れずに設定する事

  • パスマッピングの設定(ctrl + alt + s -> PHP -> Servers -> Use path mappings ...)
  • デバッグ接続の許可(ウィンドウ左上のアイコン)
  • 待ち受けポート番号を9003に設定する(xdebug2から変わっているので注意)

(TODO 画像)

PhpStormでctrl + alt + sで設定を開く Language & Frameworks -> PHP -> CLI Interpreter(右の...ボタン) -> Additional -> Configuration Option

キー: xdebug.client_host (xdebug2から変わっているので注意) 値: 10.0.2.2(Windows + VirtualBoxの場合) それ以外の環境の場合はhost.docker.internal172.17.0.1でつながるかも。

デバッグの開始方法

デバッグを行うケースは以下3パターンある。

  1. ブラウザからWebサーバに接続する場合
  2. コマンドラインからPHPを実行する場合
  3. phpstormからユニットテストを実行する場合

それぞれ詳しくみていく。

ブラウザからWebサーバに接続する場合

remote_autostartがOFFなので単純に画面を開くだけではデバッグは開始しない

URLのパラメータとしてXDEBUG_SESSION_START=xxxを付けてやる必要がある。

https://ドメイン名/パス?XDEBUG_SESSION_START=xxx

コマンドラインからPHPを実行する場合

単純にコマンドを実行するだけではデバッグは開始しない

以下のように環境変数を設定してからphpコマンドを実行する必要がある

export PHP_IDE_CONFIG="serverName=[Server Name]"
export XDEBUG_CONFIG="idekey=[dummy] client_host=172.17.0.1"
php [デバッグ対象のコマンド]

[Server Name]にはPHPStormのPHP > Serversで定義されているサーバの名前を指定する
[dummy]はたぶんなんでも良い。でも指定しないとデバッガが起動しない。
-d memory_limit=...は無くてもいいが、Killedと表示されて途中で落ちてしまう場合は付与する。

(余談) Laravelのキューワーカーをデバッグする場合

--timeout=0 を付与してタイムアウトを回避する

php artisan queue:work --queue=sync --timeout=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment