Drupal: Drush

To install Drush you need PEAR – make sure it is installed on your system before continuing.

yum -y install php-pear
pear -V

After confirming that PEAR works, enter

pear channel-discover pear.drush.org

If PEAR is correctly installed installed you should see a message “Adding Channel Pear Drush.org Succeeded. If you don’t and you get the error ini_set() has been disabled then you need to make sure to allow ini_set in your php configuration file – typically stored at /usr/local/lib/php.ini.
Now, enter

pear install drush/drush

If you want to know the installation path type:

cat `which drush` | grep -A1 "# Build the path to drush.php." | grep "SCRIPT"

This command will actually install drush on CentOS. You can also use it on other Linux distributions. Drush requires Console_Table:

pear install Console_Table-1.1.5

Now you can run drush. To verify that drush works correctly, run sudo drush or if you’re logged in as root user: drush.
Now enter whereis drush to see where drush is installed on your CentOS machine, i.e. /usr/bin/drush.

Drush automatically determines the user’s home directory by checking for HOME or HOMEDRIVE/HOMEPATH environment variables, and the temporary directory by checking for TEMP, TMP, or WINDIR environment variables. The home path is used for caching Drush commands and the git –reference cache. The temporary directory is used by various commands, including package manager for downloading projects. You may want to specify a path that is not user-specific here; e.g., to keep cache files on the same filesystem, or to share caches with other users.

Drush configuration
There are two kinds of configuration files for Drush: Drush resource files (drushrc.php) that contain configuration options, and alias files (aliases.drushrc.php) that contain information about the various local and remote Drupal sites that you are working on.

Drush will search for configuration files in multiple locations, the primary places being $HOME/.drush/drushrc.php for per-user configuration. Inside the drush folder, there is a directory called examples (examples/example.drushrc.php) that includes a number of sample files to help us get started on various activities. One of these is example.drushrc.php. Note: ~ is the shorthand for /home/user/.

You can greatly increase the convenience of working with multiple Drupal sites by defining shorthand names for each site using Drush aliases. Without: drush –root=/var/www/dev –uri=dev.server1.com status, with: drush @dev.server1.com status.
Aliases are not defined in drushrc.php; rather, they are stored in a separate file, named aliases.drushrc.php, that may be stored in the same locations as the standard drushrc.php configuration files. There is an example alias file in the examples directory. Preferred method is to put each alias in a separate file called ALIASNAME.alias.drushrc.php.

mkdir ~/.drush
cp /usr/share/pear/drush/examples/example.drushrc.php ~/.drush/drushrc.php
Uncomment and change: $options['alias-path'] = array('/path/to/user/.drush/aliases');
mkdir ~/.drush/aliases
vim ~/.drush/aliases/devel.aliases.drushrc.php

“devel.dev” that points to a local Drupal site named “dev.server1.com” looks like this:

$aliases['dev'] = array(
  'root' => '/path/to/drupal',
  'uri' => 'dev.server1.com',
$aliases['live'] = array (
  'root'=> '/var/www/dev',

When you have an alias like @sdevel.live that describes a Drupal site that resides on a remote server, it is possible to run remote commands on that site as easily as running a local command. Drush makes this possible by using ssh to invoke a remote instance of Drush. In order for remote Drush command execution to work, you first need to set up an ssh key pair allowing the local machine to connect to the remote machine. There is a command called pushkey that is available in the drush_extras module that will do all of the work for you. To use drush_extras, you must first download it.

drush pm-download drush_extras
drush pushkey @devel.live

It is also possible to make a trusted connection by exchanging SSH keys manually:

ssh-keygen -t rsa -C "youremail@domain.com"
# Copy to remote machine
scp ~/.ssh/id_rsa.pub username@serverip:/userhomedir/id_rsa.pub
# On remote machine
cat ~/id_rsa.pub >> .ssh/authorized_keys

Install Drupal
The profile defaults to standard, and the account name and account pass to ‘admin’. If you get an MySQL access error try mysql_secure_installation

cd /var/www
drush dl drupal --drupal-project-rename=projectname
cd projectname
drush si --yes --db-url=mysql://[db_user]:[db_pass]@localhost/[db_name] --account-name="admin" --account-pass="password"

Making a local copy of a remote Drupal site
Once you have remote execution set up, the Drush core-rsync and sql-sync commands can be used to quickly copy a Drupal site from one location to another. The basic operation is the same regardless of whether one site is remote or both are local. If you would like to do a dry run once to see what Drush parameters Drush will pass to core-rsync for any particular set of sites, then you may run the command with the –simulate option first. The option –include-conf tells Drush to also copy the settings.php file. Once the files have been copied over, you can pull the database across. Note that you don’t need to create the database in advance; you can tell Drush to do that for you.

drush core-rsync @devel.live @devel.dev --include-conf --simulate
drush sql-sync @devel.live @devel.dev --create-db

Leave a Reply