Home Kiến thức WebsiteWordPress Hướng dẫn cài đặt WP-CLI để quản trị WordPress

Hướng dẫn cài đặt WP-CLI để quản trị WordPress

by Thạch Phạm
Published: Last Updated on
A+A-
Reset

WP-CLI là gì?

WP-CLI là một tính năng được ra đời và phát triển từ những năm 2011 , với WP-CLI bạn có thể dễ dàng thao tác quản lý website WordPress của mình một cách đơn giản thông qua giao diện dòng lệnh (Command). Với cách thức thông thường , khi các bạn cần cài đặt Plugins, Themes, hay chỉnh sửa liên quan đến WordPress các bạn cần phải truy cập vào trình quản trị WordPress và thực hiện các thao tác bằng cách click chuột, việc này đôi khi mất hơi rườm rà và mất thời gian đúng không.

Chính vì thể WP-CLI được phát triển là để giúp bạn xử lý các vấn đề trên ngay cả khi chúng ta không cần phải truy cập vào trình quản trị của WordPress mà vẫn có thể thao tác quản trị được website của mình một cách nhanh chóng và tiết kiệm thời gian hơn khá nhiều.

Vậy làm sao để cài đặt được và sử dụng WP-CLI.

Nếu quý khách đang sử dụng dịch vụ Share Hosting tại AZDIGI thì mặc định WP-CLI đã được cài đặt sẵn, quý khách có thể sử dụng được bằng cách sử dụng trực tiếp từ cửa sổ Terminal trên giao diện Hosting hoặc có thể sử dụng thông tin Host để SSH thông qua các phần mềm như Putty , Xshell..vv.

Vậy nếu trường hợp các bạn sử dụng các dịch vụ Server/VPS nhưng chưa được cài đặt sẵn WP-CLI thì phải cài đặt và sử dụng như thế nào, thì ở bài viết này AZDIGI sẽ hướng dẫn bạn cách cài đặt WP-CLI lên máy chủ của bạn một cách chi tiết nhất.

Các bước cài đặt

Bước 1: Cài đặt WP-CLI

Đầu tiên các bạn cần SSH vào VPS của mình với quyền root, nếu chưa biết cách SSH vào VPS các bạn có thể xem qua hướng dẫn bên dưới.

Sau khi đã SSH thành công các bạn cần chạy các lệnh lần lượt như bên dưới nhé:

AZDIGI Tutorial
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
    

Chúc thích lệnh:

  1. Tải WP-CLI tool từ trang chủ Github.
  2. Xác nhận tải thành công.
  3. Phân quyền thực thi fille wp-cli.phar.
  4. Di chuyển WP-CLI đến phân vùng mà tất cả người dùng có thể sử dụng lệnh và đổi tên từ wp-cli.phar thành wp

Lý do bạn cần đổi wp-cli.phar thành wp là để trong quá trình sử dụng lệnh, bạn chỉ cần gõ wp thay vì wp-cli.phar để đơn giản và dễ nhớ hơn.

[root@sv106 ~]# curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 5437k  100 5437k    0     0  4048k      0  0:00:01  0:00:01 --:--:-- 4051k
[root@sv106 ~]# php wp-cli.phar --info
OS:	Linux 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64
Shell:	/bin/bash
PHP binary:	/usr/local/php72/bin/php72
PHP version:	7.2.33
php.ini used:	/usr/local/php72/lib/php.ini
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/root
WP-CLI packages dir:	
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	2.4.0
[root@sv106 ~]# chmod +x wp-cli.phar
[root@sv106 ~]# sudo mv wp-cli.phar /usr/local/bin/wp

Như vậy là WP-CLI đã được cài đặt thành công, các bạn đã có thể sử dụng WP-CLI để quản trị website WordPress của mình.

Bước 2: Tìm hiểu cách thức sử dụng WP-CLI

Lưu ý: Khi bạn cài đặt WP-CLI lên các máy chủ server/VPS và đang thực hiện thao tác tại quyền root, thì khi sử dụng các lệnh của WP-CLI các bạn cần thêm vào option –allow-root phía sau dòng lệnh nếu không các bạn sẽ nhận được cảnh báo. Nếu các bạn đang sử dụng các dịch vụ Sharehost hoặc không phải thao thác từ user root thì bạn không cần phải thêm option này vào phía sau.

Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress installation exists under.
If you REALLY mean to run this as root, we won't stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS.
If you'd like to continue as root, please run this again, adding this flag:  --allow-root
If you'd like to run it as the user that this site is under, you can run the following to become the respective user:
    sudo -u USER -i -- wp 

Bây giờ chúng ta sẽ đi tìm hiểu một số lệnh cơ bản của WP-CLI trước nhé.

  • Lệnh kiểm tra phiên bản WP-CLI.
AZDIGI Tutorial
wp --info 
    
[root@sv106 ~]# wp --info --allow-root
OS:	Linux 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64
Shell:	/bin/bash
PHP binary:	/usr/local/php72/bin/php72
PHP version:	7.2.33
php.ini used:	/usr/local/php72/lib/php.ini
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/root
WP-CLI packages dir:	
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	2.4.0
  • Lệnh kiểm tra danh sách các tính năng được hỗ trợ qua WP-CLI.
AZDIGI Tutorial
wp help
    

