Hey! Long time no see! Heehee.
I am just dropping a note here about the basic steps of how to install XDebug for a specific setup.
Here is my configuration:
- OS: Windows 7 64 bits
- IP: 192.168.0.3
- IDE: Netbeans 8.2
- OS: Debian Jessie 64 bits in VirtualBox
- IP: 192.168.0.4 (bridged network in VirtualBox settings)
- standard Apache2, PHP 5, MySQL from official repositories
The idea is: I want to use the stepper on a website located inside a virtual machine from an IDE on the host machine.
Step by step
- On the guest machine, get the phpinfo() output. If you have no idea how to do this quickly:
- From the command line, run
php --info > ~/tempphpinfo.txt(you can safely delete this file later).
- Open the file:
- Copy all the contents.
- Go to https://xdebug.org/wizard.php and follow the instructions: you will have to paste the contents of the phpinfo obtained earlier. The wizard will tell you exactly how to build xdebug. Make sure you are in the correct folder when running the
- Locate the xdebug.ini files (named 20-xdebug.ini on my Debian) on the guest machine:
find | grep xdebug
- Edit one of the files listed by the last command and add those lines:
20-xdebug.iniINI1234567;zend_extension=[path to xdebug .so/.dll]zend_extension=/usr/lib/php5/20131226/xdebug.soxdebug.remote_enable=1xdebug.remote_handler=dbgpxdebug.remote_mode=reqxdebug.remote_host=127.0.0.1xdebug.remote_port=9000
sudo service apache2 restart, just in case.
We will create a small php file which will react to XDebug calls.
- Create a php file:
- Insert the following:
PHP1234567891011<?php$address = '127.0.0.1';$port = 9000;$sock = socket_create(AF_INET, SOCK_STREAM, 0);socket_bind($sock, $address, $port) or die();socket_listen($sock);$client = socket_accept($sock);echo "connection established: $client";socket_close($client);socket_close($sock);?>
- Save and close.
- Start the command line debugger:
php ~/testxdebug.php. You may have a warning about the xdebug extension already started: you can ignore it or fix this later by commenting/removing the
zend_extension=path/to/xdebug.soline you have added to the /etc/php5/cli/php.ini file (not the xdebug.ini files!).
- In the web browser, go to your website location, a Drupal 8 in my case and add an attribute at the end of the URL, e.g.: http://localhost/DrupalXDebug/web/index.php?XDEBUG_SESSION_START=mysession.
- In the terminal where you have run the
php ~/testxdebug.phpcommand, you should see a message like this one:
connection established: Resource id #5
If not, the configuration is wrong and check the steps again or the resources links at the end of this article.
Setup remote debugging
Until now, we have only ran the debugger (the little php script we have created earlier, which you can safely delete now) from inside the guest machine. We will configure xdebug to work with remote machines, in my case, the Windows host.
- Edit one of the xdebug.ini files you have edited earlier: replace the
xdebug.remote_hostvalue by the IP of the host,
192.168.0.3in my case.
sudo service apache2 restart, just in case.
Now we can configure Netbeans to run the debugger.
- Open the website project.
- Right click on the project in the Projects list and click on Properties.
- Enter the URL of your website in Run Configuration > Project URL. In my case, it was like this:
- Click on the Advanced button.
- Set the path mappings. In my case:
You should now be set for debugging with the stepper!
Run the debug
- Click on the Debug Project button in the top toolbar of Netbeans and it should launch automatically a browser with a session id already set!
- Put a breakpoint somewhere in the code.
- Each time a request is sent to the website, Netbeans will control the page generation.
- Resources links:
- Detailed explanations from Netbeans: http://wiki.netbeans.org/HowToConfigureXDebug
- A really nice flowchart: https://netbeans.org/project_downloads/www/php/debug-setup-flowchart.pdf
- Explanation of the Path Mapper: https://blogs.oracle.com/netbeansphp/path-mapping-in-php-debugger
- How to check XDebug installation: https://blogs.oracle.com/netbeansphp/howto-check-xdebug-installation
- The path mapper functionality of Netbeans is required because the paths to the same resource (web page, php file, etc) are different between the host and the guest. As XDebug only knows about the guest path, e.g.: /var/www/html/path/to/site/file.php, Netbeans has to map it to the host path, e.g.: W:\path\to\site\file.php to be able to step through it. However, if you are not using remote debugging and therefore the server runs in local —and, if you are on Linux, you are not using symlinks— there is no need for path mapping.