ServBay内蔵のXdebugモジュールの有効化方法
ServBayは強力な統合Web開発ツールであり、Xdebugモジュールが内蔵されており、その有効化手順は非常に簡単です。XdebugはPHPのデバッグおよび解析ツールで、開発者がコードのデバッグ、パフォーマンス解析、コードカバレッジの検出を行うのに役立つ、PHPアプリケーションの開発と保守に欠かせないツールです。
Xdebugモジュールの概要
XdebugはPHPコードのデバッグと解析のための強力な拡張機能です。ブレークポイント調整、スタックトレース、変数ウォッチなど、豊富なデバッグ機能を提供します。さらに、Xdebugはパフォーマンス解析とコードカバレッジ検出もサポートし、開発者がコード性能を最適化し、コード品質を保証するのに役立ちます。
主な特徴
- ブレークポイントデバッグ:Xdebugはコード内にブレークポイントを設定し、行ごとにコードをデバッグすることができます。
- スタックトレース:エラー発生時に詳細なスタックトレース情報を生成し、問題の迅速な特定をサポートします。
- パフォーマンス解析:詳細なパフォーマンスレポートを生成し、コードのパフォーマンス最適化を支援します。
- コードカバレッジ:コードカバレッジ検出をサポートし、テストがすべてのコードパスをカバーしていることを確認します。
- 統合開発環境サポート:PHPStorm、Visual Studio Codeなどのさまざまな統合開発環境(IDE)と統合し、便利なデバッグ体験を提供します。
ServBay内蔵のXdebugモジュールのバージョン
ServBayは複数のPHPバージョンをサポートしており、それぞれに対応するXdebugモジュールがプレインストールされています。具体的なバージョンは以下の通りです:
- PHP 5.6、7.0、7.1:Xdebug 2.5.5
- PHP 7.2、7.3、7.4:Xdebug 3.1.6
- PHP 8.0、8.1、8.2、8.3:Xdebug 3.3.1
- PHP 8.4:Xdebug 3.4.0-dev
提示
各PHPバージョンのXdebugポートは独立しており、バージョン番号に関連付けられています。例えば、PHP 7.2
のXdebugポートは39072
、PHP 8.3
のXdebugポートは39083
です。
Xdebugモジュールの有効化方法
デフォルトでは、Xdebugモジュールは無効化されています。Xdebugモジュールを有効化する手順は非常に簡単で、対応するPHPバージョンの構成ファイルを編集するだけです。以下に詳細な手順を示します。
ステップ1:構成ファイルの見つけ方
まず、対応するPHPバージョンのconf.d
ディレクトリを見つけます。例えば、PHP 8.3のXdebugモジュールを有効にするには、次のファイルを編集します:
/Applications/ServBay/etc/php/8.3/conf.d/xdebug.ini
ステップ2:構成ファイルの編集
xdebug.ini
ファイルを開き、以下の内容のコメントを解除します:
[Xdebug]
; Uncomment the following line to enable XDebug
zend_extension = xdebug.so
2
3
ステップ3:Xdebugパラメータの設定
Xdebugの機能を最大限に活用するため、xdebug.ini
でいくつかの一般的なパラメータを設定できます。例えば:
xdebug.mode=debug,develop
xdebug.start_with_request=yes
xdebug.client_host=localhost
xdebug.client_port=39083
xdebug.log=/Applications/ServBay/logs/xdebug/8.3/xdebug.log
2
3
4
5
ステップ4:PHPサービスの再起動
ServBayのサービス管理パネルで、対応するPHPサービスを再起動します。例えば、PHP 8.3のサービスを再起動します。再起動後、Xdebugモジュールが正常に読み込まれます。
Xdebugモジュールが正常に読み込まれたかの確認方法
簡単なPHPファイルを作成して、Xdebugモジュールが正常に読み込まれたことを確認できます。Webサーバーのルートディレクトリにphpinfo.php
ファイルを作成し、次の内容を記載します:
<?php
phpinfo();
?>
2
3
https://servbay.host/phpinfo.php
にアクセスし、出力されるPHP情報ページでXdebugモジュールの関連情報を探します。Xdebugに関する情報が表示されていれば、モジュールは正常に読み込まれています。
PHPStormでXdebugを設定する
PHPStormでXdebugを使用してデバッグを行うには、以下の手順に従って設定する必要があります:
ステップ1:PHPStormを開き、設定に入る
- PHPStormを開きます。
- メニューバーの
File
をクリックし、Settings
(Windows/Linux)またはPreferences
(macOS)を選択します。
ステップ2:PHPインタープリターを設定する
- 設定ウィンドウで、
Languages & Frameworks
->PHP
に移動します。 CLI Interpreter
部分で、右側の...
ボタンをクリックします。- ポップアップウィンドウで、
+
ボタンをクリックして新しいPHPインタープリターを追加し、Local
を選択します。 - PHPインタープリターパスをServBay中の対応するPHPバージョンのパスに設定します。例:
/Applications/ServBay/package/php/8.3/8.3.7/bin/php
。 OK
をクリックして設定を保存します。
ステップ3:Xdebugデバッグを設定する
提示
各PHPバージョンのXdebugポートは独立しており、バージョン番号に関連付けられています。例えば、PHP 7.2
のXdebugポートは39072
、PHP 8.3
のXdebugポートは39083
です。
- 設定ウィンドウで、
Languages & Frameworks
->PHP
->Debug
に移動します。 Xdebug
部分で、Debug Port
を39083
に設定し、xdebug.client_port
の設定と一致させます。Can accept external connections
オプションがチェックされていることを確認します。Apply
をクリックし、その後OK
をクリックして設定を保存します。
ステップ4:PHP Webサーバーを設定する
- 設定ウィンドウで、
Languages & Frameworks
->PHP
->Servers
に移動します。 - 右側の
+
ボタンをクリックして新しいサーバー設定を追加します。 - サーバー名を設定します。例:
ServBay Localhost
。 Host
をServBayホストに対応するホストに設定し、例:servbay.new
。ポートを443(HTTPS)に設定します。Debugger
をXdebug
に設定します。OK
をクリックして設定を保存します。
ステップ5:デバッグセッションを開始する
- PHPStormでデバッグしたいPHPプロジェクトを開きます。
- コード内にブレークポイントを設定します(行番号の左側の空白部分をクリックします)。
- PHPStormの上部ツールバーの
Start Listening for PHP Debug Connections
ボタン(小さな虫のアイコン)をクリックします。 - ブラウザでPHPアプリにアクセスすると、Xdebugが自動的にPHPStormに接続し、ブレークポイントで実行が停止します。
結論
ServBayは、Xdebugモジュールを管理および有効化するための便利な手段を提供します。簡単な設定と再起動操作により、開発者は異なるPHPバージョンでXdebugモジュールを迅速に有効化でき、その強力なデバッグおよび解析機能を最大限に活用することができます。Xdebugのブレークポイントデバッグ、パフォーマンス解析、およびコードカバレッジ検出機能は、PHP開発者にとって不可欠なツールです。