Như hình bên dưới các bạn có thể thấy WP-CLI hỗ trợ rất nhiều tính năng liên quan đến WordPress, để sử dụng tính năng nào các bạn chỉ việc gõ lệnh tương ứng: wp <tinh năng cần sử dụng>

NAME
  wp
DESCRIPTION
  Manage WordPress through the command-line.
SYNOPSIS
  wp 
SUBCOMMANDS
  cache                 Adds, removes, fetches, and flushes the WP Object Cache object.
  cap                   Adds, removes, and lists capabilities of a user role.
  cli                   Reviews current WP-CLI info, checks for updates, or views defined aliases.
  comment               Creates, updates, deletes, and moderates comments.
  config                Generates and reads the wp-config.php file.
  core                  Downloads, installs, updates, and manages a WordPress installation.
  cron                  Tests, runs, and deletes WP-Cron events; manages WP-Cron schedules.
  db                    Performs basic database operations using credentials stored in wp-config.php.
  embed                 Inspects oEmbed providers, clears embed cache, and more.
  eval                  Executes arbitrary PHP code.
  eval-file             Loads and executes a PHP file.
  export                Exports WordPress content to a WXR file.
  help                  Gets help on WP-CLI, or on a specific command.
  i18n                  Provides internationalization tools for WordPress projects.
  import                Imports content from a given WXR file.
  language              Installs, activates, and manages language packs.
  maintenance-mode      Activates, deactivates or checks the status of the maintenance mode of a site.
  media                 Imports files as attachments, regenerates thumbnails, or lists registered image sizes.
  menu                  Lists, creates, assigns, and deletes the active theme's navigation menus.
  network               Perform network-wide operations.
  option                Retrieves and sets site options, including plugin and WordPress settings.
  package               Lists, installs, and removes WP-CLI packages.
  plugin                Manages plugins, including installs, activations, and updates.
  post                  Manages posts, content, and meta.
  post-type             Retrieves details on the site's registered post types.
  rewrite               Lists or flushes the site's rewrite rules, updates the permalink structure.
  role                  Manages user roles, including creating new roles and resetting to defaults.
  scaffold              Generates code for post types, taxonomies, plugins, child themes, etc.
  search-replace        Searches/replaces strings in the database.
  server                Launches PHP's built-in web server for a specific WordPress installation.
  shell                 Opens an interactive PHP console for running and testing PHP code.
  sidebar               Lists registered sidebars.
  site                  Creates, deletes, empties, moderates, and lists one or more sites on a multisite installation.
  super-admin           Lists, adds, or removes super admin users on a multisite installation.
  taxonomy              Retrieves information about registered taxonomies.
  term                  Manages taxonomy terms and term meta, with create, delete, and list commands.
  theme                 Manages themes, including installs, activations, and updates.
  transient             Adds, gets, and deletes entries in the WordPress Transient Cache.
  user                  Manages users, along with their roles, capabilities, and meta.
  widget                Manages widgets, including adding and moving them within sidebars.

Ví dụ mình cần xem chi tiết các lệnh liên quan đến Plugin , trước hết các bạn nhấn phím Q để thoát khỏi giao diện trên và sử dụng lệnh tương ứng như sau:

AZDIGI Tutorial
wp plugin
    

Tại đây bạn sẽ được liệt kê các lệnh cụ thể được hỗ trợ với Plugin như: active, deactivate, delete, uninstall Plugin..vv

[root@sv106 ~]# wp plugin --allow-root
usage: wp plugin activate [...] [--all] [--network]
   or: wp plugin deactivate [...] [--uninstall] [--all] [--network]
   or: wp plugin delete [...] [--all]
   or: wp plugin get  [--field=] [--fields=] [--format=]
   or: wp plugin install ... [--version=] [--force] [--activate] [--activate-network]
   or: wp plugin is-active 
   or: wp plugin is-installed 
   or: wp plugin list [--=] [--field=] [--fields=] [--format=]
   or: wp plugin path [] [--dir]
   or: wp plugin search  [--page=] [--per-page=] [--field=] [--fields=] [--format=]
   or: wp plugin status []
   or: wp plugin toggle ... [--network]
   or: wp plugin uninstall [...] [--deactivate] [--skip-delete] [--all]
   or: wp plugin update [...] [--all] [--exclude=] [--minor] [--patch] [--format=] [--version=] [--dry-run]
   or: wp plugin verify-checksums [...] [--all] [--strict] [--format=]

See 'wp help plugin ' for more information on a specific command.

Như vậy về cơ bản là bạn có thể nắm được cơ chế hoạt động của WP-CLI với WordPress rồi nhé. Ở bài viết sau mình sẽ hướng dẫn các bạn cách cài đặt một website WordPress với WP-CLI và sử dụng WP-CLI để quản trị Plugin, Theme ..vv.

Tổng kết

Việc sử dụng WP-CLI sẽ hỗ trợ bạn rất nhiều trong việc quản lý website WordPess của mình, nó giúp thao tác công việc của bạn được diễn ra nhanh hơn chỉ bằng vài dòng lệnh đơn giản, bạn vẫn có thể thực hiện quản lý website của mình được mà không cần phải truy cập vào giao diện quản trị website.  Nếu thấy bài viết hay và hữu ích, bạn hãy chia sẻ rộng rãi đến mọi người nhé.

Chúc các bạn thực hiện thành công.!

Nếu các bạn cần hỗ trợ các bạn có thể liên hệ bộ phận hỗ trợ theo các cách bên dưới:

5/5 - (3 votes)

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign