Debugging Laravel Homestead Applications with PHPStorm

Debugging PHP applications can be anything but easy. If you have, in the past, like me, found yourself resorting to echo, print_r and var_dump and find yourself using PHPStorm, then this article is for you.

In this article, I’ll show you how to configure PHPStorm to debug your Laravel applications running on Homestead (Laravel’s pre-packaged Vagrant box).

Homestead comes with xdebug pre-installed. Visit the official xdebug docs if you don’t have it installed.

Verify that xdebug is installed and loaded

SSH into your virtual machine (VM) and run the line below

php -m 

Confirm that xdebug is listed under [PHP Modules]
 Confirm that Xdebug is listed under [Zend Modules]

Configure php.ini

Edit xdebug.ini file in conf.d directory. Run the following command from your virtual machine (VM) replacing [version] with your PHP version.

cd /etc/php/php[version]/fpm/conf.d 
Example: cd /etc/php/php7.0/fpm/conf.d

To determine your PHP version, you can run the command below

php -v

Add the following lines to 20-xdebug.ini in the conf.d directory to enable xdebug and allow remote connections

zend_extension = xdebug.so
xdebug.remote_enable = 1
xdebug.remote_connect_back = 1
xdebug.remote_port = 9000

Restart the server. Replace [version] with your PHP version.

sudo service php[version]-fpm restart 
Example: sudo service php7.0-fpm restart

Configure PHPStorm

Open PHPStorm -> Preferences -> PHP

Select the language level (Mine is PHP7)

Click the … (pointed to by yellow arrow) to open a dialog for adding a new interpreter

Click … pointed to by yellow arrow to open dialog for adding new interpreter

Click + in the upper left corner of the dialog and select Remote from the dropdown

Select Vagrant in the resulting dialog window and using the … option specify the path to your local Homestead. It’s usually

/Users/[name]/Homestead
where [name] is the name of your home directory

NB: The Vagrant host url will be autopopulated after step (e) above

Click + button and select remote to add a remote interpreter

Open PHPStorm -> Preferences -> PHP -> Servers

Debug servers dialog

Click + to add a new server (the server PHPStorm will use for debugging). Be sure to map your project to the corresponding directory on the remote (virtual machine) server. Remote path is usually

/home/vagrant/Code/[project-name]
Adding new debug server

Install xdebug helper extension from chrome dev tools

Click the xdebug icon and select Debug

From PHPStorm menu select Run -> Start Listening for PHP Debug Connections

Set a breakpoint anywhere in your codebase

Reload the app from the browser and observe as PHPStorm starts a new debug session

Additional Resources

http://blog.elenakolevska.com/debugging-laravel-on-homestead/