Skip to content

Instantly share code, notes, and snippets.

@appkr
Last active May 28, 2019 15:22
Show Gist options
  • Save appkr/c71c85baeef117644e90a294e87590c6 to your computer and use it in GitHub Desktop.
Save appkr/c71c85baeef117644e90a294e87590c6 to your computer and use it in GitHub Desktop.
How to use Xdebug Debugger for a PHP CLI project(eg. PHPUnit)

1 Install PHP 7.1

$ brew instal [email protected]
$ php -v
# PHP 7.1.29 (cli) (built: May 21 2019 20:05:17) ( NTS )
# Copyright (c) 1997-2018 The PHP Group
# Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
#     with Zend OPcache v7.1.29, Copyright (c) 1999-2018, by Zend Technologies

2 Weird Bug

$ php -i | grep extension_dir
# extension_dir => /usr/local/lib/php/pecl/20160303 => /usr/local/lib/php/pecl/20160303

$ pear config-show | grep ext_dir
# PHP extension directory        ext_dir          /usr/local/lib/php/pecl/20160303

Seems all good, but this will fail.

$ pecl install -f xdebug
# Build process completed successfully
# Installing '/usr/local/Cellar/[email protected]/7.1.29/pecl/20160303/xdebug.so'
# ERROR: failed to mkdir /usr/local/Cellar/[email protected]/7.1.29/pecl/20160303

3 Workaround

Make the folder that xdebug wants and make the folder to be writable.

$ sudo mkdir -p /usr/local/Cellar/[email protected]/7.1.29/pecl/20160303
$ sudo chmod 777 /usr/local/Cellar/[email protected]/7.1.29/pecl/20160303

Install again.

$ pecl install -f xdebug
# Build process completed successfully
# Installing '/usr/local/Cellar/[email protected]/7.1.29/pecl/20160303/xdebug.so'
# install ok: channel://pecl.php.net/xdebug-2.7.2
# Extension xdebug enabled in php.ini

4 Wiring

We are going to make separate ini file.

First find the ini file and fix it.

php --ini
# Configuration File (php.ini) Path: /usr/local/etc/php/7.1
# Loaded Configuration File:         /usr/local/etc/php/7.1/php.ini
# Scan for additional .ini files in: /usr/local/etc/php/7.1/conf.d
# Additional .ini files parsed:      /usr/local/etc/php/7.1/conf.d/ext-opcache.ini

Open the Loaded Configuration File and remove the xdebug line like the following.

$ sudo vim /usr/local/etc/php/7.1/php.ini
-zend_extension="xdebug.so"
 [PHP]

 ;;;;;;;;;;;;;;;;;;;
 ; About php.ini   ;
 ;;;;;;;;;;;;;;;;;;;

Now let's create a separate ini file for xdebug.

$ sudo vim /usr/local/etc/php/7.1/conf.d/ext-xdebug.ini
+zend_extension=xdebug.so
+xdebug.idekey=IDEA
+xdebug.remote_enable=1
+xdebug.remote_host=host.docker.internal
+xdebug.remote_port=10001

4 Done

You see with Xdebug v2.7.2?

$ PHP 7.1.29 (cli) (built: May 21 2019 20:05:17) ( NTS )
# Copyright (c) 1997-2018 The PHP Group
# Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
#     with Zend OPcache v7.1.29, Copyright (c) 1999-2018, by Zend Technologies
#     with Xdebug v2.7.2, Copyright (c) 2002-2019, by Derick Rethans

1 Set Xdebug

Enable the xdebug like the following.

image here

2 Set IDE

Turn 'Start listening for PHP Debug Connections' on.

image here

3 Make a Breakpoint & Run Debug

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment