Create a Drush alias to connect private Remote IP via ssh

What is Drush ?

Drush is a very cool command line utility to perform actions to your Drupal Application.

Drush is a very good friend of Developer. We can do perform below tasks using Drush:

  • Drush helps to manage Drupal website remotely
  • You can Install/enable module(s) using drush. You don’t need to an FTP client to do this
  • You can clear cache using Drush.
  • Check installed modules/themes, you can disable modules using this
  • Drush Provides easier way to code review and Diffs.
  • Maintaining patches using drush is very easy
  • A drush alias is a shortcut to a remote Drupal application, which can be accessible via tunnel or VPN.

    It’s great, that you found it interesting. To use drush from local machine to your private IP like your private EC2 instance.

    Here i am assuming that Drush is already installed to remote machine (If not follow the same step to configure to remote as well). I am going to explain installation, configuration and uses of drush on your local machine to remote machine.

    Install Drush via composer

    Open terminal and run below command

    curl -sS https://getcomposer.org/installer | php 
    
    mv composer.phar /usr/local/bin/composer
    
    cd ~/
    
    touch .bash_profile
    
    nano .bash_profile
    
    export PATH="$HOME/.composer/vendor/bin:$PATH"
    
    composer global require drush/drush:dev-master
    
    composer global require drush/drush:8.*
    
    

    Now Drush is installed on your machine. Now accessing private ip from your Drush alias. Now we are going to create a drush alias that will connect to your remote IP(via VPN) Assuming you already have VPN access to your remote IP.

    To write a drush alias, Open terminal by default your path will be /Users/username/. Now go to the drush folder. Tupe below command for that.

     cd /Users/kundan/.drush 

    Now you are in your Drush directory. create a new drush alais file. file naming convention will be filename.aliases.drushrc.php. To create a file run below command on terminal

    vim filename.aliases.drushrc.php 
    <?php
      
    #Alias for dev.test.com
    
    $aliases['aliasname'] = array(
      'root' => '/var/www/html/drupal',  //path to remote drupal site
      'uri' => 'dev.test.com',          // URL 
      'remote-user' => 'username',      // ssh username
      'remote-host'=> 'XX.XXX.XX.XXX,   // Your private IP 
      'ssh-options' => '-o PasswordAuthentication=yes'
    );
    
    

    To run this alias, First connect to VPN with your credentials and go to terminal.

    and run

    drush @aliasname status

    It will ask your ssh password and display detail information about your Drupal site.

    In the same way you can run all drush commands. Some are listed below:

    drush @aliasname status
    
    drush @aliasname cc all
    
    drush @aliasname pm-list
    
    drush @aliasname update
    
    

    So all this command is getting execute from local to your remote private Server. You can create multiple drush alias in a single file E.g.

    <?php
      
    #Alias for dev.tes1.com
    
    $aliases['aliasname'] = array(
      'root' => '/var/www/html/drupal',  //path to remote drupal site
      'uri' => 'dev.test.com',          // URL 
      'remote-user' => 'username',      // ssh username
      'remote-host'=> 'XX.XXX.XX.XXX,   // Your private IP 
      'ssh-options' => '-o PasswordAuthentication=yes'
    );
    
    #Alias for dev.test1.com
    
    $aliases['aliasname1'] = array(
      'root' => '/var/www/html/drupal1',  //path to remote drupal site
      'uri' => 'dev.test1.com',          // URL 
      'remote-user' => 'username',      // ssh username
      'remote-host'=> 'XX.YYY.XX.XXX,   // Your private IP 
      'ssh-options' => '-o PasswordAuthentication=yes'
    );
    
    #Alias for dev.test2.com
    
    $aliases['aliasname2'] = array(
      'root' => '/var/www/html/drupal2',  //path to remote drupal site
      'uri' => 'dev.test3.com',          // URL 
      'remote-user' => 'username',      // ssh username
      'remote-host'=> 'XX.XXX.XX.XXX,   // Your private IP 
      'ssh-options' => '-o PasswordAuthentication=yes'
    );