Advanced WordPress Management Using WP-CLI on Alibaba Cloud ECS – Part 3

简介: This three-part tutorial walks you through setting up WordPress advanced management with WP-CLI. Part 3 focuses on managing multiple WordPress sites with WP-CLI on Alibaba Cloud ECS.

By Jeff Cleverley, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

This is the final part in our 3-part deep dive into the advanced WordPress management abilities provided by WP-CLI.

In the previous tutorials we installed WP-CLI on our instances, prepared the correct permissions and user roles for it to work properly, and used it to manage your WordPress content. Then we dove deeper into more advanced functionality to managing our installations, plugins, themes, users and performed database operations. We also configured default parameters to allow us to manage many sites on a server with each command.

In the final tutorial, I am going to demonstrate how to further leverage WP-CLI to manage hundreds of sites across many different server instances, all from your local machine. We will explore commands that can be used to speed up development of feature plugins and themes, before finally extending WP-CLI to provide custom functionality via custom commands.

As ever, this tutorial assumes you already have an Alibaba Instance provisioned and with one (or several) WordPress sites running on it.

Throughout the series, I will be using my superuser 'new_user' and will be issuing 'root' commands using the sudo command. When you follow the commands please remember to replace my user with your own.

I will also be using my test domains 'an-example-domain.com' and 'another-example-domain.com' in the code examples, remember to replace my site domains with your own when you issue the commands.

1. Managing Multiple WordPress sites using WP-CLI

It's now obvious that using a CLI is much more efficient and productive than the browser to manage your WordPress sites. Things work much more quickly, and the commands are really quite easy once you get used to them.

We are getting an impression of the power that may be available if used properly. Still, it might not seem that much more efficient than doing it from the dashboard, especially when there are many services available that let you manage many WordPress installations at the same time.

So how can we do that? Well as is often the way, there are multiple methods. Today we will look at two of those.

Alias Groups and BASH scripts.

1a.Use Alias Groups to manage multiple sites

In the previous example I used Aliases to check the plugins on two sites with the following commands:

$ cd ~
$ wp @example plugin list
$ wp @another plugin list

But WP-CLI provides us with a solution to this inefficiency too. We can combine Aliases into Alias groups to perform actions on multiple sites with once command.

Let's create an Alias Group. Open your configuration file again for editing:

$ nano ~/.wp-cli/config.yml

And this time add another line below your previous aliases, like so:

# Alias Groups
@all-examples:
 - @example
 - @another

Your full configuration file should now look like this:

1
<Add an Alias Group to your configuration>

Now we can issue a single command. Let's list all the plugins on both our sites for example:

$ wp @all-examples plugin list

2
<Use an Alias Group to manage many sites>

In actual fact, this is redundant as WP-CLI also has a built in @all alias that will run any commands against all defined Aliases:

$ wp @all plugin list

But it serves for the demonstrative purposes required by this series of tutorials.

1b.Use BASH scripts with WP-CLI to manage multiple sites

We can achieve a similar result to the above by adding WP-CLI commands to BASH Scripts in the following way.

Create the a script file in your PATH:

$ sudo touch /usr/local/bin/wp-plugin-list.sh
$ sudo nano /usr/local/bin/wp-plugin-list.sh

Then add this code:

#!/bin/bash 
declare -a sites=(
    '/var/www/an-example-domain.com/htdocs' 
    '/var/www/another-example-domain.com/htdocs'
)
for site in "${sites[@]}"; 
do 
    echo “List plugins for:  
    echo $site
    wp --path=$site plugin list
    echo “ “ 
done

Save, exit, and make the script executable:

$ sudo chmod +x /usr/local/bin/wp-plugin-list.sh

Now we are ready to run our scripts. First run the script to check the plugins:

$ wp-plugin-list.sh

3
<List all your plugins using WP-CLI commands and a script>

In general using Aliases can be considered more optimal than creating BASH scripts, but there may be times a script may prove better. With a script you can combine WP-CLI commands with other non WP-CLI commands in different combinations that may prove more effective for managing many sites. We could for example backup our databases and then copy them to a different directory on our server or a third party storage solution.

