Setup Xdebug with PHP7.2, PHPStorm and Laravel Valet
There is a newer post for PHP7.4, Xdebug3 and PHPStorm 2020.3 here
This walkthru assumes that you have Laravel Valet, PHP7.2 installed. If not, follow these instructions: https://laravel.com/docs/5.8/valet#installation
Xdebug is no longer available from Homebrew as shown in tutorials for PHP7.1, we are going to use PECL instead.
Install Xdebug
-
Prepare Pecl
pecl channel-update pecl.php.net ; pecl clear-cache
-
Install Xdebug
pecl install xdebug
-
Restart Laravel Valet
valet restart
Let’s see what we got so far:
-
First check Xdebug is installed:
php -m
-
Check the paths to ini-files
php --ini
Your output should look something like this:
Configuration File (php.ini) Path: /usr/local/etc/php/7.2
Loaded Configuration File: /usr/local/etc/php/7.2/php.ini
Scan for additional .ini files in: /usr/local/etc/php/7.2/conf.d
Additional .ini files parsed: /usr/local/etc/php/7.2/conf.d/ext-opcache.ini,
/usr/local/etc/php/7.2/conf.d/php-memory-limits.ini
If you don’t see a ext-xdebug.ini
which sometimes can be gone missing, just create one:
touch /usr/local/etc/php/7.2/conf.d/ext-xdebug.ini
- Enter this to your
ext-debug.ini
[xdebug]
zend_extension="/usr/local/opt/[email protected]/pecl/20170718/xdebug.so"
xdebug.remote_autostart=1
xdebug.default_enable=1
xdebug.remote_port=9001
xdebug.remote_host=127.0.0.1
xdebug.remote_connect_back=1
xdebug.remote_enable=1
xdebug.idekey=PHPSTORM
The default port is 9000, but I am going to use 9001 since I have a Elastic Search server running on port 9000.
-
Open
/usr/local/etc/php/7.2/php.ini
and comment out any mentions of Xdebug. The PECL-installer will add a line this by default on installation;zend_extension="xdebug.so"
-
Next restart Laravel Valet
valet restart
PHPStorm
Next we are going to configure PHPStorm to listen for Xdebug.
Open PHPStorm settings (cmd-,)
Languages & Frameworks > PHP > Debug > DBGp Proxy
Add PHPSTORM as IDE Key and port 9001
Next, open Languages & Frameworks > PHP > Debug
Make sure Xdebug is listening to port 9001 and ”Can accept external connections” is checked.
Uncheck to two first ”Force break…”-options, otherwise you will always start debugging on Laravel Valet’s server.php-file
Click ”Start listening for debug connections”
Add a breakpoint and reload your page and it should hopefullt work :-)
Done!
Troubleshooting
Stops working after an update
If you get similar errors:
Cannot load Xdebug - it was already loaded
Check if your php.ini-file contains zend_extension="xdebug.so"
remove this line. The PECL-installer can somethimes add this during upgrades.