Running WP-CLI commands remotely

by Jun 1, 2023WordPress Maintenance, WP-CLI17 comments

Kinsta - Unlock 4 Months OFF Annual WordPress Plans

Managing multiple WordPress sites can be a daunting task, especially when it comes to performing routine maintenance tasks like updates, plugin management, and backups. However, thanks to WP-CLI (WordPress Command Line Interface) and SSH (Secure Shell), administering remote WordPress sites has become much more efficient and streamlined.

In this article, we will explore how WP-CLI and SSH can revolutionize the way you manage your WordPress sites, making your life as a web administrator much easier.

What is WP-CLI?

WP-CLI is a powerful command-line tool that allows you to manage various aspects of your WordPress sites directly from the terminal. It provides a wide range of commands to perform actions such as installing and updating plugins/themes, managing users, generating content, and much more. WP-CLI operates using the WordPress REST API, enabling you to interact with your WordPress site without needing to access the admin dashboard.

WP-CLI Logo

Why SSH?

SSH, or Secure Shell, is a network protocol that provides a secure way to access and manage remote servers. It establishes an encrypted connection between your local machine and the server, allowing you to execute commands remotely.

SSH is widely used in the web development world for tasks such as server administration, file transfers, and remote debugging.

Setting up WP-CLI and SSH:

