Skip to content

Instantly share code, notes, and snippets.

@Aldo-f
Last active February 3, 2024 07:37
Show Gist options
  • Save Aldo-f/3a4fc458d7777e792515c12541b841f5 to your computer and use it in GitHub Desktop.
Save Aldo-f/3a4fc458d7777e792515c12541b841f5 to your computer and use it in GitHub Desktop.
Toggle Xdebug - Apache
#!/bin/bash
# Switch Xdebug state for different PHP configurations
PHP_VERSIONS=($(ls /etc/php/ | grep -E '^[0-9]+\.[0-9]+$'))
PHP_SAPI=("cli" "fpm") # Add more SAPIs if needed
APACHE_RESTART_COMMAND="sudo /etc/init.d/apache2 restart"
# Determine the initial Xdebug state based on the first loop
for version in "${PHP_VERSIONS[@]}"; do
for sapi in "${PHP_SAPI[@]}"; do
PHP_INI_PATH="/etc/php/${version}/${sapi}/conf.d/20-xdebug.ini"
if [ -e "$PHP_INI_PATH" ]; then
xdebug_state="enabled"
elif [ -e "$PHP_INI_PATH.disabled" ]; then
xdebug_state="disabled"
fi
break 2 # Break out of both loops after determining the state from the first configuration
done
done
for version in "${PHP_VERSIONS[@]}"; do
for sapi in "${PHP_SAPI[@]}"; do
PHP_INI_PATH="/etc/php/${version}/${sapi}/conf.d/20-xdebug.ini"
if [ -e "$PHP_INI_PATH" ] && [ "$xdebug_state" == "enabled" ]; then
# Xdebug is enabled, so let's disable it
mv "$PHP_INI_PATH" "$PHP_INI_PATH.disabled"
action="disabled"
elif [ -e "$PHP_INI_PATH.disabled" ] && [ "$xdebug_state" == "disabled" ]; then
# Xdebug is disabled, so let's enable it
mv "$PHP_INI_PATH.disabled" "$PHP_INI_PATH"
action="enabled"
fi
# Restart PHP-FPM service for the current version
if [ "$sapi" == "fpm" ]; then
sudo /etc/init.d/php${version}-${sapi} restart
fi
done
done
# Restart Apache
$APACHE_RESTART_COMMAND
# Switch Xdebug state
[ "$xdebug_state" == "enabled" ] && xdebug_state="disabled" || xdebug_state="enabled"
# Output a concise summary
echo ""
echo "Xdebug is now $xdebug_state."
echo ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment