Drupal Config Sync with Drush Commands

This guide explains how to sync configurations in Drupal using Drush commands, tailored for different environments such as development, staging, and production.

1. Install Drush

Make sure Drush is installed on your system. You can install it globally using Composer:

composer global require drush/drush

2. Directory Structure

Organize your configuration directories for different environments. For example:

config/
  dev/
  stage/
  prod/

3. Update 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';
}

4. Export Configuration

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

5. Commit Configuration

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

6. Pull Configuration

Pull the latest changes from your version control system on the target environment:

git pull origin main

7. Import Configuration

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

8. Clear Cache

Clear the cache after importing configuration changes:

drush cr

Setting Environment Variables

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:

Apache example:

SetEnv DRUPAL_ENV dev

Nginx example:

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

Card Image

How to Set Up a Local SSL Certificate on Apache: Step-by-Step Guide

Learn how to set up a local SSL certificate on Apache with this comprehensive step-by-step guide. Secure your local development environment with HTTPS.

Card Image

Latest Features of Coding Technology

Explore the latest features and advancements in coding technology, including new programming languages, frameworks, DevOps tools, AI integration, and more.

Card Image

Understanding Laravel Mix Webpack Configuration: Step-by-Step Guide

Step-by-step explanation of a Laravel Mix Webpack configuration file, including asset management for JavaScript, CSS, and Vue.js support.

Card Image

How Emojis Can Enhance Your Git Commits | Gitmoji Guide

Discover how to enhance your Git commits with emojis. Learn about the best practices for creating informative and visually distinctive commit messages.