Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Created March 11, 2019 19:54
Show Gist options
  • Save mttjohnson/10bfe1a1fd3833adb8b392693efff26b to your computer and use it in GitHub Desktop.
Save mttjohnson/10bfe1a1fd3833adb8b392693efff26b to your computer and use it in GitHub Desktop.
Update Magento 2 env.php from bash shell script
#!/usr/bin/env bash
set -eu
# Add configs values to env.php
echo "Add configs values programmatically to env.php"
set +H # disable history expansion
PHP_CODE=$(cat <<'PHP_CODE'
<?php
$existing_env = include("app/etc/env.php");
$new_env =
array (
'session' =>
array (
'save' => 'redis',
'redis' =>
array (
'host' => '127.0.0.1',
'port' => '6380',
'password' => '',
'timeout' => '2.5',
'persistent_identifier' => '',
'database' => '0',
'compression_threshold' => '2048',
'compression_library' => 'gzip',
'log_level' => '1',
'max_concurrency' => '6',
'break_after_frontend' => '5',
'break_after_adminhtml' => '30',
'first_lifetime' => '600',
'bot_first_lifetime' => '60',
'bot_lifetime' => '7200',
'disable_locking' => '0',
'min_lifetime' => '60',
'max_lifetime' => '2592000',
),
),
'cache' =>
array (
'frontend' =>
array (
'default' =>
array (
'backend' => 'Cm_Cache_Backend_Redis',
'backend_options' =>
array (
'server' => '127.0.0.1',
'database' => '0',
'port' => '6379',
'password' => '',
),
),
),
),
)
;
$merged_env = array_merge(
$existing_env,
$new_env
);
$combined_env = var_export($merged_env, true);
echo "<?php
return " . $combined_env . ";
";
PHP_CODE
)
set -H # re-enable history expansion
#cat ${MAGENTO_ROOT_DIR}/app/etc/env.php
# Get the new file contents in a bash variable before writing to file
# If anything breaks during merging the contents it will not write over the env.php
# This also prevents the env.php from getting overwritten until we have extracted
# all of the existing contents and have merged everything together
NEW_ENV=$(echo "${PHP_CODE}" | php)
#echo "${NEW_ENV}"
# Overwrite env.php with combined configs
echo "${NEW_ENV}" > ${MAGENTO_ROOT_DIR}/app/etc/env.php
echo "Finished adding configs values programmatically to env.php"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment