How to Install and Manage WordPress from the Terminal Using WP-CLI?

NewAgeWeb

NewAgeWeb

WordPress is one of the most widely used platforms for building and managing websites, with millions of users worldwide. Thanks to its graphical interface for admins, it’s known for being easy to use. However, for developers and system administrators, a powerful WP-CLI tool allows you to manage WordPress directly from the command line (the terminal).

WP-CLI can be a game-changer if you need to manage multiple sites, automate tasks, or handle server-level tasks without opening a web browser. In this guide, we’ll show you how to install and set up WP-CLI and walk you through basic and advanced commands to help you manage WordPress from the terminal. Whether you’re a developer or system admin, WP-CLI will help make your work faster, easier, and more efficient.

What is WP-CLI?

WP-CLI (WordPress Command Line Interface) is a free, open-source tool that lets you manage your WordPress site directly from the command line. Instead of using the WordPress dashboard, you can manage plugins, themes, updates, posts, and users much faster through simple commands in the terminal.

WP-CLI works on various UNIX-based systems, including Linux, macOS, and Windows through Windows Subsystem for Linux (WSL). It simplifies tasks requiring a web interface, making it a handy tool for developers and server admins.

Why Use WP-CLI?

Here are a few good reasons to use WP-CLI for managing your WordPress site:

  • Speed and Efficiency: Using command-line commands is faster than clicking through the graphical user interface (GUI), especially for repetitive tasks or bulk actions.
  • Automation: You can set up scripts to automate tasks like backups, updates, and migrations, saving you time.
  • Server-Level Management: You can handle server tasks like backups and migrations directly from the terminal without logging into the WordPress admin dashboard or using FTP.
  • Advanced Control: WP-CLI gives you more control over your site, which is excellent for developers working with custom themes or plugins.
  • Multisite Support: WP-CLI makes it much easier than the admin dashboard if you manage multiple WordPress sites in a multisite network.

System Requirements for WP-CLI

Before you install WP-CLI, make sure your server or local environment meets these requirements:

  • PHP 5.6.0 or higher: WP-CLI needs PHP to run. For better performance, it’s best to use PHP 7 or higher.
  • WordPress 3.7 or higher: WP-CLI works with most recent versions of WordPress, but you need at least version 3.7.
  • Unix-based OS: WP-CLI works best on Linux or macOS. It can also be used on Windows via WSL (Windows Subsystem for Linux).
  • SSH Access: You’ll need SSH access to your server to manage a live WordPress site. Make sure your hosting provider offers this feature.

How to Install WP-CLI?

Here are the steps to install WP-CLI.

Step 1: Access Your Server via SSH

To install WP-CLI, you need to access your server using SSH.

  • You should already have SSH access using a VPS, dedicated server, or managed WordPress hosting provider like Cloudways.
  • Check if SSH is available if you’re on a shared hosting plan.

To log in, open a terminal (on Linux or macOS) or a terminal emulator like PuTTY (on Windows). Then, enter the following command:

ssh user@yourserver.com

Replace user@yourserver.com with your actual server username and IP address.

Step 2: Download WP-CLI

Once you’re logged into your server, run this command to download the WP-CLI Phar file:

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

This command will download the WP-CLI Phar file, a PHP archive that holds the WP-CLI program.

Step 3: Make the WP-CLI File Executable

To make the WP-CLI file executable, simply run this command:

chmod +x wp-cli.phar

This command gives the WP-CLI file permission to be run as a program.

Step 4: Move WP-CLI to a Global Location

To make WP-CLI accessible from anywhere on your system, move the file to a folder that’s part of your system’s PATH:

sudo mv wp-cli.phar /usr/local/bin/wp

After this, you can use WP-CLI by typing wp in the terminal.

Step 5: Verify Installation

To check if WP-CLI was installed correctly, simply type this command:

wp --info

This will show details about your WP-CLI installation, such as the version number and system information.

Basic WP-CLI Commands

Once you have WP-CLI installed, you can start using it to manage your WordPress site. Here are some simple commands to get you started:

1. Download WordPress

To download the latest version of WordPress, follow these steps:

  1. Go to the folder where you want to download WordPress.
  2. Run this command:
wp core download

This will download all the core WordPress files into the folder you’re currently in.

2. Create wp-config.php File

Once you’ve downloaded WordPress, you need to create the wp-config.php file, which contains the settings for your WordPress site. To do this, run the following command:

wp config create --dbname=your_db_name --dbuser=your_db_user --dbpass=your_db_password --dbhost=localhost

Ensure to replace your_db_name, your_db_user, and your_db_password with your database details. This will configure your WordPress site to connect to the correct database.

3. Install WordPress

To finish the installation, run this command:

wp core install --url=your_domain.com --title="Your Site Title" --admin_user=admin --admin_password=your_password --admin_email=youremail@example.com

Make sure to replace the following placeholders with your accurate details:

  • your_domain.com with your website’s domain
  • Your Site Title with the name of your site
  • admin with your desired admin username
  • your_password with your chosen password
  • youremail@example.com with your admin email address

Advanced WP-CLI Commands

WP-CLI offers many commands for advanced tasks. Here are some of the most useful ones:

1. Update WordPress Core

To update your WordPress to the latest version, simply run the following command:

wp core update

2. Manage Plugins

Here’s how to manage plugins with WP-CLI in an easy-to-read format:

Install a Plugin: To install a plugin, use this command:

wp plugin install plugin-name

Activate a Plugin: To activate a plugin after installing it, run:

wp plugin activate plugin-name

Deactivate a Plugin: To deactivate a plugin, use this command:

wp plugin deactivate plugin-name

Just replace the plugin-name with the name of the plugin you want to manage!

3. Manage Themes

Install and activate themes with WP-CLI.

Install a Theme:

To install a theme, use this command:

wp theme install theme-name

Replace the theme-name with the name of the theme you want to install.

Activate a Theme:

To activate the theme you just installed, use this command:

wp theme activate theme-name

Again, replace the theme-name with the theme name you want to activate.

4. Search and Replace Strings in the Database

To search for a specific string in your database and replace it with a new one (like updating URLs during a migration), use this command:

wp search-replace 'old-string' 'new-string'

Just replace ‘old-string’ with the text you want to change and ‘new-string’ with the new text you’d like to use.

WordPress Database Management

WP-CLI provides several commands to help manage your WordPress database, an essential part of your site. Here’s how you can use these commands:

Create the database (if it doesn’t already exist):

wp db create

Optimize the database (helps improve performance):

wp db optimize

Export the database (back it up or move it to another site):

wp db export

Import the database (restore a backup or move data from another site):

wp db import database-file.sql

These simple commands give you complete control over your WordPress database.

Automating Tasks with WP-CLI

One of WP-CLI’s best features is its ability to automate repetitive tasks. You can create shell scripts that combine WP-CLI commands to handle tasks like backups, updates, or even managing multiple WordPress sites automatically.

For example, you can write a simple script to update all your plugins and themes:

#!/bin/bash
wp plugin update --all
wp theme update --all

Once you’ve created the script, you can set it to run automatically at scheduled times using cron jobs or run it whenever you need. This helps save time and ensures your site stays up-to-date without manual effort.

Conclusion

WP-CLI is a powerful tool that helps you manage WordPress sites directly from the terminal. It makes tasks faster and more efficient and gives you complete control over your WordPress site. From updating plugins and themes to managing your database and automating tasks, WP-CLI helps you do it all. Whether working on one site or many, WP-CLI can save you time and make managing your sites easier.

Following the steps in this guide, you’ll quickly learn how to install, set up, and master WP-CLI. 

Keep Exploring