The process of logging into each of your sites via the WP Admin, and checking for plugin updates would have taken much longer than either of these methods.

Note

From this point forwards, any commands demonstrated in this series of tutorials may be issued using an Alias if it is more appropriate. Please use your own aliases or replace any Alias with the full path to any WordPress directory you are acting upon.

2. Using SSH with WP-CLI for remote management

So far so good. We are now using the power of WP-CLI to manage multiple WordPress sites on the same server. If we have dozens or hundreds of sites this will save an incredible amount of valuable time.

But we can still improve our workflow.

At the moment we:

1.SSH into the remote server from our local server.

2.Either run WP-CLI commands in WordPress site directories or use scripts from within the server to run commands across many WordPress site directories on that server.

3.Repeat the process for any other servers.

What if we could combine these, and perhaps even run WP-CLI management commands across not just one Alibaba instance, but many instances, all from our Local machine?

That is not only possible, but often preferable.

To do this, you will need to have WP-CLI running on both your local machine and on all your Alibaba instances.

Instructions to install WP-CLI on your Windows PC.

Instructions to install WP-CLI on your Mac.

You will need to complete installation before you can complete this section.

From my local machine I can issue WP-CLI commands and use the '--ssh=' path command to have those WP-CLI command run on my remote server.

List my remote plugins with:

$ wp --ssh=new_user@an-example-domain.com/var/www/an-example-domain.com/htdocs/ plugin list

Or list my remote themes with:

$ wp --ssh=new_user@an-example-domain.com/var/www/an-example-domain.com/htdocs/ theme list

As you can see, this works perfectly:

4
<Use WP-CLI locally to manage WP sites hosted remotely>

Using Aliases and SSH

Having to type of the SSH command appended with the full path will become quite tiring and is inefficient. In a previous section we used Aliases to allow us to issue WP-CLI commands from anywhere within our Instance without requiring the full path, now we can use them again to do the same for SSH logins and paths.

Using Aliases Globally

We might not have a .wp-cli directory or Global configuration file yet created on our Local machine. If not first make them, then open the configuration file for editing:

(If one already exists, just open it for editing)

$ sudo mkdir ~/.wp-cli && sudo touch ~/.wp-cli/config.yml
$ sudo nano ~/.wp-cli/config.yml

We will add our Aliases in a very similar manner to how we added them to the Global configuration file on our instance. The only difference is that instead of using 'path' we will set them with the 'ssh' parameter. You should probably set your 'disabled_commands' at the same time:

#Global Parameter Defaults
disabled_commands:
 - db drop

# Aliases
@example:
    ssh: new_user@an-example-domain.com/var/www/an-example-domain.com/htdocs/
@another:
    ssh: new_user@another-example-domain.com/var/www/another-example-domain.com/htdocs/

Our Global WP-CLI configuration file on our local machine should now look like this:

5
<Add remote Aliases to your local Global configuration file>

Now we can use WP-CLI locally and just add the Alias '@name' between the 'wp' command and the subcommands to easily run WP-CLI management tasks on our remote Alibaba instances.

From our Local machine, issue commands to our remote instances like so:

$ wp @example plugin list

or

$ wp @another core check-update

Using Alias Groups to manage multiple sites by SSH

Just like on our server, we can use Alias Groups locally to manage many multiple sites remotely.

The difference is, that now we can create Alias Groups that contain Aliases to WordPress sites on different serves. That means we can now use Alias Groups to issue commands from our Local machine that will manage many different WordPress sites on many different Alibaba instances.

Reopen your local Global configuration file:

$ nano ~/.wp-cli/config.yml

Add the following lines underneath the existing Aliases:

@all-examples:
 - @example
 - @another

Your configuration file should now look like this:

6
<Add Alias Group to your local Global configuration file>

Using Aliases within a Project

