-
Notifications
You must be signed in to change notification settings - Fork 338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new section about how to run commands remotely. #89
Changes from 3 commits
e8b5447
952b289
21db555
d00026e
3852a93
ec489c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
# Running Commands Remotely | ||
|
||
## Using an SSH connection | ||
|
||
WP-CLI accepts an `--ssh=<host>` global parameter for running a command against a remote WordPress install. | ||
|
||
Under the hood, WP-CLI proxies commands to the ssh executable, which then passes them to WP-CLI installed on the remote machine. Your syntax for `--ssh=<host>` can be any of the following: | ||
|
||
* Just the host (e.g. `wp --ssh=runcommand.io`), which means the user will be inferred from your current system user, and the path will be the SSH user’s home directory. | ||
* The user and the host (e.g. `wp [email protected]`). | ||
* The user, the host, and the path to the WordPress install (e.g. `wp [email protected]~/webapps/production`). The path comes immediately after the TLD of the host. | ||
* A known alias, stored in `~/.ssh/config` (e.g. `wp --ssh=rc` for the `@rc` alias). | ||
|
||
**Note: you need to have a copy of WP-CLI installed on the remote server, accessible as `wp`.** | ||
|
||
Futhermore, `--ssh=<host>` won’t load your `~/.bash_profile` if you have a shell alias defined, or are extending the `$PATH` environment variable. If this affects you, [here’s a more thorough explanation](https://make.wordpress.org/cli/handbook/running-commands-remotely/#making-wp-cli-accessible-on-a-remote-server) of how you can make `wp` accessible. | ||
|
||
## Aliases | ||
|
||
WP-CLI aliases are shortcuts you register in your `wp-cli.yml` or `config.yml` to effortlessly run commands against any WordPress install. | ||
|
||
For instance, when you are working locally, have registered a new rewrite rule and need to flush rewrites inside of your Vagrant-based virtual machine, you can run: | ||
|
||
``` | ||
# Run the flush command on the development environment | ||
$ wp @dev rewrite flush | ||
Success: Rewrite rules flushed. | ||
``` | ||
|
||
Then, once the code goes to production, you can run: | ||
|
||
``` | ||
# Run the flush command on the production environment | ||
$ wp @prod rewrite flush | ||
Success: Rewrite rules flushed. | ||
``` | ||
|
||
You don't need to SSH into machines, change directories, and generally spend a full minute to get to a given WordPress install, you can just let WP-CLI know what machine to work with and it knows how to make the actual connection. | ||
|
||
Additionally, alias groups let you register groups of aliases. If I want to run a command against both configured example sites, I can use a group like `@both`: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe stick with "you" rather than using "I" here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changed. |
||
|
||
``` | ||
# Run the update check on both environments | ||
$ wp @both 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: | ||
|
||
```yaml | ||
@prod: | ||
ssh: [email protected]~/webapps/production | ||
@dev: | ||
ssh: [email protected]/srv/www/example.dev | ||
@both: | ||
- @prod | ||
- @dev | ||
``` | ||
|
||
You can find more information about how to set up your configuration files in the [Config section](https://make.wordpress.org/cli/handbook/config/#config-files). | ||
|
||
## Running custom commands remotely | ||
|
||
If you want to run a custom command on a remote server, that custom command needs to be installed on the remote server, but it does not have to be installed on the local machine you're launching `wp` from. | ||
|
||
You can use the WP-CLI package manager remotely to install custom commands to remote machines. | ||
|
||
Example: | ||
|
||
``` | ||
# The command is not installed on either local or remote machine | ||
$ wp db ack | ||
Error: 'ack' is not a registered subcommand of 'db'. See 'wp help db'. | ||
$ wp @dev db ack | ||
Error: 'ack' is not a registered subcommand of 'db'. See 'wp help db'. | ||
|
||
# To make the command work on the remote machine, we can install it remotely | ||
# through the WP-CLI package manager | ||
$ wp @dev package install runcommand/db-ack | ||
Installing package runcommand/db-ack (dev-master) | ||
Updating /home/vagrant/.wp-cli/packages/composer.json to require the package... | ||
Using Composer to install the package... | ||
--- | ||
Loading composer repositories with package information | ||
Updating dependencies | ||
Resolving dependencies through SAT | ||
Dependency resolution completed in 0.311 seconds | ||
Analyzed 4726 packages to resolve dependencies | ||
Analyzed 162199 rules to resolve dependencies | ||
Package operations: 1 install, 0 updates, 0 removals | ||
Installs: runcommand/db-ack:dev-master aff8ccc | ||
- Installing runcommand/db-ack (dev-master aff8ccc) | ||
Writing lock file | ||
Generating autoload files | ||
--- | ||
Success: Package installed. | ||
|
||
# Now we can run the command remotely, even though it is not installed locally | ||
$ wp @dev db ack [email protected] | ||
wp_users:user_email | ||
9:[email protected] | ||
``` | ||
|
||
## Making WP-CLI accessible on a remote server | ||
|
||
Running a command remotely through SSH requires having `wp` accessible on the `$PATH` on the remote server. Because SSH connections don’t load `~/.bashrc` or `~/.zshrc`, you may need to specify a custom `$PATH` when using `wp --ssh=<host>`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mention There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added a mention and a link to relevant docs. |
||
|
||
You can do so by hooking into the `before_ssh` hook, and defining an environment variable with the command you’d like to run: | ||
|
||
```php | ||
WP_CLI::add_hook( 'before_ssh', function() { | ||
|
||
$host = WP_CLI\Utils\parse_ssh_url( | ||
WP_CLI::get_runner()->config['ssh'], | ||
PHP_URL_HOST | ||
); | ||
|
||
switch( $host ) { | ||
case 'runcommand.io': | ||
putenv( 'WP_CLI_SSH_PRE_CMD=export PATH=$HOME/bin:$PATH' ); | ||
break; | ||
} | ||
} ); | ||
``` | ||
|
||
If you put the code above in a `pre-ssh.php` file, you can load it for your entire environment by requiring it from your `~/.wp-cli/config.yml` file: | ||
|
||
```yaml | ||
require: | ||
- pre-ssh.php | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Giving the format spec
[<user>@]<host>[:<port>][<path>]
would be good. Mentioning it's similar toscp
andgit
. Mentioningport
in list. And "to the WP-CLI installed" perhaps.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added these changes. I rewrote the examples, as the "list all combinations" did not make any sense with the added port.