In this article, it demonstrates you how to config SSH to run WP-CLI from remote server via a client.
Before you start, make sure WP-CLI and SSH are installed on both the client machine and the remote server.
Check whether WP-CLI is available on a remote server.
$ php wp-cli.phar --info
For convenience, rename it to wp
,make it executable and move it to somewhere in your path.
$ chmod +x wp-cli.phar
$ sudo mv wp-cli.phar /usr/jane/bin/wp
Then you can run it like below.
$ wp --info
Config SSH
Generate SSH keys
If you do not have your SSH keys yet on your client, use ssh-keygen
tool to generate a pair keys.
$ ssh-keygen -o
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/john/.ssh/id_rsa):
Created directory '/c/Users/john/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/john/.ssh.
Your public key has been saved in /c/Users/john/.ssh/id_rsa.pub.
The key fingerprint is:
c0:72:36:8e:d7:f1:aa:75:s3:53:96:d8:49:85:36:e3 john@mylaptop.local
In the process, it will ask you whether to save the key in the default file, press enter to use the default file. Then it ask you whether to set a passphrase, leave it empty if you do not want a password for the key.Then a pair key will be generated, one(id_rsa) is private key, the other(id_rsa.pub) is public key.
Then just append the content of the public key file (.pub file) to the remote server’s ~/.ssh/authorized_keys
file.
# If you have send the public key file to the remote server's home directory,
# use below command to append its content to authorized_keys.
$ cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
Config SSH
In the client, edit the SSH’s configuration file ~/.ssh/config
. If it does not exist, create it and add below content to config the remote server to connect over SSH.
Host my_server
HostName xx.xx.xx.xx
Port 22
User jane
IdentityFile c:usersjohn.sshid_rsa
The first line config the remote server’s name, here we name it “my_server_name”. The following lines config its IP, port, user as well as the private key file path on your client. When connecting to the remote server, it will be used for authorization.
Config WP-CLI
WP-CLI use a config file of YAML format. You need to create a file /home/john/.wp-cli/config.yml
in the client if it does not exist. WP-CLI will automatically find it in the filesystem.
Run WP-CLI remotely on client
If you just run WP-CLI for remote server, add below content to the YAML file.
# Global parameters for remote WP-CLI
ssh: my-server:/home/jane
Run WP-CLI locally and remotely on the same client
If you want to run WP-CLI both for local WordPress and that on remote server, use an alias for remote access in the WP-CLI config file.
# Global default parameters for local WP-CLI
path: C:xampphtdocswordpress
user: xxx
# Global default Parameters for remote WP-CLI
@svr:
ssh: my-server:/home/jane
For local WP-CLI, config the client’s WordPress’s path and WordPress user name.
For remote WP-CLI, config SSH connection information as an alias @svr
. Here my-server
is the server name configured in the SSH config file, /home/jane
is the directory on remote server, when running remotely it automatically change to that directory.
Note: The path for WordPress on the remote server can be configured on remote’s WP-CLI config file.
# Global default parameters path: /opt/wordpress/htdocs
Run WP-CLI locally and remotely on the same client.
# Local WP-CLI info
$ wp --info
# Remote WP-CLI info
$ wp @svr info
Resource
- WP-CLI markdown post tool GitHub repository.
A custom WP-CLI command tool which allows you to publish and update posts with markdown files in a terminal.
-
WordPress Git post tool GitHub repository
A tool that allows you to publish or update posts to a remote WordPress site with Git commands.