PHPStorm, Vagrant and Xdebug

debug

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”.

xdebug2

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.

xdebug2

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

xdebug5

Then we can select the related server and fill in the session ID field.

xdebug6

Apply all changes and enable the debugger:

xdebug7

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.

xdebug8

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.

xdebug9

Now we can start debugging the code. After refreshing the website, the PHPStorm should pause executing the program and show us everything in details.

[FM_form id="1"]


  • Adrian

    I followed your instruction. But phpstorm xdebug still cannot connect.

    My host is Windows 8.1 x64

    • Timur

      I too can’t set it up

    • http://piotrpasich.com Piotr Pasich

      Adrian, have you installed and enabled xdebug on Apache configuration? Can you find it in phpinfo() ?

    • Artur Dzida

      AFAIR you should put your ip/port of your host or vagrant ip in win firewall rule exception

  • http://www.elporfirio.com Porfirio Chavez

    debug doesn’t work, when you are using Route::group and ‘prefix’