Run Drush from any directory with Site Aliases
Are you sick of having to always be in the directory that your Drupal installation is in order to run Drush commands? What if you run more than one site? Changing directories all the time is a pain.
Or if run a multi-site setup and you try and run a Drush command from the root of the Drupal install (the directory where index.php is), you will get an error. This is because if you work with a multi-site setup, you need a valid Drupal installation to run Drush commands against. Drush will look for the settings.php file inside the sites/default directory, but will not find it there as each site has its settings.php in its own site folder (sites/sitename). So you have to change to the site directory for each site you want to run Drush against. That gets tiring real quick!
There are a couple of potential solutions for multi-sites and single sites which we will go over quickly, before the real solution - Drush Site Aliases.
One solution is to use the --uri option with each command. For example, clearing cache on a site called example.com:
drush cc all --uri=example.com
That's a lot of extra typing, but it is better than having to change directory all the time.
What happens if you want to run Drush outside of the Drupal projects directory entirely? Using --uri is not enough, you need to add --root as well to tell Drush where to find the install of Drupal.
drush cc all --root=/var/www/example --uri=example.com
Add the root option to specify the particular Drupal instance you want. For example:
drush cc all --root=/var/www/example
The above solutions will work, but they all involve a lot of extra typing and are prone to error from mistyping. What if you could run Drush with a specified site from anywhere with just a few extra characters? Thanks to Drush site aliases, you can.
Drush Site Aliases
When you use Drush site aliases, all you need to do is all @ and the name of the alias with the command. For example:
drush @ex cd all
In the above example, ex is the alias for a site called example.
How to setup an alias for example
- cd ~/.drush
- sudo nano ex.alias.drushrc.php
- Add the following:
- * @file
- * Site alias for example.com
- $aliases['ex'] = array(
- 'root' => '/var/www/example',
- 'uri' => 'vm.example.com',
The root path is the path to the root of the Drupal install (the directory that contains index.php), even if you are running a multi site.
The uri is the domain name you use to access your site. I tend to use a sub domain of vm for dev sites.
The key in the $aliases array is the alias you want to use, which is 'ex' in this case. This is also used in the first part of the file name, ex.alias.drushrc.php.
And that is all there is to it. You can add an alias for all the Drupal sites you are working on and save a bunch of time.
Related Tutorials & Guides
New comments for this tutorial have been turned off.