Debugging

How to set up debugging with PhpStorm and Homestead

Daniel Verner -

Many PHP developers rely on var_dump (or it’s reincarnations in frameworks: debug(), dump(), dd(), etc) when debugging their code, and in most cases, this is just fine, but not always good enough. When trying to solve complex problems or fix a stubborn bug, the great debugging capability of PhpStorm comes handy. It is quicker and more efficient to debug the code than to put dumps here and there.

Let’s take a look at what is necessary to set up the debugging:

  • PhpStorm
  • Homestead (or any other Vagrant VM)
  • Xdebug extension

In the code examples please change paths according to your PHP version, I am using PHP 7.2.

Set up Xdebug

If you don’t have xdebug installed please find installation instructions here: https://xdebug.org/docs/install
Open the Xdebug configuration with your favorite editor, in the example I’ll use nano:
sudo nano /etc/php/7.2/fpm/conf.d/20-xdebug.ini
Add/change the following settings in the xdebug configuration
zend_extension=xdebug.so
xdebug.remote_enable = 1
xdebug.remote_connect_back = 1
xdebug.remote_port = 9000
xdebug.max_nesting_level = 512
Save the file, and restart PHP fpm:
sudo service php7.2-fpm restart
If everything has been set up correctly, you should see the Xdebug extension installed and enabled in the output of the phpinfo():

Set up PhpStorm

In the menu choose Run/Edit Configurations, and add new PHP Web Page configuration:

Add a name to the configuration, and fill in the Start url and choose the browser and server from the dropdown options:

If the server dropdown is empty you’d need to add a new server. The if you are using the default Vagrant configuration the ip address of the server is: 192.168.10.10. Check the Use path mapping checkbox, and set up the Absolute path of the project in the Homestead, the default location in homestead is /home/vagrant/code/project (I am using www instead of code)

That’s it! Clicking on the debug icon on the top right corner, the browser will open the project’s URL and the debug session will start. You can put breakpoints by clicking on the left-hand side of the code beside the line numbers:

Happy debugging!

Tags: Homestead · xdebug · Debugging

Want products news and updates?

Sign up for our newsletter to stay up to date.

We care about the protection of your data. Read our Privacy Policy.

Impressions from our Team

  • Another day another #mandarinacakeshop 🎂 😀 - #

  • Happy Birthday Ognjen! And marry Christmas to all other 🎄#notacakeshop - #

  • #Office #Garden - #

  • #workhard - #

  • #belgrade #skyline - #

  • #happybirthday Phil :) - #

  • #happybirthday Stefan 🥂 - #

  • #happybirthday Lidija 🍾 - #

  • Say hi 👋 to our newest team member ☕️ - #

  • #bithday #cake 😻 - #

  • #stayathome #homeoffice #42coders - #

  • #stayathome #homeoffice #42coders #starwars :) - #

  • #stayathome #homeoffice #42coders - #

  • We had a really nice time with #laracononline #laravel - #

  • Happy Birthday 🎂 Miloš - #

  • Happy Birthday 🎂Nikola - #

  • #42coders #christmas #dinner what a nice evening :) - #

  • Happy Birthday 🎂 Ognjen - #

  • Wish you all a merry Christmas 🎄🎁 - #

  • #happybirthday Luka! - #

© 2021 42coders All rights reserved.