Manage WordPress on VPS with WP-CLI via Command Line

Manage WordPress on VPS with WP-CLI via Command Line

June 2026 · 7 min read

WP-CLI (WordPress Command Line Interface) is an essential tool for anyone managing WordPress on a VPS. It lets you perform everything the admin panel can do, but faster. You can run batch operations and automate tasks through cron jobs. Updating 50 plugins that would take 10 minutes in the admin panel can be done in 30 seconds with WP-CLI.

Install WP-CLI

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

# Check version
wp --info

Basic Commands You'll Use Frequently

# Must cd to WordPress root first
cd /var/www/example.com/public_html

# Check WordPress status
wp core version
wp core is-installed

# Update WordPress core
wp core update
wp core update-db

# Update all plugins
wp plugin update --all

# Update all themes
wp theme update --all

Managing Plugins

# List all plugins
wp plugin list

# Install a plugin
wp plugin install woocommerce --activate

# Delete plugin (not just deactivate)
wp plugin delete hello-dolly

# Activate/Deactivate
wp plugin activate contact-form-7
wp plugin deactivate contact-form-7

Managing Database

# Export database
wp db export /backup/$(date +%Y%m%d)-mysite.sql

# Import database
wp db import /backup/20260612-mysite.sql

# Search and Replace URL (after domain migration)
wp search-replace 'https://old-domain.com' 'https://new-domain.com'

# Optimize database
wp db optimize

Managing Users

# List users
wp user list

# Create new admin user
wp user create newadmin [email protected] --role=administrator --user_pass=SecurePass123

# Reset password
wp user update 1 --user_pass=NewPassword123

# Delete user
wp user delete 5 --reassign=1
Tip: When running as root, add --allow-root or run with sudo -u www-data wp ... to ensure proper file permissions.

Verify WP-CLI is Working Correctly

# Check WP-CLI is running
wp --info

# Check database connection
wp db check

# Verify WordPress integrity
wp core verify-checksums

# View error logs
wp log read 2>/dev/null || tail -20 /var/www/example.com/wp-content/debug.log
Tip: wp doctor check --all (requires wp-cli/doctor-command plugin) performs a comprehensive WordPress health check covering database, file permissions, and update status.

Common Issues & Troubleshooting

Error: "Error establishing a database connection"

wp-config.php has incorrect credentials or MySQL is not running:

# Check MySQL status
systemctl status mysql

# Test connection with credentials from wp-config.php
wp db query "SELECT 1"

# View credentials
wp config get DB_HOST DB_NAME DB_USER

"wp: command not found" after logout and login

WP-CLI is installed in a PATH location that your shell can see. Check if it exists in /usr/local/bin:

which wp
ls -la /usr/local/bin/wp

# If it doesn't exist, copy it again
sudo cp ~/wp-cli.phar /usr/local/bin/wp

Permission denied when running wp core update

The user running WP-CLI doesn't have write permissions to the WordPress directory:

# Run with www-data user
sudo -u www-data wp core update --path=/var/www/example.com/public_html

# Or change ownership temporarily
sudo chown -R $USER:www-data /var/www/example.com
wp core update
sudo chown -R www-data:www-data /var/www/example.com

Search-replace works but URL is still wrong

WordPress cache still holds the old URL. Flush the cache after search-replace:

wp search-replace 'https://old-domain.com' 'https://new-domain.com' --dry-run  # Test first
wp search-replace 'https://old-domain.com' 'https://new-domain.com'
wp cache flush
wp rewrite flush

Frequently Asked Questions

Can WP-CLI update WordPress while users are accessing the site?

Yes, but you should enable maintenance mode first: wp maintenance-mode activate, then update, then disable it with wp maintenance-mode deactivate. This reduces the risk of users seeing errors during the update.

Can I automate WordPress updates through cron?

Yes, add this to your crontab: 0 3 * * 0 cd /var/www/example.com/public_html && wp core update && wp plugin update --all. However, test on staging first because plugin updates can sometimes break your site.

Does WP-CLI work with Multisite?

Yes, add the --url=subsite.example.com flag or use --network for network-level operations, such as wp plugin update --all --network to update plugins across all sites in the network.