⚙️ Using WP-CLI to maintain multiple sites over SSH

Not all hosts provide a simple way to update WordPress Core, Plugins, or Themes within their control panel so that leaves you logging in to each WordPress install one by one to push updates (Of course you can enable auto-updates in WordPress these days). There is another way of going about this if you like to have a little more control, and that is by using WP-CLI. WP-CLI allows you to create a config.yaml file in the /User/home/username/.wp-cli/ directory on your computer that has WP-CLI installed.

The process of setting this up is pretty straightforward, first, you want to make sure for ease of use you push out your SSH key to each site/server that you will be connecting to else you are stuck typing in the password for each site which kind of defeats the purpose of this setup. I won’t explain this process as there are many walkthroughs out there that can help you get this going if you are unfamiliar, I even wrote a bash script to help with this process.

The next step is to create the config.yaml file like the one below within your credentials and site names.

# Personal Sites

@site1:
	ssh: [email protected]
	path: /home/site1/public_html/

@site2:
	ssh: [email protected]
	path: /home/site2/public_html/

@site3:
	ssh: [email protected]
	path: /home/site3/public_html/

@personal:
  - @site1
  - @site2
  - @site

After the file has been saved you can now execute the following command which will systematically go through all of the @personal sites and update the WordPress core all by running the following command.

wp @personal update core

Please see the video below demonstrating how it looks when running this command.

You can take this even further if you manage different groups of sites, perhaps you have some development sites, client sites, as well as personal sites you can then change up the config.yaml like the following to group these various sites.

# Personal Sites

@site1:
	ssh: [email protected]
	path: /home/site1/public_html/

@site2:
	ssh: [email protected]
	path: /home/site2/public_html/

@site3:
	ssh: [email protected]
	path: /home/site3/public_html/

# Client Sites

@site1:
	ssh: [email protected]
	path: /home/client1/public_html/

@site2:
	ssh: [email protected]
	path: /home/client2/public_html/

@site3:
	ssh: [email protected]
	path: /home/client3/public_html/

# Development Sites

@site1:
	ssh: [email protected]
	path: /home/devsite1/public_html/

@site2:
	ssh: [email protected]
	path: /home/devsite2/public_html/

@site3:
	ssh: [email protected]
	path: /home/devsite3/public_html/

@personal:
  - @site1
  - @site2
  - @site

@clients:
  - @client1
  - @client2
  - @client3
  
@development:
  - @devsite1
  - @devsite2
  - @devsite3

Now when you want to update just the client sites you can use the command below

wp @clients update core

While I have used the updating core as an example you can push any of the WP-CLI commands through to the groups of sites such as plugins, themes, etc. As you can see this really can help when it comes to managing a few sites, a couple of dozens or even hundreds!