Setting up Xdebug with Vagrant and PHPStorm

Setting up Xdebug with Vagrant and PHPStorm

Introduction

I've never used PHPStorm before so am no expert, but here are some notes that may help those of you who use it get it working with Vagrant and Xdebug. This approach should let you use the Nginx hostnames (eg. atd.dev) rather than the Apache hostnames (eg. atd-d6phx.dev, etc).

Xdebug settings

Xdebug has a config file (typically /etc/php.d/xdebug.ini) on each php environment (eg. each Apache VM).

Important settings are:

  • xdebug.remote_host - set this to the IP address of the Vagrant host (your laptop). For me this is 192.168.33.1
    • If you're having any problems, ssh into the Apache VM you're working with and make sure you can ping this IP address from the VM. If you suspect your IP address might be different, open your VirtualBox GUI and see if you can find your IP address there.
  • xdebug.remote_port - set this to port you have told your editor (PHPStorm) to listen on. This can be anything but the default is 9000.

Remember to restart Apache after making any changes to this file, in order for the changes to take effect.

Here is my d8 vm xdebug.ini file currently:

; Enable xdebug extension module
[xdebug]
zend_extension=/usr/lib64/php/modules/xdebug.so
xdebug.remote_enable = 1
xdebug.remote_log=/var/log/xdebug.log

// Enable to connect back to Xdebug on ip the http request came from. Won't work if coming
// via Ngnix and Varnish:
//xdebug.remote_connect_back=1

// IP address to connect back to. Should be the IP address of the Vagrant host:
xdebug.remote_host = 192.168.33.1

// Enable this if you don't want to have to tell Xdebug to start via GET/POST/COOKIE:
xdebug.remote_autostart=1

// Enable this if you want the debugger to start when a certain key is passed with the request:
xdebug.idekey=xdb

//xdebug.remote_mode=jit

xdebug.remote_port=9000
xdebug.remote_handler=dbgp

PHPStorm settings

I basically just followed this page on the PHPStorm website:

https://www.jetbrains.com/help/phpstorm/2016.3/configuring-xdebug.html#d45497e21

I don't know if all of these steps are necessary in PHPStorm (not sure why it needs to know about the Vagrant VM, reckon it might be possible to skip that bit) but here's how my settings look now:

PHP → Servers

PHP CLI Interpreter

PHP → Debug

Video

In case I missed anything above, here's a video of me setting up from scratch:

Logs

The log file /var/log/xdebug.log on your Apache VMs tells you what Xdebug is trying to do. If you're having problems, these is likely to be some useful information in this log.

If the log doesn't exist, create it and make it writeable by the Apache user (or all users).

Check that you have something like this in your Xdebug config to tell it where to write the log:

xdebug.remote_log=/var/log/xdebug.log

 

 

blog tag: