With its vast number of plugins, Drupal can be used to build many different kinds of websites, from simple blogs to photo journals to corporate websites. Managing all of these different kinds of websites with Drupal's graphical management interface can become a bit of a chore, because while a GUI makes it easy to pick the tasks and options you want, it doesn't let you run them quickly. That's where Drush, a Drupal command-line shell, makes a Drupal administrator's job easy. Drush can make it easy for you to perform an internal cleanup, do a fresh Drupal install, install and enable modules, or create users, among other things.
Drush is available in the software repositories of many distributions, but you probably won't find the most recent stable release there – and it's a good idea to always use the most recent stable release of any application that runs on or controls other software that runs on your server. Installation is simple; just download the latest available tarball and extract the files to wherever you like, such as the /usr/local/ directory or maybe the ~/Downloads directory.
The extracted drush directory contains a script called drush. You must get this script into $PATH. The simplest way to do that is to create a symbolic link between the script to a directory already in $PATH. The command ln -s /usr/local/drush/drush /usr/bin/drush will do just that. Change the command as needed to reflect the correct path to the drush script on your machine. The README.txt file in the extracted drush directory suggests a few other alternatives to get the drush script into $PATH.
ln -s /usr/local/drush/drush /usr/bin/drush
You may need to install a few things before you can use Drush to manage your Drupal websites. You need a working PHP and MySQL installation. You need to get the php5-cli, php5-gd, and php5-mysql packages. And you need to create a MySQL user to use with Drush.
Now you're ready to see how Drush can make you more productive. Let's try to install Drupal with Drush. Open a terminal and switch to the /var/www/ directory. The command drush dl drupal will download the latest stable Drupal release into the current directory and extract the files. You might have to use sudo or su for this and subsequent commands depending on your distribution and permissions.
drush dl drupal
The drush dl command can similarly be used to download Drupal modules. If you don't specify a version with the command, Drush will download the latest stable version. If, instead, you'd like to download, say, Drupal 7.2, use the command drush dl drupal-7.2; the command works the same way for Drupal modules.
drush dl drupal-7.2
You've just downloaded the core Drupal files; the next step is to perform the actual installation. Again, you might run into some permission issues; make sure the drupal/ directory is writable, and use sudo or su as needed.
Change to the drupal/ directory, and run the command drush site-install --db-url=mysql://mysqluser:passformysqluser@localhost/databasename --site-name="New Drupal Site with Drush".
drush site-install --db-url=mysql://mysqluser:passformysqluser@localhost/databasename --site-name="New Drupal Site with Drush"
The command will display a message that reads:
You are about to DROP your 'databasename' database and then CREATE a new one. Do you want to continue? (y/n): y
Here, no matter what databasename you provide, whether it exists or not, Drush will display the same message and ask for confirmation. Press y and Enter to proceed.
In this command, the site-install directive tells Drush to install the modules, themes, and configuration files for Drupal. The next argument specifies the database parameters, such as the username, password, and database name. Enter the username credentials you used when creating the MySQL user. Finally, use the optional --site-name directive to give a name to your Drupal website, or you can specify a name for the Drupal site after you log in.
To test your new Drupal site, point your browser to http://localhost/drupal and sign on with the default username and password, which are both "admin."
The drush dl command can be used to download any specified module or theme from the drupal.org website; Drush doesn't work with third-party themes hosted on other websites. After downloading them, you can enable modules or themes with the drush en command. For instance, the command drush dl mollom followed by drush en mollom first downloads and then enables the Mollom module.
drush dl mollom
drush en mollom
You can similarly use the drush dis and drush up commands to respectively disable or update the specified module.
So far, we've worked with Drush from within the downloaded drupal/ directory for all our examples. But what if you have several Drupal sites? Do you have to switch into different directories for each to run the relevant Drush commands? Thankfully, the answer is no!
You can use the --uri directive to specify the Drupal website you wish to use. That is, if you have directories named drupal_site, drupal_blog, and drupal_journal in /var/www/, your Drush commands might look like:
drush dl corolla --uri=drupal_blogdrush en corolla --uri=drupal_blogdrush dl mayo --uri=drupal_journal
Adding this long string to each command gets tedious. Not surprisingly, Drush offers a workaround that involves creating an alias for the target Drupal sites. Add code like this to a new file called ~/.drush/aliases.drushrc.php:
$aliases['ds'] = array( 'root' => '/var/www/drupal_site', 'uri' => 'drupal_site',);$aliases['db'] = array( 'root' => '/var/www/drupal_blog', 'uri' => 'drupal_blog',);$aliases['dj'] = array( 'root' => '/var/www/drupal_journal', 'uri' => 'drupal_journal',);
Now, when referencing drupal_site, irrespective of what directory you're in, you can use the command drush @ds command, and similarly @db and @dj for drupal_blog and drupal_journal. So, if you're in the ~/Music directory and suddenly realize you must download the Mayo theme for drupal_blog, just run the command drush @db dl mayo.
drush @ds command
drush @db dl mayo
We've covered only a few basic Drush commands here. You'll realize the true power of Drush only when you start using it. An increasing number of Drupal modules now offer Drush commands. Visit Drush.org for details on all Drush commands. You can also fire up a terminal and run drush --help, drush help --filter, or drush topic for details on how to use Drush. And the drush directory that we placed under /usr/local/ early on contains an examples/ directory with several files that help explain some of the tricky aspects of Drush, such as the aliases.drushrc.php file.
drush help --filter
Allowed tags: <a> link, <b> bold, <i> italics