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:
- host:
- OS: Windows 7 64 bits
- IP: 192.168.0.3
- IDE: Netbeans 8.2
- guest:
- 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
XDebug installation
- 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:
gedit ~/tempphpinfo.txt
. - 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
phpize
command. - Locate the xdebug.ini files (named 20-xdebug.ini on my Debian) on the guest machine:
cd /etc/php5
find | grep xdebug
- Edit one of the files listed by the last command and add those lines:
;zend_extension=[path to xdebug .so/.dll] zend_extension=/usr/lib/php5/20131226/xdebug.so xdebug.remote_enable=1 xdebug.remote_handler=dbgp xdebug.remote_mode=req xdebug.remote_host=127.0.0.1 xdebug.remote_port=9000
sudo service apache2 restart
, just in case.
Testing XDebug
We will create a small php file which will react to XDebug calls.
- Create a php file:
touch ~/testxdebug.php
gedit ~/testxdebug.php
- Insert the following:
- 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 thezend_extension=path/to/xdebug.so
line 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.php
command, 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_host
value by the IP of the host,192.168.0.3
in my case. sudo service apache2 restart
, just in case.
Netbeans configuration
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.
Additional Information
- 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.