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

简介: This three-part tutorial walks you through setting up WordPress advanced management with WP-CLI. Part 1 focuses on setting up 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.

WordPress is the most popular content management system used to develop websites with dynamic content on the web. Much of WordPress's popularity has been gained due to the ease of use that it provides by it's Graphical User Interface (GUI). WordPress has always provided a full feature set in a package that is easy for beginners to get started.

Since its early days as a blogging platform it has evolved into a much more widely used resource, often being used for large e-commerce sites, enterprise sites, social networks, and more. Part of its progress into a platform used by more serious developers, was the introduction of WP-CLI - the official WordPress Command Line Interface.

The Command Line Interface (CLI) has long been popular with developers due to the speed and efficiency with which you can perform tasks on your machine. Using a wide variety of tools, developers have boosted their productivity and sped up their development process. The WP-CLI affords WordPress developers and site maintainers the same benefits and efficiencies.

WP-CLI is now an officially supported project of the WordPress foundation and has it's own site and developers handbook, you can find a full list of all it's commands and tools here.

This is the first in a 3-part series that will take a deep dive into the advanced WordPress management abilities provided by WP-CLI. In this tutorial we will look at installing WP-CLI on your instance, and preparing the correct permissions and user roles for it to work properly, after that we will see how it can be used to manage your WordPress content. In the following tutorials, we will dive deeper into more advanced functionality, such as managing your themes and plugins, media, users and database. Before finally showing you how it can be leveraged to manage hundreds of sites across all of your Instances, speed up development of custom plugins and themes, and even extended to provide custom functionality by creating your own commands.

Through effective use of WP-CLI you will see how you can even reduce bloat of your WordPress sites through the removal of the need for several widely used plugins.

This tutorial assumes you already have an Alibaba Cloud ECS 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. Install WP-CLI on your Alibaba Instance

Login to your server by SSH:

# ssh new_user@an-example-domain.com

Download the 'wp-cli.phar' PHP archive file:

$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Now check to ensure the file is working:

$ php wp-cli.phar --info

Your terminal should present you information about your PHP version, configuration file, and some information about the WP-CLI installation.

We want to be able to use WP-CLI by just typing 'wp' instead of 'php wp-cli.phar', to do that we need to make the PHP archive file executable. We also want to have access to the 'wp' command from anywhere so we need to move it into our PATH.

To do both of these, execute the following commands:

$ chmod +x wp-cli.phar
$ sudo mv wp-cli.phar /usr/local/bin/wp

Your terminal should look something like this:

1

<Download the WP-CLI PHP archive file and test it>

Now you can check to make sure that this is working by issuing the same WP-CLI '--info' command but this time just using 'wp':

$ wp --info

If you want to check WP-CLI for updates, you can use:

$ wp cli update

Your terminal should now look similar to:

2

<Make the Archive executable to use the WP command >

All WP-CLI Commands

You can get a full list of all the available WP-CLI commands and Global Parameters, by entering:

$ wp

or

$ wp help

2. Prepare WordPress installation and User permissions

We will be issuing WP-CLI commands as our user, in my case 'new_user'. We might run into problems with command that need to write to our website directories if we don't prepare directory and user groups accordingly.

Change directory into your '/var/www' directory, or whichever directory contains your WordPress installations, and list the directories and their ownerships and permissions:

$ cd /var/www
$ ls -l

You should see that your Website root directory belongs to the 'www-data' user and group. This is the user that the web server runs under.

The directory permissions you may also be 'drwxr-xr-x' or something similar, this is 0755, and only grants the owner (www-data) write permissions.

We want to change this to 'drwxrwxr-x' / 775 so that the members of the 'www-data' group also have write permissions:

Do that and then list out the directories again, with the following commands:

$ sudo chmod -R 775 /var/www/an-example-domain.com
$ ls -l

You should now see that the directory permissions have been changed accordingly:

3

<Change WordPress directory permissions to 775>

Now we just need to make sure that our user is a member of the 'www-data' group so that we can write to these directories using WP-CLI commands:

