Using drush command line with Drupal multisite.

Using drush command line with Drupal multisite.

Few tips and tricks running Drupal multisite (for both Drupal 7 and Drupal 8).
Vladimir Roudakov

Drush

Drush is a command line tool that Drupal develops used for years. Drush allows you to control your Drupal instance from site installation to minor tasks brought by specific modules.

Multisite

Drupal multisite is Drupal mode where 2 or more websites are sharing same Drupal core and possibly various Drupal extensions (themes, modules, libraries) or can have their own variations.

One of the good multisite examples is where you have various vendors with some common functionality and some vendor specific functionality e.g. vendor1.site.com and vendor2.site.com

sites.php

sites.php file is located in <root>/sites/sites.php (or can be copied from <root>/sites/examples.sites.php template). It contains all the data required to define multisite.

Let's say, you have 2 websites in your <root>/sites/ folder site1 and site2. Let's say you have few environments for each site with the following URLs:

  • Local: site1.local and site2.local
  • Development: dev.site1.com and site2.dev-some-hosting.com
  • Production: site1.com and www.site2.io

So your sites.php files would look like this:

$sites = [ 'site1' => 'site1', 'site1.local' => 'site1', 'dev.site1.com' => 'site1', ' site1.com' => 'site1', 'site2' => 'site2', 'site2.local' => 'site2', 'site2.dev-some-hosting.com' => 'site2', ' www.site2.io' => 'site2', ];

In the end we have all possible URLs on the left and existing folder on the right.

Drupal 8

Now, to target specific website in Drupal would you use -l flag in drush.

For example to clear all cache on site1 run

drush -l site1 cache:rebuild

To update database on site2 run

drush -l site2 updatedb

Drupal 7

For Drupal 7 drush requires both -l and --root parameters to work correctly for multi site e.g. If your drupal is installed in web (default on platform.sh hosting) folder you'd run the following to clear cache on site1:

drush --root=web -l site1 cache-clear all

If your drupal is installed in docroot(default on acquia hosting) folder you'd run the following command to update database on site2:

drush --root=docroot -l site2 updatedb

Conclusion

Note that some of the commands on drush 8 (last version supported for Drupal 7) and drush 9 (latest version for Drupal 8) use different syntax so you might hear to drush commands to familiarise yourself with version you are using.

Happy coding.