目次

Xdebug

インストール

Yum

# yum list \*xdebug\*
# yum install php-pecl-xdebug.i386

PECL

# dnf install php php-devel php-pear
# pecl install xdebug
PHP 7.2-7.4 の場合
# pecl install xdebug-3.1.6
# vi /etc/php.d/user.ini
+zend_extension=/usr/lib64/php/modules/xdebug.so

プロファイリング

下準備

# mkdir /php-xdebug
# chown apache /php-xdebug

/html/.htaccess

Linux の例

php_flag xdebug.profiler_enable 1
php_value xdebug.profiler_output_dir "/php-xdebug"
php_flag xdebug.profiler_append 0
php_value xdebug.profiler_output_name "cachegrind.out.%u_%R"

Windows の場合、xdebug.profiler_output_dir を以下要領で変える。

php_value xdebug.profiler_output_dir "C:\app\php-52-ts\xdebug"

リモートデバッグ

VSCode

クライアント側

サーバー側

PHP 5.4 (Xdebug 2.2.7) (FCGI)

実際に動作した設定。

.user.ini
xdebug.remote_enable = on
xdebug.remote_autostart = on
xdebug.remote_port = 9003

PHP 5.6 (Xdebug 2.5.5)

実際に動作した設定。

.htaccess
php_flag xdebug.remote_enable on
php_flag xdebug.remote_autostart on
xdebug.remote_port = 9003 

PHP 7.4 (Xdebug 2.9.5)

「PHP 5.4 (Xdebug 2.2.7) (FCGI)」同様の設定で動作した。(/etc/php.d/*.ini に記述した。)

PHP 7.4 (Xdebug 3)

実際に動作した設定。

/etc/opt/remi/php74/php.d/user.ini
# xdebug.client_host = "localhost"
# xdebug.client_port=9003
xdebug.mode=develop,debug
.user.ini
xdebug.start_with_request=yes
.htaccess
php_value xdebug.start_with_request yes

設定の記述先について

xdebug.mode

xdebug.start_with_request

Docker 上の PHP をデバッグ

通常との差異。

php.ini

xdebug.client_host = host.docker.internal

.vscode/launch.json

{
    "configurations": [
        {
            "pathMappings": {
                "/var/www/app": "${workspaceRoot}",
            },
        },
    ],
}

別の発火方法

xdebug.remote_autostart や xdebug.start_with_request を使わずデバッグする方法。その方が、多分安全。

WEB

コマンドライン

$ XDEBUG_SESSION=1 ./foo.php
# sudo -u apache XDEBUG_SESSION=1 ./foo.php
$ docker compose exec -T -e XDEBUG_SESSION=1 <サービス名> php

資料

未編集原稿

.user.ini
; for xdebug 2
xdebug.remote_enable = ${PHPFPM_xdebug_remote_enable}
xdebug.remote_autostart = ${PHPFPM_xdebug_remote_autostart}
; for xdebug 3
xdebug.start_with_request = ${PHPFPM_xdebug_start_with_request}

.htaccess
# 開発サイト
# - 別名によるブレを回避するため HTTP_HOST を使わず、SERVER_NAME を使っている。(効果未検証)
<If "%{SERVER_NAME} =~ /^dev\./">
    SetEnv PHPFPM_xdebug_remote_enable on
    SetEnv PHPFPM_xdebug_remote_autostart on
    SetEnv PHPFPM_xdebug_start_with_request yes
</If>
# 本番サイト
<Else>
    SetEnv PHPFPM_xdebug_remote_enable off
    SetEnv PHPFPM_xdebug_remote_autostart off
    SetEnv PHPFPM_xdebug_start_with_request no
</Else>