A bash script to run php commands with XDebug enabled.

I’m a big fan of XDebug. But it really slows down the PHP native tools I frequently use: Composer, Symfony console commands, phpunit, etc. So, I don’t leave it installed all the time on my development system. Instead I use this bash script to enable XDebug just when I want to debug:

<br />
#!/bin/bash<br />
(<br />
    PHP=${PHP:-/usr/bin/php}<br />
    XDEBUG_SO=${XDEBUG_SO:-/usr/lib64/php/modules/xdebug.so}<br />
<p>    nc -z ${XDEBUG_HOST} ${XDEBUG_PORT}</p>
<p>    if [ &quot;$?&quot; -ne 0 ]<br />
    then<br />
    echo &quot;No listener at ${XDEBUG_HOST} ${XDEBUG_PORT}&quot;<br />
        exit<br />
<p>    export XDEBUG_CONFIG=&quot;idekey=${IDE_KEY} remote_host=${XDEBUG_HOST} remote_port=${XDEBUG_PORT}&quot;</p>
<p>    ${PHP} \<br />
        -didekey=${IDE_KEY} \<br />
        -dzend_extension=${XDEBUG_SO} \<br />
        -dxdebug.remote_enable=1 \<br />
        -dxdebug.remote_mode=req \<br />
        -dxdebug.remote_port=${XDEBUG_PORT} \<br />
        -dxdebug.remote_host=${XDEBUG_HOST} \<br />
          &quot;$@&quot;<br />
)<br />

For example:

<br />
xdebug yabot.php<br />

The script also uses netcat (nc) to check that I have something listening at XDEBUG_HOST:XDEBUG_PORT and warns if I haven’t started my debug client (e.g. phpstorm) listening for connections.

There is one case where this approach doesn’t work: when php is being run by a web server (e.g. httpd or php-fpm).

My earlier blog post Laravel, Valet, and XDebug shows a quick work around for that case..

Comments are closed.