Hướng dẫn cài đặt WP-CLI trên máy chủ Linux

Giới thiệu

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.

Điều kiện cài đặt WP-CLI

  • Bạn cần phải có quyền root của Server

Hướng dẫn cài đặt

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

Để cài đặt WP-CLI bạn cần SSH vào VPS/Server của mình và chạy các lệnh cài đặt sau

Đàm Trung Kiên
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
    
Hướng dẫn cài đặt WP-CLI trên máy chủ Linux

Giải thích các lệnh trên:

  • Lệnh 1: Tải WP-CLI tool từ trang chủ Github.
  • Lệnh 2: Xem thông tin WP-CLI đã tải xuống
  • Lệnh 3: Phân quyền thực thi fille đã tải
  • Lệnh 4: Đổi tên wp-cli.phar thành wp và move File đến đường dẫn /usr/local/bin (nơi mà bạn có thể sử dụng lệnh wp ở bất cứ đâu trên VPS/Server)

Nếu các bạn muốn cài đặt nhanh hơn, chỉ cần copy lệnh bên dưới và dán vào Server của mình là xong.

Đàm Trung Kiên
wget https://tool.damtrungkien.info/cli/cli.sh && chmod +x cli.sh && bash cli.sh
    

Bước 2: Cách 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à sử dụng dướ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 CLI. Ngoài ra để sử dụng được WP-CLI bạn cần di chuyển vào đúng thư mục Document Root của website WordPress cần thao tác.

Và dưới đây là một số lệnh WP-CLI cơ bản mà bạn có thể xem qua

  • Kiểm tra phiên bản WP-CLI
Đàm Trung Kiên
wp --info
    
Hướng dẫn cài đặt WP-CLI trên máy chủ Linux
  • Liệt kê các tính năng được hỗ trợ của WP-CLI.
Đàm Trung Kiên
wp help --allow-root
    
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ụ ở đây nếu bạn cần thao tác với Plugin, thì bạn có thể sử dụng câu lệnh như sau

Đàm Trung Kiên
wp plugin --allow-root
    

Tiếp đó WP-CLI sẽ hiển thị chi tiết cho bạn cụ thể các lệnh thao tác với Plugin trên WP-CLI

[root@da1 ~]# wp plugin --allow-root
usage: wp plugin activate [<plugin>...] [--all] [--network]
   or: wp plugin auto-updates <command>
   or: wp plugin deactivate [<plugin>...] [--uninstall] [--all] [--network]
   or: wp plugin delete [<plugin>...] [--all]
   or: wp plugin get <plugin> [--field=<field>] [--fields=<fields>] [--format=<format>]
   or: wp plugin install <plugin|zip|url>... [--version=<version>] [--force] [--activate] [--activate-network] [--insecure]
   or: wp plugin is-active <plugin> [--network]
   or: wp plugin is-installed <plugin>
   or: wp plugin list [--<field>=<value>] [--field=<field>] [--fields=<fields>] [--format=<format>] [--status=<status>] [--skip-update-check]
   or: wp plugin path [<plugin>] [--dir]
   or: wp plugin search <search> [--page=<page>] [--per-page=<per-page>] [--field=<field>] [--fields=<fields>] [--format=<format>]
   or: wp plugin status [<plugin>]
   or: wp plugin toggle <plugin>... [--network]
   or: wp plugin uninstall [<plugin>...] [--deactivate] [--skip-delete] [--all]
   or: wp plugin update [<plugin>...] [--all] [--exclude=<name>] [--minor] [--patch] [--format=<format>] [--version=<version>] [--dry-run] [--insecure]
   or: wp plugin verify-checksums [<plugin>...] [--all] [--strict] [--format=<format>] [--insecure]

See 'wp help plugin <command>' for more information on a specific command.
[root@da1 ~]#

Dưới đây là một ví dụ cụ thể về quản lý Plugin với các thao tác list / activate / deactivate/ delete Plugin.

Hướng dẫn cài đặt WP-CLI trên máy chủ Linux

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

Leave a Reply

Your email address will not be published. Required fields are marked *