When I have installed the Vagrant for the first time I struggled with connecting xdebug and PhpStorm for a while. I’d like to present a quick solution for this problem. Moreover, you can apply this when you’re working on remote server too.
Installing xdebug in Vagrant
A few next steps are related to the instance of Vagrant downloaded from puPHPet. If you’re connecting with remote server or have already configured xdebug on your virtual machine you can skip this point.
To add xdebug to Vagrant’s instance we need to add a new module to PHP configuration and run provisioning command:
1 2 3 4 5 6 7 8 | #puphpet/config.yaml #... php: #... modules: php: #... - xdebug |
1 | vagrant provision |
In the result Puppet should create a new configuration file /etc/php5/mods-available/xdebug.ini:
1 2 3 4 5 6 7 | #/etc/php5/mods-available/xdebug.ini zend_extension=/usr/lib/php5/20100525+lfs/xdebug.so xdebug.max_nesting_level = 400 xdebug.remote_enable=on xdebug.remote_connect_back=on html_errors=1 xdebug.extended_info=1 |
PHPStorm configuration
Now we should open PHPStorm and configure the remote server. To do this, you need to find a debugger icon on the top of the IDE and click “Edit configurations”.
Now we need to add a configuration type. In our case it will be remote debug where we can select to which server we’re going to connect and define the session ID.
But before that we need to add a related remote server. In a form we will find a few traps:
- a host field – this is where we put the host url (not IP address) of our website
- an absolute path – it’s empty, but should be filled by absolute path on the server where the application is run
Then we can select the related server and fill in the session ID field.
Apply all changes and enable the debugger:
Creating Xdebug cookie
The PHPStorm needs to know whether you want to debug or not. Enabling debugger in IDE is not enough, unfortunately. We need to generate a cookie on our domain. You can do it from browser’s profiler or use a ready tool available on PHPStorm page – http://www.jetbrains.com/phpstorm/marklets/.
As you can see we have a few possibilities. The most important for us are Start Debugger and Stop Debugger. These links include javascript code which creates or destroys cookie on selected domain.
I suppose it could be really annoying to find, open and generate this link every time we need to profile the code, so I have created a bookmark from these links in my browser’s toolbar – good practise.
Now we can start debugging the code. After refreshing the website, the PHPStorm should pause executing the program and show us everything in details.