The first tool we will install and configure is xdebug, a debugging and profiling tool for PHP. This extension can be downloaded, decompressed, configured, compiled and installed in a very easy manner by using the PECL utility included with PHP (https://pecl.php.net/). To do this, inside the container's Terminal window, please enter the following commands:
# pecl install xdebug
# echo -e "zend_extension=$( php -i | grep extensions | awk '{print $3}' )/xdebug.so\n" >> /etc/php.ini
# echo -e "xdebug.remote_enable = 1\n" >> /etc/php.ini
# echo -e "xdebug.remote_enable_trigger = 1\n" >> /etc/php.ini
# echo -e "xdebug.remote_connect_back = 1\n" >> /etc/php.ini
# echo -e "xdebug.idekey = PHPSTORM\n" >> /etc/php.ini
# echo -e "xdebug.profiler_enable = 1\n" >> /etc/php.ini
# echo -e "xdebug.profiler_enable_trigger = 1\n" >> /etc/php.ini
# /etc/init.d/php-fpm restart
# tail -50 /etc/php.ini
The last lines of your container's /etc/php.ini file should now look like this:
Newly added lines to the php.ini file
Once done, please reload the http://localhost:8181 page in your favorite browser. It should now read as follows:
Confirmation that the xdebug extension has been loaded
If you scroll towards the end of the page, you should now see an xdebug section:
The xdebug section of the phpinfo page
You should also notice that the profiler options are now enabled under the xdebug entry:
Confirmation that xdebug code profiling is enabled
We will now configure PHPStorm to be the debugging server. This will allow us to use our IDE as the control center for our debugging sessions.
Before we start, we will make the entire fasterweb folder available as the server’s web root directory by entering these commands inside the container:
# rm /srv/www
# ln -s /srv/fasterweb /srv/www
# cd /srv/www
Now, start PHPStorm and make our fasterweb directory the home root of this project. To do so, select Create New Project from Existing Files and Source files are in a local directory and designate our fasterweb directory as the Project root before clicking on Finish.
Once created, select Settings from within the File menu. Under the Languages & Frameworks section, unfold the PHP menu entry and click on the Servers entry. Please enter all the appropriate information according to the specifics of your setup. The Host option must contain the value of the IP address of the Linux for PHP container. If you are not sure what is the IP address of your Docker container, please enter the following command on the container's command line in order to obtain it:
# ifconfig
Once done, you can confirm by clicking on the Apply and OK buttons:
Configuring PHPStorm to connect to the web server and xdebug
Then, under the Run menu, you will find the Edit Configurations... entry. It can also be found on the right-hand side of the IDE's screen:
The ‘Edit configurations…’ setting
You can then add a PHP Remote Debug entry by clicking on the green plus sign in the upper-left corner of this window. Please select the server that we created in the previous step and please make sure that the Ide key(session id) is set to PHPSTORM:
Configuring the debugging session
You can now activate the PHPStorm debugging server by clicking on the Listen to debugger connections button in the upper-right menu of the main PHPStorm screen, set a breakpoint by clicking in the space to the right of any line number of the index.php file, and launch the debug tool corresponding to the index.php configuration that we created in the previous step.
If the top-right toolbar menu is not displayed on your screen, please click on the Toolbar entry of the View menu to make them appear on your screen. These buttons are also accessible as entries in the Run menu.
Activating the PHPStorm debugging server, setting a breakpoint and launching the debug tool
Now, open your favorite browser and request the same web page by entering the IP address of your Docker container: http://[IP_ADDRESS]/?XDEBUG_SESSION_START=PHPSTORM.
You will then notice that the browser is caught in an infinite loop:
The browser is waiting for the debug session to resume or end
You will also notice that the debugging information is now showing inside the IDE. We can also control the session and determine when execution will resume from within the IDE. Please inspect the contents of the variables before allowing execution to resume by clicking on the green play button on the left-hand side of the screen. You can also end the debugging session by clicking on the pink stop button in the same icon menu:
The debugging session allows for detailed inspection of variables during runtime
Once the debugging session is over, we can now inspect our container's /tmp directory and should find the profiler output in a file named cachegrind.out. You can then inspect this file directly through your favorite text editor or by installing specialized software such as Kcachegrind with the package manager of your Linux distribution. Here is a sample output when using Kcachegrind:
Viewing the xdebug profiling report with Kcachegrind
Thus, xdebug’s profiling tool will be available to you if you wish to use it on top of those that we will be using to optimize our code examples in the next chapters. This being said, in the next chapter, we will be looking into more advanced profiling tools such as Blackfire.io.
Once you are done testing xdebug, you can restore the chapter_1 folder as the server's web root directory:
# rm /srv/www
# ln -s /srv/fasterweb/chapter_1 /srv/www
# cd /srv/www
Now, let's continue by having a look at SQL speed testing tools.