LoginSignup
0
2

More than 1 year has passed since last update.

[2022年版] VSCode+docker-compose+php8.1.4 (xdebug 3.1.4)の環境構築のポイント

Last updated at Posted at 2020-03-29

はじめに

ありふれた記事なのではあるが一発では起動できない。。
なので基本的なところは他の記事に譲るとして、必ずデバッグする為のポイントを上げてみた。

使用するdockerイメージ

FROM php:8.1-apache

1. VSCodeにPHPDebugのインストールとlaunch.jsonの設定

RUN and Debugを起動してlaunch.jsonを適当に作る。

launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "XDebug on docker",
            "type": "php",
            "request": "launch",
            "port": 9005,
            "pathMappings": {
                "/var/www/html/": "${workspaceRoot}/src/"
            }
        }
    ]
}

2. サーバ側にxdebugをインストール

Dockerfileでxdebugをインストールする。

dockerfile
# xdebug
RUN pecl install xdebug && docker-php-ext-enable xdebug

上記を1行追記する。
後ろの
docker-php-ext-enable xdebug
で、docker上に
/usr/local/etc/php/conf.d/docker-php-ext-xdebug.iniが作られる。中身は

docker-php-ext-xdebug.ini
zend_extension=xdebug

だけだがそのままでOK

そしてxdebug.iniを用意する。

xdebug.ini
[xdebug]
xdebug.mode=debug
# IP address or host.docker.internal
xdebug.client_host=host.docker.internal
xdebug.start_with_request=yes
xdebug.discover_client_host=0
xdebug.remote_handler="dbgp"
xdebug.client_port=9005

ここのclient_portとlaunch.jsonのportをあわせる。
client_hostは環境によってローカルのIPアドレスを記述する必要があるので適宜設定する。

xdebug.iniのコピー先に注意。
docker上の
/usr/local/etc/php/conf.dの中に置いてあげる。

docker-compose.yaml
    volumes:
      - './src:/var/www/html'
      - ./docker/php-apache/php/xdebug.ini:/usr/local/etc/php/conf.d/xdebug.ini

3. VSCodeのdebugを起動

あとは、ブレークポイントを設定してアクセスして確認する

4. はまりポイント

  • xdebug.iniのコピーの位置
  • xdebug.client_hostの設定内容。vagrant等の仮想環境を使っている場合はそのIPを指定する。

終わりに

記事が古かったので更新。最新のphp8.1.4とxdebug3.1.4に対応した。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2