$ sudo usermod -a -G www-data new_user

You can check which groups your user belongs to with the 'groups' command:

$ groups

Your terminal should look like this:

4

<Make sure your user is in the www-data group>

3. WordPress Core Commands

Installing WordPress Core

WP-CLI offers a set of powerful tools for managing the installation and updating of WordPress core, these are the 'core' commands.

Before doing this, you need to your WordPress site domain configured, alongside virtual host and site root folder, as well as the having created and empty database for WordPress to use.

I go over everything in detail in another tutorial here.

They include the ability to download different versions and localizations of WordPress using the '--version' and '--locale' parameters.

For example:

$ wp core download --version=4.8.4 --local=en_GB

Would download WordPress v.4.8.4 with the correct (British) English localization.

You can also do things like create a 'wp-config.php' file using 'core config':

$ wp core config --dbname=tutorial --dbuser=new_user --dbpass=new_users_password --dbhost=localhost --dbprefix=tut_

And we can install WordPress using the 'core install' command:

$ wp core install --url=an-example-domain.com --title=“WP-CLI Tutorials” --admin_user=new_user --admin_password=new_user_admin_password

Now your WordPress site will be up and running, without the need to go through the usual installation process. As you can see installing with WP-CLI it is far quicker than WordPress' 'famous' (if they do say so themselves) 5-minute installation procedure.

Updating WordPress Core

Keeping your WordPress installations up to date is incredibly important from a security standpoint.

To check your WordPress version, change directory into the directory that contains your WordPress files and directories and issue the following command:

$ wp core version

As you can see, my 'an-example-domain.com' WordPress site is out of date, it is only running WordPress 4.8.4 and not the latest 4.9.1 version, that is poor security practice on my part:

5

<Check WP Core version – 4.8.4 is not the newest version>

6

<WordPress needs updating>

Before you update your WordPress Core, you are always warned to back up your Database.

Many people don't perform this important step because it goes out of their workflow and usually requires logging into some sort of Database management tool, either desktop based, in their hosting control panel, or some sort of web based tool.

With WP-CLI it is incredibly easy and fast, so now there is no excuse:

$ wp db export backup.sql

If you have ever used a third party GUI based tool to export a database you will be surprised by just how quickly this command is executed and completed.

In no time you will see a 'Success' message:

7

<Backing up your Database prior to Core updates has never been easier>

Now we are ready to update core, and check the version again:

$ wp core update
$ wp core version

In your terminal you should see:

8

<Use WPCLI to update WordPress core WordPress Core quickly>

We can also update our Database with ease:

$ wp core update db

9

<Update your WordPress Database>

If you are having problems following a Core update, don't worry…

It's also incredibly easy to walk WordPress back to any of the previous versions. Just add the '--version' and '--force' parameters.

$ wp core update —version=4.9 --force

4. Managing WordPress Content

Listing, Creating, Deleting, and Editing Posts and Pages

You can manage all of your posts and pages, and their content directly from the terminal. Perhaps not the optimal content creation method, but still incredibly useful.

These commands have a parameter '--post_type' this defaults to '--post_type=post' but all commands can be used to manipulate pages with the use of '--post_type=page'. For the sake of brevity I will just be demonstrating 'post' commands as '--post_type=post'.

To list all post types you can use:

$ wp post list

Since I have a new installation, there is only the “Hello World” post with the ID of 1, let's delete that:

$ wp post delete 1

Then when we list out the posts again we can see it has gone:

10

<List the posts then delete the “Hello World!” post>

11

<Tutorial Site with no post>

We can create posts and their content with a command, using parameters for the title, content, and status:

$ wp post create --post_title='Your title' --post_content=“Your most interesting post content” --post_status='publish;

List the posts again:

$ wp post list

Now we can see our new post in the list:

12

<WP-CLI created post listed in the terminal>

Often, when developing WordPress themes or plugins, we often need to create a whole bunch of posts, just to test things out. It is a slow process using the browser and GUI, but WP-CLI has the answer with the 'post generate' command:

