Cloud Service >> Knowledgebase >> WordPress >> Learn to Install and Manage WordPress Sites With WP-CLI
submit query

Cut Hosting Costs! Submit Query Today!

Learn to Install and Manage WordPress Sites With WP-CLI

WP-CLI (WordPress Command Line Interface) is a powerful tool that allows you to manage WordPress sites from the command line, streamlining tasks like installing WordPress and managing plugins, themes, and databases. Here’s a step-by-step guide on how to install and manage WordPress sites using WP-CLI:

Step 1: Install WP-CLI

1. SSH into Your Server:

    • Access your server via SSH. If you're on a shared hosting plan, ensure your hosting provider allows SSH access.

2. Download WP-CLI:

Run the following command to download the WP-CLI phar file:
bash
Copy code
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

  •  

3. Verify the Phar File:

Ensure the file is executable by running:
bash
Copy code
php wp-cli.phar --info

  •  

4. Make WP-CLI Globally Accessible:

Move the file to a directory in your PATH and rename it to wp:
bash
Copy code
chmod +x wp-cli.phar

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

  •  

5. Test WP-CLI Installation:

Check if WP-CLI is installed correctly by running:
bash
Copy code
wp --info

  •  

Step 2: Install WordPress Using WP-CLI

1. Navigate to the Web Directory:

Go to the directory where you want to install WordPress:
bash
Copy code
cd /path/to/your/web/directory

  •  

2. Download WordPress:

Download the latest version of WordPress:
bash
Copy code
wp core download

  •  

3. Create a wp-config.php File:

Generate the wp-config.php file with your database details:
bash
Copy code
wp config create --dbname=your_db_name --dbuser=your_db_user --dbpass=your_db_password --dbhost=localhost --dbprefix=wp_

  •  

4. Install WordPress:

Run the installation command, providing your site details:
bash
Copy code
wp core install --url="yourdomain.com" --title="Your Site Title" --admin_user="admin" --admin_password="strongpassword" --admin_email="[email protected]"

  •  

Step 3: Manage WordPress Plugins

1. List Installed Plugins:

Display a list of installed plugins:
bash
Copy code
wp plugin list

  •  

2. Install a New Plugin:

Install a plugin by name:
bash
Copy code
wp plugin install plugin-name

  •  

To activate the plugin immediately, add the --activate flag:
bash
Copy code
wp plugin install plugin-name --activate

  •  

3. Activate/Deactivate a Plugin:

Activate a plugin:
bash
Copy code
wp plugin activate plugin-name

  •  

Deactivate a plugin:
bash
Copy code
wp plugin deactivate plugin-name

  •  

4. Update Plugins:

Update all plugins to their latest versions:
bash
Copy code
wp plugin update --all

  •  

Update a specific plugin:
bash
Copy code
wp plugin update plugin-name

  •  

Step 4: Manage WordPress Themes

1. List Installed Themes:

Display a list of installed themes:
bash
Copy code
wp theme list

  •  

2. Install a New Theme:

Install a theme by name:
bash
Copy code
wp theme install theme-name

  •  

To activate the theme immediately, add the --activate flag:
bash
Copy code
wp theme install theme-name --activate

  •  

3. Activate/Deactivate a Theme:

Activate a theme:
bash
Copy code
wp theme activate theme-name

  •  

4. Update Themes:

Update all themes to their latest versions:
bash
Copy code
wp theme update --all

  •  

Update a specific theme:
bash
Copy code
wp theme update theme-name

  •  

Step 5: Manage WordPress Databases

1. Export Database:

Export the WordPress database to a SQL file:
bash
Copy code
wp db export

  •  
  • The file will be saved in the current directory with a .sql extension.

2. Import Database:

Import a SQL file into your WordPress database:
bash
Copy code
wp db import filename.sql

  •  

3. Optimize Database:

Optimize the WordPress database:
bash
Copy code
wp db optimize

  •  

4. Repair Database:

Repair the WordPress database:
bash
Copy code
wp db repair

  •  

Step 6: Update WordPress Core

1.Check for Updates:

See if there are any updates available for WordPress:
bash
Copy code
wp core check-update

  •  

2. Update WordPress Core:

Update WordPress to the latest version:
bash
Copy code
wp core update

  •  

3. Update the Database After Core Update:

Sometimes, after updating the core, you may need to update the database:
bash
Copy code
wp core update-db

  •  

Step 7: Run WordPress Maintenance Tasks

  1. Clear Cache:

Clear the WordPress cache:
bash
Copy code
wp cache flush

  •  
  1. Check for Deprecated Files:

Identify deprecated files in your installation:
bash
Copy code
wp core verify-checksums

  •  

Step 8: Secure Your WordPress Installation

  1. Secure Configuration:

Ensure secure file permissions:
bash
Copy code
sudo chown -R www-data:www-data /path/to/your/wordpress

sudo find /path/to/your/wordpress/ -type d -exec chmod 755 {} \;

sudo find /path/to/your/wordpress/ -type f -exec chmod 644 {} \;

  •  
  1. Limit Login Attempts:

Install and configure a security plugin to limit login attempts via WP-CLI:
bash
Copy code
wp plugin install limit-login-attempts --activate

  •  
  1. Enable Two-Factor Authentication:

Install a two-factor authentication plugin:
bash
Copy code
wp plugin install two-factor --activate

  •  

Step 9: Set Up Automated Backups

  1. Install a Backup Plugin:

Install a backup plugin like UpdraftPlus:
bash
Copy code
wp plugin install updraftplus --activate

  •  
  1. Schedule Regular Backups:

    • Configure the plugin settings via WP-CLI or the WordPress dashboard to schedule regular backups.

Step 10: Troubleshoot Common Issues

  1. Enable Debugging:

Enable WordPress debugging to troubleshoot issues:
bash
Copy code
wp config set WP_DEBUG true --raw

wp config set WP_DEBUG_LOG true --raw

  •  
  1. Check Site Status:

Get information about your site’s status:
bash
Copy code
wp site status

  •  
  1. Fix File Permissions:

Correct any file permission issues:
bash
Copy code
sudo chmod -R 755 /path/to/your/wordpress/wp-content/

sudo chmod -R 644 /path/to/your/wordpress/wp-config.php

  •  

Step 11: Uninstall WordPress (If Needed)

  1. Delete WordPress Files:

Remove all WordPress files from your server:
bash
Copy code
rm -rf /path/to/your/wordpress

  •  
  1. Drop the Database:

Delete the WordPress database:
bash
Copy code
wp db drop --yes

  •  

  1. Remove WP-CLI:

    • If you no longer need WP-CLI, remove it from your system:
      bash
      Copy code
      sudo rm /usr/local/bin/wp

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!