VPSでPHP Xdebugする

PHP

ひょんなことからVPSでPHP Xdebugしてみたのでメモ。

VPS環境

OS:RockyLinux 8.10
Apache:2.4
PHP:8.2

クライアント環境

エディタ:VSCode
VSCodeプラグイン:PHP Debug、Remote SSH
※Remote SSHでサーバーに接続していること

Xdebugのインストール

dnf install php-xdebug

Xdebugの設定

vi /etc/php.d/15-xdebug.ini

----以下を追加----
xdebug.mode=debug
xdebug.start_with_request = yes
xdebug.client_host=127.0.0.1
xdebug.log = "/var/log/xdebug.log"
xdebug.log_level=7
xdebug.discover_client_host=true
xdebug.client_port=9000

Apache、PHP-FPMの再起動

systemctl restart php-fpm
systemctl restart httpd

VSCode設定

対象のPHPファイルを開き、「F5」をクリックします。
「launch.jsonを開く」をクリックします。
以下をコピペします。

{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "Listen for Xdebug",
            "type": "php",
            "request": "launch",
            "port": 9000,
            "pathMappings": {
                "/var/www/html/": "${workspaceRoot}" # /var/www/html/の部分はPHPのパスを入力
            }
        },
        {
            "name": "Launch currently open script",
            "type": "php",
            "request": "launch",
            "program": "${file}",
            "cwd": "${fileDirname}",
            "port": 9000,
        }
    ]
}

Xdebug実行

VSCodeでブレークポイントを貼り、ページを開きます。
ページを開いた際にブレークポイントの箇所で処理が止まれば完了です。

今回は以上です。