$ wp post generate --count=4

And in an instant we have 4 new posts:

13

<Generate dummy posts using WP-CLI - view them in the terminal>

14

<Generate dummy posts using WP-CLI - view them in the browser>

Deleting them is just as easy, we can delete batches just by listing their ID's:

$ wp post delete 8 9 10 11

Easy come, easy go:

15

<Batch Deleting Posts with WP-CLI>

We can also edit posts with WP-CLI using your system editor. Let's edit the post we created earlier and add some new content:

$ wp post edit 7

16

<Edit your posts using WP-CLI and the System Editor>

Your terminal should now look like this:

17

<Post successfully edited>

View your edited post it in your browser:

18

<A WP-CLI created and edited post>

Managing Post Meta

We can also use WP-CLI 'post meta' commands to 'add' meta data including custom fields.

Let's add a new Custom Meta Field called 'post_type' and give it the value of 'tutorial':

$ wp post meta add 7 post_type tutorial

Then we can 'get' the 'post_type' value with:

$ wp post meta get 7 post_type

19

<Setting and Getting Post Meta with WP-CLI>

We can also add, or 'set', terms:

$ wp post term set 7 category wpcli

And 'remove' them:

$ wp post term remove 7 category wpcli

20

<Create and remove Terms with WP-CLI>

Managing Revisions

WordPress stores every revision of a post as a post in the Posts table. This can lead to a huge amount of Database bloat, there are a lot of useful plugins that help you manage these revisions.

But, with WP-CLI it is even easier! Why add another plugin to your WordPress site, if you can do it more quickly and easily with WP-CLI?

One of the beauty of WP-CLI is it's extensibility, other developers are developing command cookbooks and plugins to extend its functionality all the time. One of these plugins is the WP Revisions CLI plugin. At the very end of this series we will even create our own custom commands in such a plugin.

Install the plugin as a wp-cli package:

$ wp package install trepmal/wp-revisions-cli

It will be installed and use composter to install any dependencies it needs:

21

<Install WP Revisions CLI as a WP-CLI package>

Now, we can list all the WordPress post revisions with:

$ wp revisions list

22

<List your WordPress revisions using the WP Revisions CLI>

As we can see in my case there are 7 revisions. I don't think I need them, so I am going to delete them with the clean command. I am using '-1' to delete all revisions, and specify only '--post_type=post' post revisions:

$ wp revisions clean -1 --post_type=post
$ wp revisions list

Now we can see, I no longer have any revisions bloating my database:

23

<Clean all Post revisions>

I can also check the value of my 'WP_POST_REVISIONS' option using:

$ wp revisions status

24

<Check the value of WP_POST_REVISIONS>

WP Revisions CLI also includes a 'wp revisions dump' command, that is faster than the clean command as it doesn't query each post before deleting the revisions. You can see the full list of commands and their parameters here.

That is the end of this tutorial, in the next part of the series we will delve deeper into useful commands for managing your WordPress installation, it's users, plugins and database, and more advanced configurations to manage many sites on an Instance at once.

In the Final tutorial in the series we will then reconfigure these commands to allow us to manage hundreds of WordPress sites across any number of Alibaba Cloud ECS Instances, all with a few commands on our local machine. We will also learn how to use WP-CLI in our WordPress development and deployment workflow to speed development, before we finally extend it with Custom Commands.

