Skip to main content

Using Drush to administer multisite

One of the admiring features about Drupal is its ability to leverage single code base to power a stack of sites. Drush the excellent command line utility eases the work of administering Drupal sites.

Our recent work raised a question, will these two blend & work together?

Of course we tried this, the answer is yes but to be used with more caution!!

In case of Drupal multisite, Apache (or any Web server) is configured such that more than one domain name points to same document root. Drupal by identifying the request originating domain and folder structures loads the respective modules/themes & database. Drush utilizes the similar approach.

In case of multisite, to run a drush command against a specific site/database there are two ways,

1. Switching to respective site folder

In this case cd to the site folder. Ideally from inside the folder sites/ or similar the drush commands to be issued. This will run all the commands against site.

Assume multisite setup with separate database per site. Then

$ drush sql-connect

would return 

user@hostname:/var/www/sites/$ drush sql-connect
mysql --database=example_one --host=localhost --user=example_one --password=example1

2. Passing additional argument to drush commands

This works by passing argument -l (or --uri). The request originating domain is mentioned explicitly here and hence takes more precedence over #1.

user@hostname:/var/www/sites/$ drush sql-connect -l
mysql --database=example_two --host=localhost --user=example_two --password=example2
Since in multisite environment resources are shared extra care to be taken when commands like pm-updatecode, updatedb, etc. as these are more likely to perform write operation on database and/or files and hence could affect more than one site at once. So it is recommended to use drush for limited cases when it comes to administering a multisite environment.