Managing Drupal 8 applications remotely using Drush aliases
Have you ever thought that your business needs to make sure that your web application has a quick release in order to sustain the long race. This sort of has become easy to manage by continuous development & continuous integration using Drush & Drush Aliases. Drupal web development is one such place where multiple command-line interface (CLI) tools are available to make developer’s life easy, and among them, the two important things are Drush and Drupal Console.
In this blog, we will take a brief look at Drush & Drush Aliases and how it can make developer’s tedious manual web development tasks easy by offering various commands to replace the manual steps. You may also like to check out our previous blog where we have discussed how to write the custom Drush Commands in Drupal 8.
First thing first, what is Drush?
Drush is a Command Line Shell Interface, which provides its own dictionary of commands to interact with Drupal modules/ themes/ profiles. For instance, If you want to enable the Drupal project (modules) on your website then all you need to do is execute the following command in your CLI.
How to install Drush in Drupal 8?
The easiest way to install Drush on Drupal 8 instance is by using Composer. Just hit the below-mentioned command in your CLI and you are done with installation. Learn more about installing Drupal with drush on our previous blog.
What are Drush Aliases?
An ‘Alias’, in general, is nothing but a small representation/acronym for something bigger. In case of Drush, the ‘Aliases’ are a short form of Drush Commands. Quite often when we have to ssh on our remote servers to perform different operations, the process becomes a tedious task and even more tedious when we have multiple instances. This is where Drush Aliases comes to our rescue.
Here you have three instances “Dev > Stage > Production”, remember, all the three instances can be configured by Aliases so that Drush can perform its tasks on the remote server(s) from your local server.
Some of the common tasks can be ‘Database Sync’, ‘Files Sync’, ‘Cache Clearance’, ‘Module Operations’, ‘importing and exporting the configuration’ e.t.c.
How to set up local Aliases?
- Create an alias file in .drush folder of your home directory(~/.drush) to make Drush aliases (short commands) accessible from anywhere. Drush alias file must be named similar to the below convention.
Make sure your newly created alias file contains below snippet.
Similarly, you can create alias file for remote servers as well.
2. The following snippet, when placed in your alias file, will automatically create aliases for all of your projects.
Note: You can add $local_sites path and /docroot as per your local configuration.
If you are using Acquia and Pantheon as a remote server, you can follow the below-mentioned steps to set up the aliases on your local machine.
Acquia and Drush Aliases
- Sign in to the Acquia Cloud interface using your credentials.
- Click your name in the upper left corner and then click Edit profile.
- On the Profile page, click Credentials and then enter your Acquia password.
- Under Drush integration, click Download Drush aliases
- Extract the downloaded archive file into $HOME:
- tar -C $HOME -xf $HOME/Downloads/mysite.tar.gz or you can place mysite.aliases.drushrc.php file into .drush folder manually.
- Now go to your terminal and run drush sa command to check aliases.
Pantheon and Drush Aliases
- Sign in to Pantheon dashboard using your account details.
- On the home page you will find Drush Aliases, click on it to download the file.
- Now you can place that file inside .drush folder.
Drush commands - A fast and easy-to-use command reference for Drush users.
$ drush @staging updatedb - This command is used to run pending updates on staging site.
$ drush rsync @staging:%files/ @live:%files - You can use this command to Synchronize staging files to production.
$ drush sql-sync --structure-tables-key=custom @live @local - This command is used to Synchronize database from production to local, excluding the cache table.
$ drush cex @dev, $ drush cim @live - This command is used to export and import the configuration.
Okay so now you know what is Drush and Drush Aliases and how it makes life easier for both Drupal developers and site administrators. We recommend you to try out Drush Commands to find out what works well for your current project or requirements. But there are many more advanced commands that are really worth learning.
If you have any suggestions or queries please comment down let me try to answer.