victoria web design information architecture (IA)

Analyze

victoria web design

Design

Drupal CMS Web Development

Develop

victoria website Maintenance

Launch

How to Setup Drush on Shared Hosting

2015-10-06: Mac OS 10.11 El Capitan Note: With the new operating system, Apple has clamped down on installing applications in /System, /bin, /usr (exception: /usr/local), and /sbin with the policing of the 'System Integrity Protection' measure. In some cases, using symlinks can help get your now legacy software installs of Vagrant or Drush to work. For more info on these directories, see: http://pointlessramblings.com/posts/El_Capitan/. For more detail on El Capitan, see Ars Technica here.

This article is primarily about setting up Drush 8 (or others) on remote shared hosting accounts which may require some custom configuration in order to access those tools remotely. This article assumes you know what Drush is but if you don't, you can read about it from Drupal.org as well as hundreds of other websites.

These instructions are assuming that:

  • you already have your public key on your remote server (required for the particular case of using Drush aliases)
  • you are logged in via SSH. If you require help on setting this up, please refer to external this article.
  • you have at least PHP 5.4.5+ since both Composer and Drush 8 require that minimum.
  • your host is running Linux.
Drush Compatibility Table - (updated: 2016-05-05)
Drush Version Drush Branch PHP Compatible Drupal versions Code Status
Drush 9 master 5.5+ D7, D8
Drush 8 8.x 5.4.5+ D6, D7, D8
Drush 7 7.x 5.3.0+ D6, D7
Drush 6 6.x 5.3.0+ D6, D7 Unsupported
Drush 5 5.x 5.2.0+ D6, D7 Unsupported

Summary of Commands to Install Drush 8

cd ~
mkdir ~/bin
curl -sS https://getcomposer.org/installer | php -- --install-dir=bin
(maybe)mv composer.phar bin/composer
(in ~/.bashrc) alias composer="/usr/bin/php-cli ~/bin/composer.phar"
source ~/.bashrc
composer global require drush/drush
(in ~/.bashrc) [ -z "$PS1" ] && return
(in ~/.bashrc) export PATH=$PATH:~/.composer/vendor/drush/drush
(in ~/.bashrc) alias drush='~/.composer/vendor/drush/drush/drush'
source ~/.bashrc
drush core-status

Below, I'll outline what all these steps are in detail.

