[fa icon="calendar"] Publié le 08 January 2018 par Clément Taboulot


First thing I ask myself when I begin to work on a new technolgy is “How can I debug it?”.

I am currently working on a PHP project, and wanted to be efficient by debugging my code, rather than using log functions like echo, print_r and var_dump. May people do so and usually waste time…

This tutorial was tested with a Symfony2 backend project hosted on a vagrant VM (php5).

Step 1 : Install and configure Xdebug on your Vagrant machine. (~10mins)

Xdebug is an extension for PHP. It helps you debug your php scripts and applications.

  1. Install Xdebug :
sudo apt-get install php5-xdebug
  1. Configure it :
sudo vim /etc/php5/mods-available/xdebug.ini

Change the file so it looks like :

[XDebug]
zend_extension="/usr/lib/php5/20121212/xdebug.so"   # Path to your Xdebug extension
xdebug.remote_enable=true                           # Indicate that it will contact a client which is listening on a port
xdebug.remote_port=9000                             # Indicate that the client will listen on port 9000
xdebug.remote_connect_back=true                     # Xdebug will try to connect to the client that made the HTTP request
xdebug.remote_autostart = on                        # Xdebug will always attempt to start a remote debugging session

Find Xdebug path with :

 sudo find / -name "xdebug.so"
  1. If you use php5-fpm restart it :
sudo service php5-fpm restart
  1. Verify you did it right :
$ php -v
PHP 5.5.9-1ubuntu4.22 (cli) (built: Aug  4 2017 19:40:28)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

If you don’t see Xdebug  like above, run as sudo :

sudo ln -s /etc/php5/mods-available/xdebug.ini /etc/php5/fpm/conf.d/20-xdebug.ini
sudo ln -s /etc/php5/mods-available/xdebug.ini /etc/php5/cli/conf.d/20-xdebug.ini

Now you must see Xdebug :

$ php -v
PHP 5.5.9-1ubuntu4.22 (cli) (built: Aug  4 2017 19:40:28)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans

 

Step 2 : Install and configure PHP Debug extension for VSCode by Felix Becker. (~5mins)

  1. Install PHP Debug extension from the marketplace.

    marketplace_php_debug-1.png
  2. Add this configuration in your launch.json which you can find with Cmd + Shift + P :

{
    "name": "Listen for XDebug",
    "type": "php",
    "request": "launch",
    "pathMappings": {
        "<path_to_your_project_in_vagrant>": "${workspaceRoot}"
    },
    "port": 9000,
    "log": true
}

Don't forget to replace <path_to_your_project_in_vagrant> !

Step 3 : Enjoy debugging. (~2mins)

  1. Place a breakpoint in your controller and start your debugger :

    start_debug.png
  2. Hit your url in your favorite browser. Start debugging !

    enjoy_debug.png

Do you want to debug phpunit test ? (~2min)

  1. Use ifconfig and find the IP address of the interface which is used to enable communication between your vagrant VM and your local Machine :

    ip_address.png
  2. Add xdebug.remote_host=<IP> to the xdebug.ini file. Don’t forget to restart php5-fpm !
    You can also specify it directly when you launch phpunit in your VM :
    php -d xdebug.remote_host=<IP> ./bin/phpunit ...

  3. Place a breakpoint and launch your test. That’s all.

 

Learn more about us !


Liked this article? Interested in building an app with us?

Contact a React Native expert in Paris




Want to rate this article?
Submit Rating

Topics: Symfony, vagrant, Debug