This guide explains how to sync configurations in Drupal using Drush commands, tailored for different environments such as development, staging, and production.
Make sure Drush is installed on your system. You can install it globally using Composer:
composer global require drush/drush
Organize your configuration directories for different environments. For example:
config/
dev/
stage/
prod/
settings.php
Update the settings.php
file to specify the configuration directory based on the environment:
// settings.php
// Define the environment (could be set in Apache/Nginx or .htaccess file)
$env = getenv('DRUPAL_ENV') ?: 'prod'; // default to 'prod' if not set
// Set the config directory based on the environment
if ($env == 'dev') {
$config_directories[CONFIG_SYNC_DIRECTORY] = '../config/dev';
} elseif ($env == 'stage') {
$config_directories[CONFIG_SYNC_DIRECTORY] = '../config/stage';
} else {
$config_directories[CONFIG_SYNC_DIRECTORY] = '../config/prod';
}
Use Drush to export configuration based on the current environment:
# On Development Environment
drush config-export --destination=../config/dev
# On Staging Environment
drush config-export --destination=../config/stage
# On Production Environment
drush config-export --destination=../config/prod
Commit the exported configuration to your version control system:
# Example for Development Environment
git add config/dev
git commit -m "Exported Drupal configuration for development"
git push origin main
Pull the latest changes from your version control system on the target environment:
git pull origin main
Use Drush to import the configuration based on the current environment:
# On Development Environment
drush config-import --source=../config/dev
# On Staging Environment
drush config-import --source=../config/stage
# On Production Environment
drush config-import --source=../config/prod
Clear the cache after importing configuration changes:
drush cr
Ensure that the DRUPAL_ENV
environment variable is set correctly in each environment. This can be done in your web server configuration or in an .htaccess
file:
SetEnv DRUPAL_ENV dev
env DRUPAL_ENV=dev;
By organizing configurations in this way, you can manage environment-specific settings more effectively, ensuring that each environment has the correct configuration.
Published By: Krishanu Jadiya
Updated at: 2024-08-03 01:15:56