Debian 10 (buster) PHP7.1 Xdebug2.9

Xdebugの設置でつまづいたため、とりあえず最小限で試してみる。
(別Ver. Debian 12 (bookworm) PHP8.2 Xdebug3.3 - demandosigno

Xdebug: Documentation » Supported Versions and Compatibility

docker-compose.yml

version: '3'

services:
    php:
        image: php:7.1-apache
        volumes:
            - ./html:/var/www/html
        ports:
            - 8001:80
        container_name: php7.1

docker-compose up -d

# cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 10 (buster)"

# php -v
PHP 7.1.33 (cli) (built: Nov 22 2019 18:28:25) ( NTS )

# apt update && apt upgrade
# service apache2 status
[ ok ] apache2 is running.
# apt install vim
# vim phpinfo.php

# pecl install xdebug
WARNING: channel "pecl.php.net" has updated its protocols, use "pecl channel-update pecl.php.net" to update
pecl/xdebug **requires PHP (version >= 8.0.0, version <= 8.3.99), installed version is 7.1.33**
No valid packages found
install failed

# apt install php-xdebug
Package php-xdebug is not available, but is referred to by another package.
E: Package 'php-xdebug' has no installation candidate

Debian -- Package Search Results -- php-xdebug
php-xdebug パッケージ
buster (oldoldstable) (php): Xdebug Module for PHP
Debian -- Details of package php-xdebug in buster
以下のパッケージによって提供される仮想パッケージです: libapache2-mod-php7.3, libphp7.3-embed, php7.3-cgi, php7.3-cli, php7.3-fpm, php7.3-phpdbg
PHP7.3でないとダメそう。

再度PECLでバージョン付き

# pecl install xdebug-2.9.8
~(中略)~
Build complete.
Don't forget to run 'make test'.

running: make INSTALL_ROOT="/tmp/pear/temp/pear-build-defaultusergbmz49/install-xdebug-2.9.8" install
Installing shared extensions:     /tmp/pear/temp/pear-build-defaultusergbmz49/install-xdebug-2.9.8/usr/local/lib/php/extensions/no-debug-non-zts-20160303/

  +----------------------------------------------------------------------+
  |                                                                      |
  |   INSTALLATION INSTRUCTIONS                                          |
  |   =========================                                          |
  |                                                                      |
  |   See https://xdebug.org/install.php#configure-php for instructions  |
  |   on how to enable Xdebug for PHP.                                   |
  |                                                                      |
  |   Documentation is available online as well:                         |
  |   - A list of all settings:  https://xdebug.org/docs-settings.php    |
  |   - A list of all functions: https://xdebug.org/docs-functions.php   |
  |   - Profiling instructions:  https://xdebug.org/docs-profiling2.php  |
  |   - Remote debugging:        https://xdebug.org/docs-debugger.php    |
  |                                                                      |
  |                                                                      |
  |   NOTE: Please disregard the message                                 |
  |       You should add "extension=xdebug.so" to php.ini                |
  |   that is emitted by the PECL installer. This does not work for      |
  |   Xdebug.                                                            |
  |                                                                      |
  +----------------------------------------------------------------------+

~(中略)~
Build process completed successfully
Installing '/usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so'
install ok: channel://pecl.php.net/xdebug-2.9.8
configuration option "php_ini" is not set to php.ini location
You should add "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so" to php.ini

Xdebug: Documentation » Installation
"Warning: You should ignore any prompts to add "extension=xdebug.so" to php.ini — this will cause problems."
警告: php.iniに "extension=xdebug.so "を追加するよう促されても無視すべきです。

PHPの設定

1. コマンドラインで php --ini を実行し、変更すべきPHPのiniファイルを見つける。

# php --ini
Configuration File (php.ini) Path: /usr/local/etc/php
Loaded Configuration File:         (none)
Scan for additional .ini files in: /usr/local/etc/php/conf.d
Additional .ini files parsed:      (none)

もし、/etc/php/7.4/cli/conf.d/99-xdebug.ini のように xdebug を名前に含むファイルがあれば、このファイルを使用します。
このファイルが存在せず、conf.dや同様のディレクトリに他のファイルがある場合は、そこにも新しいファイルを作成できます。その場合、99-xdebug.iniと命名してください。
そうでない場合は、スクリプトまたはphp --iniコマンドで表示されるphp.iniファイルを修正してください。
php.iniファイルは1つだけではありません。多くのセットアップでは、コマンドライン用(多くの場合cli/php.ini)とウェブサーバー用(多くの場合fpm/php.ini)があります。

2. Add the following line to this PHP ini file:
zend_extension=xdebug

# cd /usr/local/etc/php/conf.d/
# vi 99-xdebug.ini
zend_extension=xdebug
[xdebug]
xdebug.remote_enable=1
;xdebug.remote_host = host.docker.internal // 自分の環境ではこれを記載するとVSCodeを立ち上げた時点でアプリに繋がらなくなった。この辺りの設定詳細がまだ理解できていません。
xdebug.remote_autostart=1
xdebug.remote_port=9003

3. Restart your webserver, or PHP-FPM, depending on what you are using.
しかし再起動後もphpinfoにxdebugが現れない。

# php -v
Failed loading /usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug:  /usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug: cannot open shared object file: No such file or directory
PHP 7.1.33 (cli) (built: Nov 22 2019 18:28:25) ( NTS )

今度はインストール時の
"You should add "zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so" to php.ini" に従ってみる。

# cd /usr/local/etc/php/
# vi php.ini
zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so

# php -v
PHP 7.1.33 (cli) (built: Nov 22 2019 18:28:25) ( NTS )
    with Xdebug v2.9.8, Copyright (c) 2002-2020, by Derick Rethans

代わりに 99-xdebug.ini から以下を削除
zend_extension=xdebug

再起動。動いた。

VSCode側の設定

VSCodeでLocalにインストールした拡張機能PHPdebugをクリックしてコンテナの方にもインストールする。

launch.jsonファイルを作成

VSCode launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9003,
            "pathMappings": {
                "${workspaceRoot}/html": "${workspaceRoot}/html"
            }
        }
    ]
}

デバッグ実行してからブラウザを開き、ブレークポイントで止まるか確認。問題なさそう。

/* -----codeの行番号----- */