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

本文涉及的产品
云服务器 ECS,每月免费额度200元 3个月
云服务器ECS,u1 2核4GB 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.

相关实践学习
一小时快速掌握 SQL 语法
本实验带您学习SQL的基础语法,快速入门SQL。
7天玩转云服务器
云服务器ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,可降低 IT 成本,提升运维效率。本课程手把手带你了解ECS、掌握基本操作、动手实操快照管理、镜像管理等。了解产品详情:&nbsp;https://www.aliyun.com/product/ecs
目录
相关文章
|
Linux 开发工具 C#
Tencent Cloud Code Analysis介绍及安装部署
Tencent Cloud Code Analysis介绍及安装部署
854 0
Tencent Cloud Code Analysis介绍及安装部署
|
弹性计算 Shell 网络安全
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.
1255 0
Advanced WordPress Management Using WP-CLI on Alibaba Cloud ECS – Part 3
|
弹性计算 Linux PHP
Advanced WordPress Management Using WP-CLI on Alibaba Cloud ECS – Part 2
This three-part tutorial walks you through setting up WordPress advanced management with WP-CLI. Part 2 focuses on managing WordPress with WP-CLI on Alibaba Cloud ECS.
2446 0
Advanced WordPress Management Using WP-CLI on Alibaba Cloud ECS – Part 2
|
弹性计算 NoSQL 安全
AMP for E-Commerce Part 2: Creating Backend with Alibaba Cloud ApsaraDB for MongoDB
In this three-part tutorial, we will explore how to create a fully functional e-commerce mobile application using AMP.
1548 0
AMP for E-Commerce Part 2: Creating Backend with Alibaba Cloud ApsaraDB for MongoDB
|
JSON 弹性计算 NoSQL
AMP for E-Commerce Part 3: Integrating the Entire Application with Alibaba Cloud ApsaraDB for MongoDB
In this three-part tutorial, we will explore how to create a fully functional e-commerce mobile application using AMP.
1773 0
AMP for E-Commerce Part 3: Integrating the Entire Application with Alibaba Cloud ApsaraDB for MongoDB
|
弹性计算 Ubuntu Linux
3 Ways to Set Up a Linux Server on Alibaba Cloud
Find out how you can quickly and easily set up a Linux server on Alibaba Cloud ECS. This tutorial is designed for first-time users of Alibaba Cloud.
3000 0
3 Ways to Set Up a Linux Server on Alibaba Cloud
|
关系型数据库 MySQL 应用服务中间件
WordPress with LEMP on Alibaba Cloud – Part 4 Installing WordPress on Your Alibaba Cloud ECS Instance
In the previous tutorials we secured an Alibaba Ubuntu 16.04 ECS instance, then installed NGINX, MariaDB, and PHP7 to complete our LEMP stack.
2965 0
|
安全 网络安全
Comparing Security Cloud Tools: Alibaba Cloud Anti-DDoS vs. AWS Shield
To maintain a secure web application, you need to invest in security tools. But because there are so many security tools available, it can be difficult to decide which one to choose.
3249 0
|
安全 Windows
Preventing Ransomware Using Alibaba Cloud Server Guard
On the evening of May 12, the WanaCrypt0r 2.0 (WannaCry2.0 for short) ransomware broke out worldwide.
2177 0
|
网络协议 Go
WordPress with LEMP on Alibaba Cloud – Part 5 Using DirectMail for WordPress Transactional Email
In the previous tutorials we successfully installed WordPress on a highly performant lamp stack using an Alibaba ECS Cloud Instance.
2602 0