Steps to Install Drush 7 or Drush 8

  1. For Drush 7 and up it's advisable to use Composer which known as a 'Dependency Manager for PHP', so we're going to start with downloading it first, then moving it into a bin directory. Start by going to your home directory with: cd ~ and then creating a bin directory if one doesn't already exist with: mkdir ~/bin followed by
    curl -sS https://getcomposer.org/installer | php -- --install-dir=bin. You should get a message saying something like
    Composer successfully installed to: /home/[your-account]/bin/composer.phar
    (Note: If it was instead installed in your main account directory, create the composer directory with: mkdir ~/bin/composer and move Composer into that directory with mv composer.phar bin/composer)
  2. We next need to figure out where php-cli is on our on remote hosting account, which you can do with the command: which php-cli. which can tell you for example that it's at /usr/bin/php-cli. We take this information and add it into ~/.bashrc. This tells the system where to run composer commands from. Edit ~/.bashrc by adding this to the end Alias section: alias composer="/usr/bin/php-cli ~/bin/composer.phar". Save and reload the information in this file by typing: source ~/.bashrc (Note: see section 'Drush Aliases and Accessing your Remote Machine' below for why we use this file and not .bash_profile)
  3. At this point, if you type: composer you should get a listing of commands, and if so, we know Composer is ready for use and to help us install Drush.
  4. To do this, type: composer global require drush/drush which should install Drush 8.1 into: ~/.composer/vendor/drush/  along with dependencies such as console_table, yaml and possibly others Note: If you require specific versions of Drush, you could use instead:
    • composer global require drush/drush:8.1 for Drush 8.1, which works for 6,7 or 8 as long as you meet the min php 5.4.5 requirements
    • composer global require drush/drush:7.0.0 for Drupal 6 or 7
    • composer global require drush/drush:dev-master gives the default of Drush 9 
    In any case, Drush will be installed into your ~/.composer/vendor directory along with its dependencies.
  5. Once that is complete we need to return to our ~/.bashrc file to state where Drush is installed. Edit your ~/.bashrc file and before the line [ -z "$PS1" ] && return (Add that line at the top of your file if it doesn't already exist. More on this below), add:
    export PATH=$PATH:~/.composer/vendor/drush/drush. Then, in your Alias section, add:
    alias drush="~/.composer/vendor/drush/drush/drush" Now save and reload your .bashrc file with: source ~/.bashrc
  6. Now type: drush core-status and hopefully you should get output telling you basic info about your Drush install.

 

Here's what your .bashrc file could look like at minimum:

# .bashrc
export PATH=$PATH:~/.composer/vendor/drush/drush
[ -z "$PS1" ] && return

# Source global definitions
if [ -f /etc/bashrc ]; then
	. /etc/bashrc
fi

# User specific aliases and functions
alias composer="/usr/bin/php-cli ~/bin/composer.phar"
alias drush="~/.composer/vendor/drush/drush/drush"

Troubleshooting

If you're using Drush 7.x and got this message while running a drush command:

exec() has been disabled for security reasons bootstrap.inc:645      [warning]
The following restricted PHP modes have non-empty values:                [error]
disable_functions. This configuration is incompatible with drush. 
Please check your configuration settings in /usr/local/lib/php.ini or
in your drush.ini file; see examples/example.drush.ini for details.

...then you may need to edit you php.ini file. Don't edit the original, but make a copy of it. Typically, on shared hosting, this is kept in /usr/local/lib/php.ini, so use this command to make a copy of it:
cp /usr/local/lib/php.ini $HOME/.drush.Then within your copied file, find the line with the disable_functions = directive and remove exec from the list of disabled function names. (Note: You may have to remove everything after the disable_functions = directive in order for Drush to work properly but contact your hosting provider to see how secure that choice is.)

Congratulations! You now have Drush running on your shared hosting. The reason Composer is going to be important is that you can use it later on down the road to update Drush, so you don't have to do it manually.

Drush Aliases and Accessing your Remote Machine

The reason that the [ -z "$PS1" ] && return is important is because Drush logs into your remote machine over ssh, but since it is not a normal 'interactive' login, it doesn't source the .bash_profile file. It does, however, source the .bashrc file and although the settings above could go into .bash_profile, they need to go into .bashrc to allow remote connections with Drush. You can read more about the differences between these two files here. Making your changes here will therefore help with accessing your remote site using Drush aliases. You need this declaration at the top of your .bashrc file to avoid getting the message:

stdin: is not a tty

There are various ways of creating aliases, but I will focus on the most basic. Here is an example of a Drush alias for a basic local setup plus a remote site which is setup with a file called aliases.drushrc.php in your local ~/.drush directory:

$aliases['coolsite1.local'] = array(
    'uri'  => 'local.mysite.com',    
    'root' => '/Users/your-username/Sites/mysite/www',

);
$aliases['coolsite1.live'] = array(
    'uri'  => 'http://mysite.com',
    'root' => '/home/user/www/mysite',
    'remote-host' => 'myisp.com',
    'remote-user' => 'wwwadmin', 
);

Once you've cleared your local Drush cache with: drush cc drush, you should be able to type both: drush @coolsite1.local status and receive the status of your local Drupal site without having to be in the Drush root directory; While you should also be able to type: drush @coolsite1.live status and in a few moments, you should receive the status of your live site.

Use Drush to Synchronize Live and Local Databases

If everything is working as expected then, while you're on your local machine, you can harness the power of Drush to copy the database defined in sites/live to the database in sites/local with: drush sql-sync @coolsite.live @coolsite.local or just drush sql-sync @coolsite.live @self. Leave our the -y flag and as a safety precaution, Drush will then spell out exactly what is about to happen before it continues. Get more info on this command from the Drupal Commands website. Note that you should do your best to use the same versions of Drush both locally and remotely, otherwise you could run into issues. See this article for more info.

Use Composer to Update Drush

Besides downloading the dependencies required for various software installs, like Drush, Composer can be used to update the versions of software you installed with it by using one simple line:
composer global update. It's that easy.

Further resources: