Dev Blog

Please note, that current versions of composer do work OK with XDebug. This post is irrelevant now, but it is left just in case someone might find useful those code snippets.

Using composer with Xdebug turned on will result in warning message, saying that Xdebug affects performance - and that's true. Composer will run much slower when used with Xdebug enabled. To overcome this issue, it is possible to disable Xdebug when using composer command.

Note that following steps are meant to be used only on development machine, do not use on production servers!

To turn on and off Xdebug, we will use bash script - which will link or unlink Xdebug configuration - as just disabling Xdebug in config still degrades performance.

Allow execution from anywhere

Place script within your $PATH environment. You might have Your home directory's bin folder (~/bin) already added to path, if not add it to ~/.bashrc file:

$ nano ~/.bashrc

And then add this line to inform shell about new search path for executable files:

$ export PATH="/home/$USER/bin:$PATH"

Now let's create file named Xdebug and make it executable:

$ touch ~/bin/xdebug
$ chmod +x ~/bin/xdebug

Actual enabling and disabling script

Folllowing  bash script will toggle Xdebug on cli and fpm SAPI's. Edit script:

$ nano ~/bin/xdebug

Alter this script to Your needs:

#!/bin/bash
# Usage: xdebug on|off
# Xdebug configuration sources (link sources)
srcs=("/etc/php/7.0/mods-available/xdebug.ini"
"/etc/php/7.1/mods-available/xdebug.ini"
"/etc/php/7.1/mods-available/xdebug.ini"
)
# Xdebug configuration destinations (link targets)
dests=("/etc/php/7.0/cli/conf.d/25-xdebug.ini"
"/etc/php/7.1/cli/conf.d/25-xdebug.ini"
"/etc/php/7.1/fpm/conf.d/25-xdebug.ini"
)

if [ "$1" == "on" ]; then
echo "Turning on xdebug"
i=0
for dest in ${dests[@]}; do
if [ ! -e "$dest" ]; then
ln -s "${srcs[i]}" "$dest"
fi
((i++))
done
# Remove below lines if only CLI mode is required
sudo service php7.0-fpm restart
sudo service php7.1-fpm restart

fi
if [ "$1" == "off" ]; then
echo "Turning off xdebug"
for dest in ${dests[@]}; do
if [ -e "$dest" ]; then
unlink "$dest"
fi
done
# Remove below lines if only CLI mode is required
sudo service php7.0-fpm restart
sudo service php7.1-fpm restart
fi

Allow password-less service restart

Skip this part if don't want to toggle Xdebug on PHP (FPM) service.

On development machine, web-related services might be restarted many time, so we don't want to be asked for password every time, for instance, when using composer. To overcome this issue, we will allow PHP FPM to be restarted without password. First we need to add allowed commands to /etc/sudoers.d/commands file. Do not edit it directly, use visudo command - it will check for errors before saving:

$ sudo visudo -f /etc/sudoers.d/commands

Add following lines to allow any service operations on PHP FPM - change service name to Your needs. Set user name (first column) to Your local user:

peter   ALL=(root)NOPASSWD: /usr/sbin/service php7.0-fpm *
peter ALL=(root)NOPASSWD: /usr/sbin/service php7.1-fpm *

This will allow managing PHP FPM without providing password - so do not add this on production machine!

Automatically disable Xdebug when using composer

With above setup, few last steps are to make proxy script, which will disable Xdebug, run any composer command and then enable Xdebug and ensure that it is executed instead of real composer script. To make it happen, we will create script to proxy our composer calls and than make alias to it. This script simply wraps composer call with Xdebug on and off, adjust Your path if needed. So open editor on file:

$ nano ~/bin/composer

And put in proxy script:

#!/bin/bash
xdebug off
/usr/local/bin/composer $@
xdebug on

Now replace original composer with alias, via ~/.bash_aliases file:

$ nano ~/.bash_aliases

By adding following line:

alias composer='~/bin/composer'

To make it work in current terminal window, type bash. From now on, when You call composer Xdebug will be automatically disabled for run time of composer. Example output of aliased composer:

$ composer --version
Turning off xdebug
Composer version 1.1.3 2016-06-26 15:42:08
Turning on xdebug