Sometimes we are using WP-CLI as part of a modern WordPress development and Deployment workflow. In other tutorials we have set up Staging, Local and Production environments and implemented a Git version controlled workflow.

WP-CLI works very well in this situation, and allows you to set specific Aliases per project within the project's local WP-CLI configuration file:

If you have a local development version of your production site, then we can add a 'wp-cli.yml' file to configure global parameters for this workflow.

Change directories to the root of one of your local development sites and create a 'wp-cli.yml' file in it:

$ cd /local/sites/anotherexampledomain/app/public
$ sudo nano wp-cli.yml

Within this file add different aliases for each of your remote sites:

@test:
    ssh: new_user@another-example-domain.com/var/www/test.another-example-domain.com/htdocs
@staging: 
    ssh: new_user@another-domain.com/var/www/staging.another-domain.com/htdocs/
@production:
    ssh: new_user@another-domain.com/var/www/another-domain.com/htdocs/

Your project 'wp-cli.yml' configuration file should now look like this:

7
<Add the SSH path to your staging and production sites>

Now when we are working locally, we can issue WP-CLI commands to our remote servers using these aliases, like so:

$ wp @production plugin list

And just like with Global Aliases we can create Alias Groups:

@test:
    ssh: new_user@another-example-domain.com/var/www/test.another-example-domain.com/htdocs
@staging: 
    ssh: new_user@another-domain.com/var/www/staging.another-domain.com/htdocs/
@production:
    ssh: new_user@another-domain.com/var/www/another-domain.com/htdocs/
@prerelease:
 - @test
 - @staging

We could now issue commands to a Project Alias Group like so:

$ wp @prerelease plugin list

3. Using WP-CLI Scaffold to speed up development

WP-CLI has a command called 'wp scaffold' that is used to generate the boilerplate code necessary for many of the standard WordPress development tasks.

These generating the basic code needed for:

  • Child Themes
  • Feature Plugins
  • Custom Post Types
  • Custom Taxonomies
  • PHPunit testing of plugins
  • PHPunit testing of themes

Demonstrating the PHPunit testing code generation is beyond the scope of this tutorial as it requires a series in itself. But let's quickly run through some of the other cool things 'wp scaffold' can do:

If we want to create our basic directory structure and minimum necessary files for a theme to be activated, we can do that with the following command:

 wp @example scaffold child-theme twentyseventeen-child --parent_theme=twentyseventeen --theme_name="Twenty Seventeen Child" --author="New User" --author_uri="an-example-domain.com" --theme_uri="an-example-theme.com" —activate

Notice how I am creating a Child theme on our Alibaba instance from my local machine, using an Alias? Incredibly efficient.

We can see in the terminal the theme has been created:

8
<Create a Child theme with WP-CLI scaffold>

And it is activated on the site:

9

10
<Our new Child theme in the WP Admin>

What about a plugin, I hear you asking? Well that is just as simple:

wp @example scaffold plugin custom-plugin --plugin_name="Custom Plugin" --plugin_description="A custom plugin for custom functionality" --plugin_author="New User" --plugin_author_uri=https://an-example-domain.com --plugin_uri=https://custom-plugin.com —activate

And now we have a plugin installed and activated:

11
<Our new Plugin in the WP Admin>

One of the most common developer tasks with WordPress is building custom post types and taxonomies for true Custom WordPress development.

We can create the Custom Post type boilerplate code, and add it to either a plugin or theme, with the necessary directory structure using:

wp @example scaffold post-type tutorials --label="Tutorials" --plugin=custom-plugin --dashicon=welcome-learn-more

Likewise, with a Custom taxonomy:

wp @example scaffold taxonomy wpcli --post_types=posts --label="wpcli" --plugin=custom-plugin

You can inspect the code and directory structure generated in the WP Admin:

12
<Custom Post Type Boilerplate code and files generated in a plugin>

13
<Custom Taxonomy Boilerplate code and files generated in a plugin>

4 Extending WP-CLI