To start using WP-CLI and SSH for managing your WordPress sites, you’ll need to set up both tools on your local machine and the remote server. Here’s a quick overview of the process:

  • Installing WP-CLI: WP-CLI is a command-line tool that runs on top of PHP. You can install it globally on your local machine using a package manager like Homebrew (for macOS/Linux) or Chocolatey (for Windows). Once installed, you can verify the installation by running the “wp” command in your terminal. You can find detailed installation steps on the official WP-CLI website (https://wp-cli.org/).
  • Enabling SSH Access: To use SSH, you need to ensure that the remote server allows SSH connections. Most hosting providers offer SSH access, but you may need to enable it through their control panel or contact your hosting support. Once SSH access is enabled, you can connect to the server using the SSH client available in your operating system’s terminal. If SSH access is not enabled, check your hosting control panel or contact your hosting support to enable it.

Setting up WP-CLI Aliases

WP-CLI aliases provide a convenient way to execute commands on any WordPress installation by setting up shortcuts in your wp-cli.yml or config.yml file.

For example, let’s say you’re working on a client development environment using Vagrant and you’ve registered a new rewrite rule. To flush the rewrites inside your Vagrant-based virtual machine, you can simply run:

# Run the flush command on the development environment of your client
$ wp @client-dev rewrite flush
Success: Rewrite rules flushed.

Then, once you push the site to production, you can run:

# Run the flush command on the production environment of your client
$ wp @client-prod rewrite flush
Success: Rewrite rules flushed.

With WP-CLI aliases, you no longer have to go through the process of SSHing into machines, changing directories, and spending precious time just to access a specific WordPress installation. By specifying the machine to work with, WP-CLI handles the connection seamlessly, eliminating the need for manual setup.

Moreover, WP-CLI alias groups allow you to create sets of aliases. By defining a group, such as “@client_name“, you can conveniently execute a command simultaneously across multiple configured example sites. This simplifies the process and allows for efficient management of multiple sites with a single command.

# Run the update check on both environments dev & production
$ wp @client-name core check-update
Success: WordPress is at the latest version.
Success: WordPress is at the latest version.

Aliases can be registered in your project’s wp-cli.yml file, or your user’s global ~/.wp-cli/config.yml file. See the example below:

# Aliases to several WordPress installs 
# An alias can include 'user', 'url', 'path', 'ssh', or 'http'

@client-prod:
  ssh: production@domain.com
  user: production
  path: /path/to/public_dir/

@client-dev:
  ssh: vagrant@192.168.50.10
  user: vagrant
  path: /path/to/public_dir/

@local:
  ssh: vagrant:default

@client:
  - @client-prod
  - @client-dev

Using WP-CLI aliases offers several benefits that save time and streamline tasks, especially when it comes to running batch operations. Here are a few reasons why WP-CLI aliases are great for managing WordPress sites efficiently:

  1. Time-saving convenience: WP-CLI aliases eliminate the need to manually SSH into individual machines or change directories to access specific WordPress installations. By setting up aliases, you can quickly switch between sites and execute commands without repetitive steps, saving valuable time.
  2. Seamless connection handling: Once you define the aliases in your configuration file, WP-CLI knows how to establish the actual connection to the specified machines. This automated connection handling simplifies the process and eliminates the need for manual intervention, making it easier to manage multiple sites efficiently.
  3. Batch operations: WP-CLI aliases enable you to create alias groups, allowing you to perform batch operations across multiple sites simultaneously. Whether it’s updating plugins, performing backups, or running other routine tasks, using alias groups eliminates the need to execute commands individually for each site. This streamlines the process and boosts productivity.
  4. Consistent commands across sites: With aliases, you can execute the same WP-CLI commands across different WordPress installations consistently. This ensures that your batch tasks or configurations are applied uniformly, reducing the chances of errors or inconsistencies that may arise from manual execution.
  5. Scalability and flexibility: As your WordPress site portfolio grows, managing numerous installations can become challenging. WP-CLI aliases provide a scalable solution that allows you to easily manage an expanding number of sites. You can add, modify, or remove aliases as needed, adapting to your evolving requirements with ease.

By leveraging WP-CLI aliases, you can harness the power of automation, streamline your workflow, and efficiently manage multiple WordPress sites. The time saved and the ability to run batch tasks seamlessly make WP-CLI aliases an invaluable tool for web administrators and developers.

Further reading material:

Kinsta - Unlock 4 Months OFF Annual WordPress Plans
5 2 votes
Article Rating
Subscribe
Notify of
guest
17 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
RJ Sanchez
10 months ago

Awesome Blog. Good in depths and details regarding the main issues and probable solution

Mario
Mario
10 months ago

Awesome article!! Great work.

Rolando
10 months ago

Great article, txs Salvador for always keeping us updated 🙌

Arian
Arian
10 months ago

Such a great article about this, sometimes, complex topic. I did share it with a folk who was struggling trying to use some of the things that are so well explained here, and he nailed it, then, highly recommended.

Byron
Byron
10 months ago

Very useful information, I will be looking forward to more wp-cli Articles, and Cron Automation.

Bayardo Rivas
Bayardo Rivas
10 months ago

Hey Salva, this an amazing post, thanks for sharing.

Danfer
10 months ago

Great post Salvador! I assume you can use these scripts to automate tasks, am I right?

Roberto
10 months ago

Great article máster. Thanks for the info.

Roberto Remedios
10 months ago

Great article máster. Thanks for the info. You should send this as a Wordcamp talk.

Alvaro
10 months ago

Great work! I didn’t know about WP-CLI. This blog has inspired me to dive deeper into WordPress and search for more information.

Bob Remedios
Bob Remedios
4 months ago

Great post

4 Local Development Tools for WordPress

4 Local Development Tools for WordPress

Local development environments are essential for WordPress developers to build, test, and debug websites and plugins without affecting live sites. Several tools have emerged over the years to simplify this process, making it easier for developers to work locally. In this article, we’ll compare four popular local development tools for WordPress: LocalWP, DevKinsta, Lando, and DDEV.

read more
Installing WP-CLI on your MacOS (via Homebrew)

Installing WP-CLI on your MacOS (via Homebrew)

As a WordPress developer or enthusiast, having the right tools at your disposal can greatly enhance your productivity. In this tutorial, we will walk you through the process of installing Homebrew and WP-CLI on macOS. Homebrew is a package manager for macOS, while WP-CLI is a command-line tool for managing WordPress installations. Let’s dive in and get started!

read more
How to debug an error in WordPress?

How to debug an error in WordPress?

WordPress is an incredibly powerful and versatile content management system that allows people to create stunning websites with ease. However, like any complex software, it can occasionally throw up errors that can be frustrating to deal with. Debugging an error in WordPress can be a time-consuming process, but with the right approach, it is possible to identify and fix the issue.

read more
Cool Snippets for wp-config.php

Cool Snippets for wp-config.php

As a WordPress developer, you may already be familiar with the wp-config.php file. This file is an essential part of your WordPress installation and contains important configuration information. But did you know that you can enhance your WordPress site’s functionality and performance by adding some cool and useful snippets to this file? In this blog post, we’ll explore 12 snippets that you can add to your wp-config.php file to improve your WordPress site.

read more
Doing cool things with WP-CLI

Doing cool things with WP-CLI

If you’re managing a WordPress website, you know that there are a lot of tasks that need to be performed regularly to keep your site up and running smoothly. While there are many ways to accomplish these tasks, one tool that can make your life easier is WP-CLI.

WP-CLI is a command-line interface for WordPress that allows you to manage your website from the terminal. In this post, we’ll look at some of the cool things you can do with WP-CLI to manage your WordPress website.

read more
6 Reasons to use a WordPress Maintenance Service

6 Reasons to use a WordPress Maintenance Service

If you own a WordPress website, you may already know the importance of keeping it up-to-date and secure. However, maintaining a website can be time-consuming and sometimes requires specialized knowledge. This is where a WordPress maintenance service can come in handy. In this blog post, we will discuss why paying for a WordPress maintenance service is a good idea for website owners.

read more
Buy me a Beer
Ad - Web Hosting from SiteGround - Crafted for easy site management. Click to learn more.
Sucuri - Complete end-to-end security