相关实践学习
通义万相文本绘图与人像美化
本解决方案展示了如何利用自研的通义万相AIGC技术在Web服务中实现先进的图像生成。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情:&nbsp;https://www.aliyun.com/product/ecs
目录
相关文章
|
6月前
|
存储 人工智能 搜索推荐
智教相伴,守正前行 ——AI 发展对幼儿教育的利弊探析
人工智能技术正快速渗透幼儿教育领域,以个性化教学、资源普惠、效率提升为幼儿教育注入新动能。同时,过度依赖 AI 也带来情感陪伴缺失、创造力受限、隐私安全等风险。本文辩证分析 AI 对幼儿教育的利好与弊端,提出技术赋能需坚守教育本质、坚持人机协同,让 AI 成为幼儿健康成长的助力而非阻力。
|
5月前
|
人工智能 弹性计算 数据可视化
OpenClaw部署成本说明及一键部署指南
OpenClaw是开源智能办公助手,支持任务管理与多平台接入。本文详解其零软件费、低硬件成本(云服务器首月9.9元起)及模型调用策略(百炼新用户享7000万Token免费额度),并提供阿里云一键部署指南,全程可视化、免写代码,5分钟上线专属AI助理!
993 15
|
11月前
|
数据采集 机器学习/深度学习 运维
量化合约系统开发架构入门
量化合约系统核心在于数据、策略、风控与执行四大模块的协同,构建从数据到决策再到执行的闭环工作流。强调可追溯、可复现与可观测性,避免常见误区如重回测轻验证、忽视数据质量或滞后风控。初学者应以MVP为起点,结合回测框架与实时风控实践,逐步迭代。详见相关入门与实战资料。
|
7月前
|
JSON 测试技术 API
代码生成不再是梦:GitHub Copilot 在测试开发脚本编写中的极效应用
本文分享GitHub Copilot如何革新测试开发:从环境配置、上下文优化到API测试框架搭建,通过需求注释自动生成结构化代码、批量创建测试用例、智能补全断言与数据生成,实测提升编写效率60%、覆盖率30%。强调人机协作——Copilot处理重复,工程师聚焦逻辑与决策。
|
7月前
|
网络协议 安全 Linux
2026年OpenClaw(原Moltbot、Clawdbot)最新部署教程及IMessage调用实现指南
在数字化办公与智能交互场景中,OpenClaw(原Moltbot、Clawdbot)的功能扩展性备受关注。本文将详细拆解OpenClaw的完整部署流程,并聚焦通过Tailscale构建安全网络隧道,实现Linux服务器部署的OpenClaw网关与Mac设备互联,进而管理Mac端iMessage的具体操作,全程遵循原有技术逻辑与操作细节,确保信息的准确性与实用性。
2852 5
|
存储 人工智能 测试技术
HarmonyOS Next~HarmonyOS应用测试全流程解析:从一级类目上架到二级类目专项测试
本文深入解析HarmonyOS应用测试全流程,涵盖从一级类目通用测试到二级类目专项测试的技术方案。针对兼容性、性能、安全测试及分布式能力验证等关键环节,提供详细实践指导与代码示例。同时,结合典型案例分析常见问题及优化策略,帮助开发者满足华为严苛的质量标准,顺利上架应用。文章强调测试在开发中的核心地位,助力打造高品质HarmonyOS应用。
836 2
|
网络协议 数据安全/隐私保护 网络架构
|
SQL 数据库连接 Go
Go语言数据库编程:GORM 的基本使用
GORM 是 Go 语言最流行的 ORM 框架,封装了 database/sql,支持自动迁移、关联关系、事务等功能,开发体验接近高层语言的 ORM。本文介绍了 GORM 的安装与初始化、模型定义、自动迁移、基本 CRUD 操作、条件构造器、钩子函数、事务处理、日志调试等内容,帮助开发者快速掌握其使用方法。
|
Linux 虚拟化 Windows
VMware隐藏黑科技!CentOS安装这个神器效率翻倍 文件互传竟比U盘还快?
### 安装VMtools的作用及步骤 安装VMtools可以实现Windows与CentOS之间的字符串和命令粘贴、文件夹共享等操作。在CentOS中,通过VMware菜单栏选择“VMware Tools”,将下载的压缩包拷贝到`/opt`目录并解压。接着进入终端,使用`./vmware-install.pl`命令完成安装。安装后重启系统,即可实现Windows与CentOS之间的字符和文件共享。具体步骤包括解压文件、启动命令行终端、安装VMtools以及配置文件共享。
VMware隐藏黑科技!CentOS安装这个神器效率翻倍 文件互传竟比U盘还快?