In this tutorial, I will be going over how to setup XDebug for your PHP environment that happens to be on an IIS Server. The prerequisite is that you have IIS setup, PHP Manager installed and accessible under the Internet Information (IIS) Server Manager, and a working PHP engine.
My current setup at the time of this tutorial is as follows:
- Windows 10
- IIS Version 10.0.1.14393.0
- PHP Version 7.0.9
Press the Windows buttons in the lower left-hand corner or press the Windows key on your keyboard and type in “Internet Information Server.” Then fire up the PHP Manager.
You’ll see a link for Check phpinfo(). Go ahead and click on this. You may have to choose a local site. Just choose the one you’ll want to xdebug. Afterwards you should see the phpinfo() data in an iframe-like window. What you will want to do is copy everything inside the iframe to your clipboard. It is important that you get the entire phpinfo() rendered output. CTRL + A and then CTRL + C while the iframe window is in an active state.
The next thing you want to do is go to this link. https://xdebug.org/wizard.php and you will see an empty box on the page. In that box, you’ll paste from your clipboard the phpinfo() output. What this page does is provide you information about which .dll of xdebug to install. In my case, I was suggested to install PHP 7.0 VC14 (32bit) version 2.5.3.
Go ahead and download the suggested xdebug .dll. If there was an issue and the tool could not give you a suggestion or you want to download a specific .dll, you can do so by going here: https://xdebug.org/download.php
Once you download the .dll, you will want to put the file in the ext folder inside your PHP directory. In my case, this was C:\Program Files (x86)\PHP\v7.0\ext
Now that you have the .dll installed inside your PHP directory, it is time to register that .dll file with PHP by modifying the php.ini file. Go back to the PHP Manager and click on the php.ini link under PHP Settings.
Scroll all the way to the bottom of the php.ini and add some lines similar to this following:
In the brackets, you could change it to something simple like [XDEBUG], it really does not matter. You will want to change the zend_extension to be the exact filename of the .dll file you just installed in the ext folder inside the PHP folder. Set x.debug.profiler_enable = 1 if you want xdebug to always be turned on and then set xdebug.profiler_enable_trigger = 0. In my example, I opted to manually trigger xdebug, which I will show you more on how to do this in a moment.
xdebug.profiler_output_dir is the directory path on your computer where you will want the cachgrind output files to go when they get created. In my case I have them go in a folder one level up from my local sites at C:\inetpub\cachegrind\
Feel free to modify the xdebug.profiler_output_name syntax. I found this particular syntax helpful. In this example, cachegrind output files will contain the URL and the querystring of the page the output was created is for. Learn more here: https://xdebug.org/docs/all_settings#trace_output_name to further customize the filenames.
Once you are happy with the settings. Save the php.ini file, go back to the Internet Information Server manager and restart the server.
Once the server is finished rebooting, click on PHP Manager again and click on check phpinfo() to view the PHP info again. However, this time you will looking to see if XDEBUG was registered correctly. When viewing the phpinfo() output, try to find the XDEBUG section. If you find it, you have successfully have all this working. If you do not see it, go back to the php.ini file and make sure everything is correct. Alternatively, you may need to install a different XDEBUG .dll.
Now its time to go to your local website. Fire up your favorite browser and type in the URL. If you used my configuration, where xdebug is off unless manually triggered, you will need to add an additional querystring suffix of XDEBUG_PROFILE to trigger xdebug. So something like http://mylocalwebsite.local/?XDEBUG_PROFILE
The page should load up a little bit slower than usual since xdeubg is running and tabulating calculations. When the page finally finishes, go to the directory you have set under xdebug.profiler_output_dir. You should see some cachgrind output files typically looking something like: cachegrind.out.mylocalwebsite_local___XDEBUG_PROFILE if you used my configurations.
If you see your cachegrind output file, then congrats, you’re almost there! If not, you will have to recheck the xdebug.profiler_output_dir in your php.ini. Therefore, you will need to go back and fix it.
The last thing you will need to do is find a viewer that could interpret the cachegrind output files into pretty charts and meaningful numbers for you to analyze. There’s a few clients out there for Windows, but the one I use is QCacheGrind (https://sourceforge.net/projects/qcachegrindwin/) and it looks something like this:
In addition, PHPStorm has an xdebug interrupter build in. It can be found under Tools > Analyze XDebug Profiler snapshot. And that’s it! Enjoy!
Some other resources if you are stuck or want to learn more:
- Profiling PHP Applications with xdebug, WinCacheGrind & XAMPP for Windows – https://www.youtube.com/watch?v=YHKFdfbcP8U
- WinCacheGrind, an alternative to QCacheGrind – http://sourceforge.net/projects/wincachegrind/
- KCacheGrind, the non-Windows version of QCacheGrind – http://kcachegrind.sourceforge.net/