If you have followed through these tutorials and gotten to this point, it is my hope that you now consider WP-CLI an indispensable tool in your WordPress toolset.

Even if we ended here, then this is great!

Our journey doesn't end here though, WP-CLI is the gift that keeps on giving, as WP-CLI is extensible!

This can be achieved in two ways, you can either load PHP in from a file and execute it in WP-CLI, or you can build a plugin that creates a custom command.

To demonstrate both methods, I will use a similar example output.

Execute a PHP file

If you have a PHP snippet you want to test to see if it works, you don't need to go to the lengths of including it in a WordPress plugin, or your functions.php theme file to test it.

You can quickly test it using the 'wp eval-file' command provided by WP-CLI. This will execute your PHP file after having loaded WordPress.

Create a 'random-post.php' file in your home directory, and open it for editing:

$ nano random-post.php

Inside the file add the following code:

<?php
global $wpdb;
$random_example_post = $wpdb->get_var(
    "SELECT post_title
    FROM $wpdb->posts
    WHERE post_type = 'post'
        AND post_status = 'publish'
    ORDER BY rand()
    LIMIT 1"
);
echo "A random example post: $random_example_post \n";

In your editor your PHP script:

14
<Your PHP script file to run>

This is just a basic function that queries the database for a random post and displays it out.

Now run the following command, either using an Alias or from within a WordPress directory:

$ wp @example eval-file random-post.php

Your terminal will now display a random post title:

15
<Execute a PHP file in WordPress with WP-CLI eval-file>

Create Custom Commands in a Plugin

We can also extend WP-CLI's inbuilt functionality by creating our own WordPress plugins. In this manner we can extend WP-CLI by adding our own custom functionality accessed through WP-CLI commands we define.

First, we shall put the 'wp scaffold' function to good use to create our plugin boilerplate:

wp @example scaffold plugin wp-cli-custom-commands —plugin_name=“WP-CLI Custom Commands" --plugin_description="A custom plugin for creating a WP-CLI custom command“ --plugin_author="New User" --activate

Now change directory to the plugin root directory and open the plugin file:

$ sudo nano wp-cli-custom-commands.php

$ cd /var/www/an-example-domain.com/htdocs/wp-content/plugins/wp-cli-custom-commands

The Plugin file requires a class for each command, and public methods for each subcommand.

To demonstrate this, we recreate the same functionality as contained in the previous script and have it execute on a the 'random-post' command.

We will create a class, called 'Custom_Commands', that contains a single public method, 'random_post' for our subcommand - this executes the same simple database query to return a random post as in the above script.

We will then register our class as a WP-CLI command using an 'add-command' call. This function takes the name of the command and the name of the class as it's arguments.

in your 'wp-cli-custom-commands.php' add the following code:

