Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Drush Aliases Tutorial

Introduction to Drush Aliases

Drush is a command-line shell and scripting interface for Drupal. One of its powerful features is the ability to use aliases, which allow you to run Drush commands on remote sites or different environments (like development, staging, and production) without needing to SSH into each server.

What are Drush Aliases?

Drush aliases are shorthand notations for different Drupal sites or environments. They allow you to define configurations that point to different Drupal installations, which can be on the same server or on different servers. By using aliases, you can quickly switch contexts and run commands on any defined site.

Setting Up Drush Aliases

To set up Drush aliases, you will need to create an aliases file. This file typically resides in the Drush directory under your home directory. The common location is:

~/.drush/aliases.drushrc.php

In this file, you can define your site aliases.

Creating a Simple Alias

Here’s an example of how to create a simple alias for a local Drupal installation:

Example Alias Definition

$aliases['local'] = array(
    'uri' => 'http://localhost:8080',
    'root' => '/path/to/drupal/root',
    'db-url' => 'mysql://user:password@localhost/database',
);
                

In this example, we are defining an alias called local. The uri refers to the site's URL, root specifies the path to the Drupal root directory, and db-url provides the database connection string.

Using Aliases

Once you have defined your aliases, you can use them in Drush commands. For example, to run a command on the local site defined above, you would use:

drush @local status

This command retrieves the status of the Drupal site specified by the local alias.

Remote Aliases

You can also define aliases for remote Drupal sites. Here’s an example:

Example Remote Alias Definition

$aliases['production'] = array(
    'uri' => 'https://example.com',
    'root' => '/var/www/example',
    'remote-host' => 'example.com',
    'remote-user' => 'username',
);
                

In this example, the production alias is set up to connect to a remote server. The remote-host specifies the hostname of the remote server, and remote-user is the SSH user.

Running Commands on Remote Sites

To execute a command on a remote site, use the alias just like you would for a local site:

drush @production cr

This command would clear the cache on the production site defined by the production alias.

Managing Aliases

To list all available aliases, use the following command:

drush sa

This command will show you all site aliases defined in your aliases file.

Best Practices for Drush Aliases

When working with Drush aliases, consider the following best practices:

  • Keep your aliases file organized and well-commented.
  • Use descriptive names for your aliases to avoid confusion.
  • Regularly review and update your aliases to reflect changes in your environments.
  • Secure sensitive information such as database credentials by using environment variables or secure storage solutions.

Conclusion

Drush aliases provide a powerful way to manage multiple Drupal installations and streamline your workflow. By setting up aliases, you can easily switch between environments and run commands without the hassle of SSHing into servers. With the examples and best practices outlined in this tutorial, you should be well on your way to mastering Drush aliases in your Drupal development.