Skip to content

Instantly share code, notes, and snippets.

@ProcessEight
Last active November 25, 2021 11:00
Show Gist options
  • Save ProcessEight/5b9b7052b30f8aef7bf1d29fc5f87421 to your computer and use it in GitHub Desktop.
Save ProcessEight/5b9b7052b30f8aef7bf1d29fc5f87421 to your computer and use it in GitHub Desktop.
How to enable and disable smoothly using simple bash scripts

Enabling and disabling the Xdebug PHP extension with a single command

Note: This guide assumes you are using PHP-FPM and Ubuntu 16.04

Speed (and therefore time) is of the essence

Xdebug is a fantastic tool which can save hours or even days of debugging effort.

Yet just enabling the extension itself will slow down Magento. Composer will also warn you of performance degradation when running it with Xdebug enabled.

Disabling it using bookmarklets, cookies or other flags isn't good enough - the performance impact will still be felt.

Magento themselves recommend disabling Xdebug when it is not required.

So the best case scenario is to enable it for debugging, yet keep it disabled when we are not using it.

So how can we make this process as easy as a single command?

Let's create a bash script:

#!/usr/bin/env bash

if [[ -f /etc/php/7.0/fpm/conf.d/20-xdebug.ini && -f /etc/php/7.0/cli/conf.d/20-xdebug.ini ]]; then
    echo "
#
# Xdebug is already enabled
#
";
    /usr/bin/php7.0 -v
    exit
fi

echo "
#
# Enabling Xdebug...
#
";
sudo ln -s /etc/php/7.0/mods-available/xdebug.ini /etc/php/7.0/fpm/conf.d/20-xdebug.ini
sudo ln -s /etc/php/7.0/mods-available/xdebug.ini /etc/php/7.0/cli/conf.d/20-xdebug.ini
sudo service php7.0-fpm restart
/usr/bin/php7.0 -v

echo "
#
# Remember to execute 'export XDEBUG_CONFIG=\"idekey=PHPSTORM\"' if you are debugging from the command line
#
";

This script basically enables Xdebug and restarts PHP.

Now let's create a sister script to do the opposite:

#!/usr/bin/env bash

if [[ ! -f /etc/php/7.0/fpm/conf.d/20-xdebug.ini && ! -f /etc/php/7.0/cli/conf.d/20-xdebug.ini ]]; then
    echo "
#
# Xdebug is not enabled
#
";
    /usr/bin/php7.0 -v
    exit
fi

echo "
#
# Disabling Xdebug...
#
";
sudo rm -f /etc/php/7.0/fpm/conf.d/20-xdebug.ini
sudo rm -f /etc/php/7.0/cli/conf.d/20-xdebug.ini
sudo service php7.0-fpm restart
/usr/bin/php7.0 -v

echo "
#
# Remember to execute 'unset XDEBUG_CONFIG' if you are debugging from the command line
#
";

This script disables Xdebug.

To run either script, we just execute them from the command line:

$ ./enable-xdebug-php70.sh

#
# Enabling Xdebug...
#

PHP 7.0.27-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jan  5 2018 14:12:46) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.27-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies
    with Xdebug v2.5.1, Copyright (c) 2002-2017, by Derick Rethans

#
# Remember to execute 'export XDEBUG_CONFIG=\"idekey=PHPSTORM\"' if you are debugging from the command line
#

Disabling Xdebug is just as easy:

$ ./disable-xdebug-php70.sh

#
# Disabling Xdebug...
#

PHP 7.0.27-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jan  5 2018 14:12:46) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.27-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies
    
#
# Remember to execute 'unset XDEBUG_CONFIG' if you are debugging from the command line
#

As an added bonus, both scripts check whether Xdebug is enabled/disabled before attempting to enable/disable Xdebug.

However, we can optimise this still further. I said we could make this a single command. As it stands, you have to remember where the scripts are and either change directory or provide the full path to the scripts.

Wouldn't it be great if we could run these scripts anywhere? Well, we can!

Just edit the ~/.bash_aliases file (or create it if it doesn't exist) and create a new alias:

alias xde='/path/to/enable-xdebug-php70.sh'
alias xdd='/path/to/disable-xdebug-php70.sh'

I've chosen xde for Xdebug enable and xdd for Xdebug disable.

Now just run source ~/.bashrc (or close and re-open all terminal windows) in order to load the new aliases.

Now you can enable or disable Xdebug with just three characters:

$ xde

#
# Enabling Xdebug...
#

PHP 7.0.27-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jan  5 2018 14:12:46) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.27-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies
    with Xdebug v2.5.1, Copyright (c) 2002-2017, by Derick Rethans

#
# Remember to execute 'export XDEBUG_CONFIG="idekey=PHPSTORM"' if you are debugging from the command line
#

$ xdd

#
# Disabling Xdebug...
#

PHP 7.0.27-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jan  5 2018 14:12:46) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.27-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

#
# Remember to execute 'unset XDEBUG_CONFIG' if you are debugging from the command line
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment