WordPress Deployment Part 2: Deploying WordPress using the Command Line

#

In my last article, we looked briefly at why automated deployments are important and how to prepare a WordPress site for automated deployments.

Now that our WordPress site is ready to be deployed, in this article we’re going to look at how we can use command line (CLI) tools to deploy WordPress from our local computer to a remote server. While we’re not quite at the stage of being ready to set up automated deployments just yet, understanding how we can use CLI tools to deploy WordPress will serve as a good foundation for all of the automated deployment methods we will look at later in this series.

Before we get going, it’s worth reiterating here that you should have a strategy in place for deploying the database and media library. This series is primarily going to focus on how to deploy the code files for a WordPress site. I may touch on the database and media library at points when we should be considering them in our approach, but I’m not going to cover them in depth as they are whole other topics in themselves.

Deployment Destination & SSH Access

Before we look at any deployment CLI tools, we need to have a destination server where we will deploy our WordPress site. This could be a staging site or production site hosted on a DigitalOcean droplet, for example. If you’re looking for some inspiration on how to host WordPress yourself, check out Ash’s Hosting WordPress Yourself series (psst: we’re actually working on a product to make hosting WordPress yourself super simple. Sign up to be notified).

If you don’t have a destination site ready to use, I’d recommend at least setting up a DigitalOcean droplet (or other VPS) so you can run through the examples in this article. You must have SSH access to your destination server and WordPress site for these examples to work (i.e. using shared hosting instances probably won’t give you SSH access).

Deploying using SCP

If you’re using macOS/Linux the easiest way to get started is to simply use the scp command. It stands for “secure copy” and uses SSH to copy files to a remote server (or vice versa). If you’re on Windows you can download and run an equivalent program called pscp which is part of Putty.

The scp command has an array of options, but to get started deploying our WordPress site we can simply copy the entire WordPress directory to our remote server by running the following command:

scp -r /path/to/local/site/* user@123.456.78.90:/sites/example.com

In this command:

  • The -r flag means recursively copy entire directories.
  • The first argument is the path to your local WordPress site followed by /*.
  • The second argument contains the SSH connection information (user@123.456.78.90) followed by a colon : and the remote path to the destination of your site (/sites/example.com).

For more info on the available scp options see the scp man page.

If you run the command correctly you should see a list of every file being uploaded to the remote server including its progress, size and elapsed time. Note that scp will copy all files and overwrite any destination files. Which leads me nicely on to our next tool: rsync.

Deploying using Rsync

Next up we’re going to look at a CLI tool called rsync. Rsync is also available on macOS/Linux by default. Sadly, there is no direct alternative for Windows, although DeltaCopy might be worth checking out.

While rsync might look very similar to scp it has one important difference. The rsync command employs what’s known as the “delta transfer algorithm”, which basically means it will compare the local and remote files and only transfer the files, or parts of the file (i.e. the delta), that have been modified. This makes transfers much quicker and saves on bandwidth, especially if you’re going to be deploying the same WordPress site multiple times to the same location.

Again, rsync has an array of options, but to get started deploying our WordPress site we need to run the following command:

rsync -av /path/to/local/site/ user@123.456.78.90:/sites/example.com

In this command:

  • The -a flag means use “archive mode” (recursively copy entire directories with permissions etc).
  • We use the -v flag to increase verbosity to see what the progress is.
  • The first argument is the path to your local WordPress site.
  • The second argument contains the SSH connection information (user@123.456.78.90) followed by a colon : and the remote path to the destination of your site (/sites/example.com).

For more info on the available rsync options see the rsync man page.

Deploying using Deployer

If built-in CLI tools aren’t your thing then there are a handful of third-party alternatives that offer a range of enhancements and features over the low-level commands like scp and rsync. Once such tool is Deployer. It boasts features such as:

  • Modular, script based “recipes”
  • Rollback support
  • Atomic deployments
  • Running tasks in parallel

To get started with Deployer lets use the installation instructions from their docs:

curl -LO https://deployer.org/deployer.phar
mv deployer.phar /usr/local/bin/dep
chmod +x /usr/local/bin/dep

Now we should be able to run the dep command in our WordPress site directory:

dep init

Running the dep init command will take you through a multiple choice config process and create a deploy.php file in the directory that contains a “recipe” for deploying our site (based on the choices we made). This file will probably need a bit of tweaking depending on your site setup and server config, but mine ended up looking like this:

<?php
namespace Deployer;

require 'recipe/common.php';

// Project name
set('application', 'wordpress');

// Shared files/dirs between deploys
set('shared_files', [
    'wp-config.php'
]);
set('shared_dirs', [
    'wp-content/uploads'
]);

// Writable dirs by web server
set('writable_dirs', [
    'wp-content/uploads'
]);

set('allow_anonymous_stats', false);

// Hosts
host('123.456.78.90')
    ->set('deploy_path', '/sites/example.com');

// Override `deploy:update_code` command to upload our files instead of `git pull`
task('deploy:update_code', function () {
    upload('.', '{{release_path}}');
});

// Tasks
desc('Deploy your project');
task('deploy', [
    'deploy:info',
    'deploy:prepare',
    'deploy:lock',
    'deploy:release',
    'deploy:update_code',
    'deploy:shared',
    'deploy:writable',
    //'deploy:vendors', - Don't need this as we don't use composer
    'deploy:clear_paths',
    'deploy:symlink',
    'deploy:unlock',
    'cleanup',
    'success'
]);

// [Optional] If deploy fails automatically unlock.
after('deploy:failed', 'deploy:unlock');

Now that our deploy.php file is set up we simply need to run dep deploy and watch Deployer deploy our site. For more info on setting up a deployer recipe check out the Deployer docs.

Deploying using WP-CLI

Sadly WP-CLI isn’t designed to move files from a local computer to a remote server, and as such doesn’t support migrating WordPress files. It is worth noting, however, that WP -CLI does support running commands remotely using SSH. For example, you can create a wp-cli.yml file and add the SSH connection information like this:

@prod:
  ssh: user@123.456.78.90:/sites/example.com

You can then manage your remote WordPress site using the @prod WP-CLI alias:

wp @prod core check-update

This can be an incredibly powerful way of managing a WordPress site remotely using the CLI. In reality, a good deployment workflow might be to use rsync to actually deploy your WordPress files and use WP-CLI remote commands to manage your site.

It’s also worth noting at this point that WP Migrate DB Pro comes with a migratedb command for WP-CLI and also has a CLI addon which enables you to script your database migrations as well as deploying your files.

Moving On Up

By now you should have a decent understanding of what is involved in deploying your WordPress site from your local computer to a remote server using CLI tools. There are many other CLI tools and techniques not mentioned in this article so these examples are by no means the only way to do WordPress deployments. If you know of any others please let me know about them in the comments.

In my next article, we’re going to look at how we can use Git to deploy our WordPress sites and see what different deployment strategies we can use with Git.

Have you used CLI tools to deploy your WordPress site? Got any tips or suggestions for using CLI tools to deploy WordPress? Let us know in the comments.

About the Author

Gilbert Pellegrom

Gilbert loves to build software. From jQuery scripts to WordPress plugins to full blown SaaS apps, Gilbert has been creating elegant software his whole career. Probably most famous for creating the Nivo Slider.