-
Is there any way to set or change an env (environment variable) value without the full restart of the web container? I know that I can temporarily set it for the current terminal, but need to set it for other processes (including the php-fpm) too. And it's ok that it will be dropped or reset after the restart. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
I've invented a workaround to set the ENV variable to the
Maybe it's a good idea to move this to a utility script inside |
Beta Was this translation helpful? Give feedback.
-
And here is a more detailed script, that applies the ENV value to the new terminals, and also to the current (if ran with #!/bin/bash
if [[ "$1" == *"="* ]]; then
ENV=$(echo "$1" | cut -d '=' -f 1)
VALUE=$(echo "$1" | cut -d '=' -f 2)
else
ENV=$1
VALUE=$2
fi
FILE_TERMINAL1=~/.profile
FILE_TERMINAL2=~/.bashrc
FILE_FPM=/etc/php/$DDEV_PHP_VERSION/fpm/pool.d/www.conf
# If arguments are empty.
if [ -z "$ENV" ]; then
echo "Usage format:
$0 ENV value
or
$0 ENV=value
"
exit
fi
# Removing the line if the value is empty, because php-fpm doesn't support empty values.
if [ -z "$VALUE" ]; then
sed -i "s/^env\[$ENV\] = .*//" $FILE_FPM
else
sed -i "/^env\[$ENV\] = /{h;s/ = .*/ = $VALUE/};\${x;/^$/{s//env\[$ENV\] = $VALUE/;H};x}" $FILE_FPM
fi
sed -i "/^export $ENV=/{h;s/=.*/=$VALUE/};\${x;/^$/{s//export $ENV=$VALUE/;H};x}" $FILE_TERMINAL1
sed -i "/^export $ENV=/{h;s/=.*/=$VALUE/};\${x;/^$/{s//export $ENV=$VALUE/;H};x}" $FILE_TERMINAL2
supervisorctl restart php-fpm
# This will not work if ran as a subshell
# The solution is to run the script with `source` prefix
# source /usr/local/bin/setenv NAME VALUE
export $ENV="$VALUE"
if [ "$0" = "$BASH_SOURCE" ]; then
echo "Warning: To apply the ENV variable also to the current terminal, execute it via this command:"
echo "source $0 $1 $2"
fi
echo "Environment variable successfully set: $ENV=\"$VALUE\"" Usage example:
Can we include it into DDEV web container? |
Beta Was this translation helpful? Give feedback.
And here is a more detailed script, that applies the ENV value to the new terminals, and also to the current (if ran with
source
prefix):cat /usr/local/bin/ddev-setenv
: