Monday, July 7, 2014

Web and Cli Debugging with PhpStorm, XDebug in Vagrant

We've a Vagrant box in a development environment and want to debug our application. Take a quick review of the two main ways, so it can be either a web app or a cli based app.


Web app debugging
First, we have to create a Server configuration:

File -> Settings -> Php -> Servers

Server configuration

Add a new server with its ip address (or hostname) and set the desired port. Check "Use path mappings" and map the vagrant's paths to the local ones.

The next step is creating a Run/Debug configuration:

Run -> Edit Configurations

Run/Debug configuration

Add a "PHP Web Application" config. Give it a name and choose the previously configured server from the select box.

To run our app in debug mode just go to:

Run -> Debug

and the web app will appear on a new tab in debug mode! If you want to restart the debug session again (after the actual one is disconnected) just refresh the page in the browser.

Cli app debugging:
At the start we should able to reach the host machine from our Vagrant box. So, how can we find out the host's ip?
"The IP address of the host is always the same IP address but with the final octet as a 1."
Okay, e.g.: if the vbox's ip is 192.168.33.10 we can ping 192.168.33.1 as our host machine from our guest.

We need the Server configurations as well (the same as the Web app debugging).

On the server side (in Vagrant) we've to create some environment variables:

export PHP_IDE_CONFIG="serverName=phpsaltbox"

export XDEBUG_CONFIG="remote_host=192.168.33.1 idekey=phpsaltbox"

serverName is the name what we typed in the Server configurations.
idekey comes from the xdebug config xdebug.idekey (it can be anything).

To run our cli app in debug mode we'll use the "Start Listen PHP Debug Connections" function in PhpStorm.

"Start Listen PHP Debug Connections" function


As the final step, just run your cli app in Vagrant and you'll see the debug window in PhpStorm in a couple of seconds!


References:

No comments:

Post a Comment