Using the Command Line for Automation – Part I Remote Control WordPress with WP-CLI Aliases

WP-CLI is a tool that allows commands to be run in WordPress from the command line or terminal. One of the reasons why developers love WP-CLI is that it provides a way to automate WordPress and common operations that otherwise require navigating through the WordPress Dashboard in order to execute. There can be many clicks to get through operations like updating all plugins on a WordPress Installation. WP-CLI makes it possible to automate repeatable commands with scripting in order to create a list of executable tasks. Instead of running many commands, they can be saved together in a script and executed at once. The benefit of scripting is repeatability and this comes in handy when there is a need to manage tasks on many WordPress websites with consistency.

Working locally with WP-CLI on VVV

Varying Vagrant Vagrants (VVV) is an all-in-one Vagrant environment that allows developers to work locally on a fully functional virtual machine that is pre-configured to work with WordPress. VVV is a relatively simple way to set up a local WordPress environment configured for WordPress development. VVV provisions a web server which is optimized for WordPress in a virtual machine on your local computer. It uses Virtual Box and Vagrant and requires a few other dependencies for installation. See the VVV repository for more information about installation and functionality.

Connecting via SSH

SSH is an acronym for Secure Shell and allows a secure connection between two servers using public/private key authentication. For our purposes, SSH can be used to establish a secure connection between your local machine and a remote server. You’ll need to generate an ssh key pair and copy your public key from your local machine to your remote environment. This allows you to connect to the remote server securely without entering a password.

Learn more about SSH Key Management and generating keys.

Setting up SSH

Before we get started with using with WP-CLI in VVV, we need to create a way to connect to our remote WordPress install. Remember that VVV acts like an entirely separate server even though it is running on your local system. Connecting to VVV can be done over SSH with a little bit of configuration.

SSH Configuration File

A typical SSH configuration entry will look like the following and will likely have more options than I’ve listed below.

Basic SSH config file

Host your_site
HostName your_site@your_domain.com
User user_name
IdentityFile path_to_private_key

If you are using VVV or another environment that uses Vagrant, you can generate your SSH configuration by running the command vagrant ssh-config and copying the output to your SSH configuration file which will typically be at ~/.ssh/config on a Mac or Linux system. You will use the value of Host when you run SSH and the command will look like this: ssh your_site. You will need to have a SSH host entry for each server that you want to connect to and run WP-CLI commands on.

*vagrant ssh-config command output*

Host default
HostName 127.0.0.1
User vagrant
Port 2200
UserKnownHostsFile /dev/null
StrictHostKeyChecking no
PasswordAuthentication no
IdentityFile /path_to_private_key
IdentitiesOnly yes
LogLevel FATAL
ForwardAgent yes

Note: You probably want to change this from your_site or default to something that makes a little more sense like your actual website’s name.

Pro Tip: the Port value may change if you restart your vagrant instance. If your ssh host will not connect you may need to change the port number in your ssh config. This can be generated by running vagrant ssh-config again, noting the port number and updating the entry in your SSH configuration file.

WP-CLI Aliases

WP-CLI has a feature called aliases, which allow you to specify a remote WordPress site to run commands on by adding a prefix. For example, you can export a database from your production site, download it to a local machine and then import it to a development site all from the command line without touching a mouse. If this sounds interesting keep an eye out for Part 2 of this series where I’ll dive in to creating a shell function to sync databases from production to a local dev server.

*A typical WP-CLI info command*

wp cli info

The above command will produce general information about the current system’s WP-CLI installation. Using an Alias, it is possible to run the same command on a different server.

*A WP-CLI info command with an alias @dev*

wp @dev cli info

In this case, the same command cli info is executed on a remote server called @dev which I have set to my VVV development site.

*List plugins on development server @dev*

wp @dev plugin list

This command will show me all of the plugins that I have installed on my local development site. If add another alias for my production site called @prod, I could run commands on my production server.

*List plugins on production server @prod*

wp @prod plugin list

This command will show me a list of plugins that are on my production machine. The commands in this post are just a tiny sampling of what can be done with WP-CLI. For more WP-CLI commands check out the [WP-CLI command list](https://developer.wordpress.org/cli/commands/).

Configuring WP-CLI Aliases in VVV

A file called /wp-cli.yml is included with VVV, a popular Vagrant environment for building WordPress sites locally. After VVV is installed and configured it is possible to add an alias in order to connect via SSH into the Vagrant virtual machine and execute commands. The benefit of using WP-CLI aliases is that the SSH connection happens automatically when aliases are used. You do not have to SSH into the server to run commands, as they can be run from your local machine by adding an alias name to the command you would like to run. This is a time saver and can simplify your workflow when using WP-CLI across multiple servers or environments.

Setup looks like this:

*wp-cli.yml file*

@dev:
ssh: edmund.test
path: /srv/www/edmund

@prod:
ssh: edmund

In the configuration above, @dev is the name of the alias that you’ll add to your SSH command. The ssh: value is the host that is used to remote into your server. The path: value is the location where WordPress is installed on your server. I’ve included a second alias in the example above that I use to connect to a production server. The @prod server is set up to connect to my production site on WP Engine via the recently released [SSH Gateway](https://wpengine.co.uk/support/getting-started-ssh-gateway/).

Note: there is no path needed for the @prod alias and setup is done in your local ~/.ssh/config file as mentioned in the section above.

Conclusion

WP-CLI is a great tool that allows you to manage and configure WordPress without having to use the WordPress Dashboard. WP-CLI can be used from the command line, which makes it convenient for developers. If you followed along with the examples above you should understand how to execute WP-CLI commands on VVV and remote servers. WP-CLI Aliases are an effective way to optimize your workflow and allow you to work with WP-CLI on remote servers without the need to SSH into a server before running commands. VVV provides support for running WordPress locally on a vagrant virtual machine and has WP-CLI Alias support built in.

That’s all for now, but keep an eye out for the second part of this series where I will dive into automating a WordPress database sync between 2 servers using WP-CLI and Aliases.

Edmund Turbin

Edmund Turbin is Senior Sales Engineer at WP Engine, London. Over the past ten years he has built websites with content management systems and has held technical roles in media, publishing and ad tech in New York City. Edmund is passionate about front end coding, development workflow and architecting scalable WordPress solutions for enterprise. Edmund has recently is a certified AWS Solutions Architect – Associate. In his free time, Edmund enjoys electronic music production, snowboarding, cycling and discovering new adventures across the globe with his family & two kids. Follow him on Twitter @spicecadet.

The post Using the Command Line for Automation – Part I Remote Control WordPress with WP-CLI Aliases appeared first on Torque.

Sharing is Awesome, Thank You! :)

Share this Blue 37 post with your friends
close-link