if( defined( 'WP_CLI' ) && WP_CLI ) {

        class Custom_Commands {

                function random_post ( $args, $assoc_args ) {

                        global $wpdb;

                        $random_post = $wpdb->get_var(
                        "SELECT post_title FROM $wpdb->posts 
                        WHERE post_type = 'post' 
                        AND post_status = 'publish' 
                        ORDER BY rand() 
                        LIMIT 1");

                        WP_CLI::success( "Random post: $random_post" );
                }
        }

        WP_CLI::add_command( 'custom_commands', 'Custom_Commands' );
}

Our plugin's main file should look like this in the editor:

16
<Your custom command plugin file>

The Plugin URI and Author URI are missing as I didn't define them in the 'wp scaffold' command, you could include them in yours.

Now we can confirm that the command has been registered by executing:

$ wp @example custom_commands

This will list the available subcommands; we only have one 'random_post'. We can execute that command now:

$ wp @example custom_commands random_post

In your terminal WP-CLI should return a 'Success' message followed by a randomly selected Post title from your WordPress site:

17
<Congratulations - You have successfully extended WP-CLI>

Well done, you have now created and executed your very first WP-CLI custom command!

You should now be able to see how you can use Plugins to create Commands that will perform any number of functions on your WordPress site and create a simple way to perform any administrative WordPress task you can think of.

We have reached the end of this series of tutorials. It has been quite a long in depth dive into the possibilities of WP-CLI, but believe me when I tell you… this is just scratching the surface of the possibilities.

If you want to find out more, make sure you check the official resources:

相关实践学习
2分钟自动化部署人生模拟器
本场景将带你借助云效流水线Flow实现人生模拟器小游戏的自动化部署
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情:&nbsp;https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
域名解析 网络协议 数据安全/隐私保护
阿里云轻量应用服务器搭建WordPress个人博客教程
阿里云轻量应用服务器搭建WordPress个人博客教程
|
2月前
|
域名解析 弹性计算 程序员
想要轻松地搭建一个即开即用的WordPress博客吗?借助宝塔面板镜像+阿里云ECS,迅速拥有自己的个人博客
拥有个人博客是每位程序员的梦想,但对服务器不熟悉的初学者而言,搭建博客颇具挑战。本文介绍利用阿里云市场的宝塔面板镜像与ECS云服务器,轻松搭建WordPress博客的方法,让您快速拥有专属博客空间。通过简单的操作步骤,即使是新手也能轻松上手,实现从零到有的博客搭建过程。
230 3
|
2月前
|
人工智能 运维 Kubernetes
87cloud案例分析:阿里云国际服务器如何支持在线教育
87cloud案例分析:阿里云国际服务器如何支持在线教育
|
2月前
|
域名解析 安全 网络安全
阿里云服务器WordPress环境上安装SSL证书
阿里云服务器WordPress环境上安装SSL证书
|
2月前
|
弹性计算 JSON 关系型数据库
使用ROS模板基于ECS和RDS创建WordPress环境
使用ROS模板基于ECS和RDS创建WordPress环境
|
2月前
|
弹性计算 关系型数据库 MySQL
CentOS 7.x操作系统的ECS云服务器上搭建WordPress网站
CentOS 7.x操作系统的ECS云服务器上搭建WordPress网站
|
2天前
|
人工智能 JSON Linux
利用阿里云GPU加速服务器实现pdf转换为markdown格式
随着AI模型的发展,GPU需求日益增长,尤其是个人学习和研究。直接购置硬件成本高且更新快,建议选择阿里云等提供的GPU加速型服务器。
利用阿里云GPU加速服务器实现pdf转换为markdown格式
|
1天前
|
开发框架 缓存 .NET
阿里云轻量应用服务器、经济型e、通用算力型u1实例怎么选?区别及选择参考
在阿里云目前的活动中,价格比较优惠的云服务器有轻量应用服务器2核2G3M带宽68元1年,经济型e实例2核2G3M带宽99元1年,通用算力型u1实例2核4G5M带宽199元1年,这几个云服务器是用户关注度最高的。有的新手用户由于是初次使用阿里云服务器,对于轻量应用服务器、经济型e、通用算力型u1实例的相关性能并不是很清楚,本文为大家做个简单的介绍和对比,以供参考。
|
9天前
|
弹性计算 运维 安全
阿里云轻量应用服务器与ECS的区别及选择指南
轻量应用服务器和云服务器ECS(Elastic Compute Service)是两款颇受欢迎的产品。本文将对这两者进行详细的对比,帮助用户更好地理解它们之间的区别,并根据自身需求做出明智的选择。
|
10天前
|
SQL 弹性计算 安全
阿里云上云优选与飞天加速计划活动区别及购买云服务器后续必做功课参考
对于很多用户来说,购买云服务器通常都是通过阿里云当下的各种活动来购买,这就有必要了解这些活动的区别,同时由于活动内的云服务器购买之后还需要单独购买并挂载数据盘,还需要设置远程密码以及安全组等操作之后才能正常使用云服务器。本文就为大家介绍一下目前比较热门的上云优选与飞天加速计划两个活动的区别,以及通过活动来购买云服务器之后的一些必做功课,确保云服务器可以正常使用,以供参考。

热门文章

最新文章