Skip to content

WordPress WP-CLI on remote servers via SSH — patterns that hold up

WordPress WP-CLI is built to run remotely via SSH using --ssh, alias files, and ~/.wp-cli/config.yml. The patterns that keep one operator command working across 40 servers.

A WordPress operator runs the same wp option get siteurl against 40 client sites a week. Doing it as ssh client1 'cd /var/www && wp option get siteurl' works once and degrades quickly — different paths, different PHP binaries, different shells with different $PATH. WP-CLI’s --ssh flag and alias system collapse all of that into wp @client1 option get siteurl and a wp-cli.yml that lives in a Git repo.

What’s happening

wp --ssh=user@host:port/path opens an SSH connection, locates the WordPress install at path, and runs the command remotely with the local wp binary streaming arguments and output. The remote server only needs wp on $PATH and a PHP version WordPress accepts. Aliases are named shortcuts to that connection string, stored in ~/.wp-cli/config.yml or in a project-local wp-cli.yml. The local-vs-project precedence is the same as Git config.

The two failure modes are PATH and PHP. If SSH login shells differ from interactive shells (Bash login vs Bash non-interactive), wp may not be on $PATH for the non-interactive session WP-CLI opens. And if the server has PHP 7.4 on $PATH but the site runs under PHP 8.2 via FPM, WP-CLI runs against the wrong binary.

How to verify

Test a single host’s WordPress WP-CLI reachability before adding it to the alias file:

ssh client1.example.com 'which wp && php -v && ls /var/www/example.com/wp-config.php'

If which wp returns empty, install WP-CLI globally on the host or symlink it under /usr/local/bin/wp. If php -v shows 7.x but the site needs 8.x, write a wrapper:

# /usr/local/bin/wp on the remote host
#!/bin/bash
/opt/php82/bin/php /usr/local/bin/wp.phar "$@"

Then confirm WP-CLI works through SSH from your workstation:

wp --ssh=client1.example.com/var/www/example.com option get siteurl

What to do

Set up an alias-driven workflow, then layer scripts on top.

  1. Create ~/.wp-cli/config.yml for personal aliases and wp-cli.yml in the project repo for shared ones:
@client1:
  ssh: deploy@client1.example.com/var/www/example.com
@client1-staging:
  ssh: deploy@staging.client1.example.com/var/www/staging.example.com
@all:
  - @client1
  - @client1-staging
  1. Confirm the alias works: wp @client1 core version. If it does, every other WP-CLI command works by prepending @client1.

  2. For commands that mutate state, force confirmation with --prompt:

wp @client1 user delete 42 --prompt
  1. For commands that stream lots of output (wp post list --format=csv), let WP-CLI handle the SSH framing rather than piping over a manual ssh call — UTF-8 stays intact and partial reads don’t truncate.

  2. SSH config is the right place for jump hosts and key selection — don’t embed those in the alias. ~/.ssh/config entries like Host client1.example.com / ProxyJump bastion.internal / IdentityFile ~/.ssh/id_client1 keep WP-CLI aliases declarative.

  3. For multi-site portfolios, generate the alias file from an inventory:

# inventory.csv: alias,user,host,path
while IFS=, read -r a u h p; do
  printf "@%s:\n  ssh: %s@%s%s\n" "$a" "$u" "$h" "$p"
done < inventory.csv > ~/.wp-cli/config.yml
  1. The @all group runs against every alias in sequence: wp @all core version reports core versions across the portfolio in one command — exactly the kind of question that needs an answer in 90 seconds when a CVE drops.

For running mutations across the alias group safely, see WordPress fleet batch operations. Stack Harbor manages portfolios this way under enterprise WordPress hosting.

If a team is still doing one-at-a-time SSH sessions for WordPress operations, we can replace that with a real fleet workflow.