Drupal 8 configuration management using Drupal Console

Viktor Šulák
3 min readApr 14, 2020

--

Do you have a website where you need to synchronize configuration of development and production environment? That’s exactly the purpose of Drupal configuration management. It is possible to use it using UI, Drush, or Drupal Console. We focus on Drupal Console since if is a great tool for automation.

What configuration does this manage?

Installed modules, module settings, content types, basically everything you change using admin UI. Everything except content.

How does this configuration look like?

All the small pieces of configuration are exported into separate YAML files. Their respective names refer to their module, field, block, etc.

Example of several YAML configuration files

Why do I want this?

If you are working with versioning software like Git, it is easier to keep track of changes. It is easy to maintain website for enterprise client without having their real data. You don’t have to repeat changes using UI or send a database to someone. It is also great to synchronize configuration changes between developers.

How do I do this?

Well, you have two options we will cover. First is to do it using Admin UI. You go to Manage > Configuration > Development > Configuration Synchronization (admin/config/development/configuration) and you will see configuration synchronization page. You will see differences between database and active configuration. You can export and import whole configuration. Import and export is in form of tarred archive.

Our favourite option is to use Drupal Console. drupal config:export and drupal config:import is all you need. We can export local changes of several developers and merge them using git. Then it is all deployed and imported to development, then pre-production and then production environment.

It is good practise to make a database dump before you import configuration. Just in case. But if you are able to import configuration on your local machine (with clean installation of vendors if using Composer), it is almost sure that you will be able to import it also on server.

Is it safe to use?

Well, we suggest that you move configuration folder outside of project web root and put there .htaccess file rejecting all connections. To do this, we will need to set configuration sync directory in sites/default/settings.php and create the folder relative to web root (relative to index.php).

$config_directories[CONFIG_SYNC_DIRECTORY] = ‘../config’;

Using this, we tell Drupal to put configuration one level up from index.php and into folder config. If it is not possible to put it in this structure because of your hosting, just use the web root for configuration folder

$config_directories[CONFIG_SYNC_DIRECTORY] = ‘./config’;

To make things more secure on Apache, you have to put .htaccess file into this folder. If you use other web server than Apache, you will have to disable it in host configuration.

# Deny all requests from Apache 2.4+
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
# Deny all requests from Apache 2.0–2.2
<IfModule !mod_authz_core.c>
Deny from all
</IfModule>
# Turn off all options we don’t need
Options -Indexes -ExecCGI -Includes -MultiViews
# Set the catch-all handler to prevent scripts from being executed
SetHandler Drupal_Security_Do_Not_Remove_See_SA_2006_006
<Files *>
# Override the handler again if we’re run later in the evaluation list
SetHandler Drupal_Security_Do_Not_Remove_See_SA_2013_003
</Files>
# Disable the PHP engine
<IfModule mod_php5.c>
php_flag engine off
</IfModule>

Conclusion

You want it. Seriously. It is good to synchronize data between environments and to track your changes so you will be able to return to them if needed. And it can be used to save a lot of time if you use CI/CD pipelines to deliver your Drupal site.

--

--

Viktor Šulák

Software engineer, traveller, photographer, motorcycle rider. Interested in fiddling with new technologies.