Created
May 15, 2019 13:07
-
-
Save stefanotorresi/9f48f8c476b17c44d68535630522a2be to your computer and use it in GitHub Desktop.
bash script to enable xdebug inside a PHP Docker container
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
USER=$(id -u) | |
XDEBUG_INI_FILE="/usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini" | |
if [[ ${USER} != 0 ]]; then | |
echo "This script must be run as root" | |
exit 1 | |
fi | |
function enable { | |
[[ ! -f ${XDEBUG_INI_FILE} ]] && docker-php-ext-enable xdebug | |
local HOST="" | |
local PORT="" | |
while getopts :h:p: OPT; do | |
case ${OPT} in | |
h) HOST=$OPTARG ;; | |
p) PORT=$OPTARG ;; | |
\?) | |
echo "Invalid option: -$OPTARG" | |
exit 1 | |
;; | |
:) | |
echo "Option -$OPTARG requires an argument." | |
exit 1 | |
;; | |
esac | |
done | |
[ -z ${HOST} ] && HOST=$(ip route | awk '/default/ { print $3 }') | |
[ -z ${PORT} ] && PORT=9000 | |
tee -a ${XDEBUG_INI_FILE} <<EOM | |
xdebug.remote_host=${HOST} | |
xdebug.remote_port=${PORT} | |
xdebug.remote_enable=true | |
EOM | |
} | |
function disable { | |
rm -f ${XDEBUG_INI_FILE} | |
} | |
function show_status { | |
if (php -i | grep xdebug | grep 'xdebug.remote_enable => On' &>/dev/null) ; then | |
echo "Xdebug is enabled" | |
else | |
echo "Xdebug is disabled" | |
fi | |
} | |
function show_usage { | |
echo "Usage: xdebug [ enable [-h host] [-p port] | disable | status ]" | |
echo " 'enable' command can take options -h and -p to set the remote host and port" | |
echo " 'enable' and 'disable' commands will also reload the php configuration on the fly, no container restart is required" | |
} | |
function restart_phpfpm { | |
pkill -USR2 php-fpm | |
} | |
case "${1-}" in | |
"enable") | |
shift | |
enable "$@" | |
show_status | |
restart_phpfpm | |
;; | |
"disable") | |
disable | |
show_status | |
restart_phpfpm | |
;; | |
"status") show_status ;; | |
*) show_usage ;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment