LoginSignup
7
16

More than 5 years have passed since last update.

MacOSでPHP7.2にxdebugを組み込みデバッグする

Posted at

brew php56-xdebugが使えなくなっていたので、調べ直しました。

MacOSにははじめからapache2+php7.1がインストールされていますが、それを使った場合はうまくいきませんでした。そのため、nginx+php-fpmという組み合わせで動かします。

xdebugのインストールには、MacOSの場合pecl(pear)を使うことが推奨されていたため、そちらを利用します。

brew install php@7.2
pecl install xdebug
brew install nginx

この場合、設定ファイルは以下になります。

  • php: /usr/local/etc/php/7.2/php.ini
  • nginx: /usr/local/etc/nginx/nginx.conf
/usr/local/etc/php/7.2/php.ini
zend_extension="xdebug.so"
xdebug.remote_enable=1
xdebug.remote_autostart=1
xdebug.remote_port="9001"
xdebug.profiler_enable=0
xdebug.profiler_output_dir="/tmp"
xdebug.max_nesting_level=1000
xdebug.idekey = "PHPSTORM"
[PHP]

...

1行目に大胆にも設定が追加されています。その後ろにdebugの設定を追加します。VSCodeを使った場合、リモート接続として設定しなければ動作しませんでした。

続いて、nginxで.phpをphp-fpmに流すようにします。

/etc/nginx/etc/nginx/nginx.conf
http {
    ...
    server {
        listen       8080;
        server_name  localhost;

        location / {
            #root   html;
            root   /Users/nnyn/Documents/vscode-debug-specs/php;            
            index  index.html index.htm;
        }

        location ~ \.php$ {
            root           /Users/nnyn/Documents/vscode-debug-specs/php;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  $document_root/$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

これらをサービスとして起動します。

brew services start php@7.2
brew services start nginx

ログは以下に出力されます。

  • php: /usr/local/var/log/php-fpm.log
  • nginx: /usr/local/var/log/nginx/

phpinfo.php を置いて、http://localhost:8080/ にアクセスします。

phpinfo.php
<?php phpinfo(); ?>

xdebugの項目があれば正しくxdebugが組み込まれています。

Screen Shot 2018-07-28 at 16.26.18.png

あとは、VSCodeなど自分のエディタでデバッグ!

つまずいたところ

  • phpが別の場所にもインストールされていて、xdebugをビルドした際にそちらを見ていた→phpizeを実行し、目的のphpにパスが通っているか確認する。php@7.1はうまく行かず、php@7.2ではpeclがインストールされて、prcl経由でインストールできた。

VSCode側の設定

7
16
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
7
16