WordPress Installation via CLI (WP-CLI): Complete Production Guide
Introduction
WP-CLI (WordPress Command Line Interface) is a powerful tool that allows you to manage WordPress installations from the command line without using a web browser. This official WordPress tool enables rapid installations, automated deployments, bulk operations, and efficient management of WordPress sites - making it essential for developers, system administrators, and anyone managing multiple WordPress installations.
This comprehensive guide covers everything from basic WP-CLI installation to advanced WordPress management, automation, and production deployment strategies.
What You'll Learn
- Installing and configuring WP-CLI
- Complete WordPress installation from command line
- Database and user management via CLI
- Plugin and theme management
- Content management (posts, pages, media)
- WordPress core updates and maintenance
- Multi-site management
- Automated backup and deployment
- Security hardening via CLI
- Troubleshooting common issues
Why Use WP-CLI?
- Speed: Install WordPress in seconds vs minutes through browser
- Automation: Script repetitive tasks and deployments
- Remote Management: Manage sites via SSH without web access
- Bulk Operations: Update multiple plugins/themes simultaneously
- Consistency: Reproducible installations with same configuration
- CI/CD Integration: Automate testing and deployment pipelines
- Server Resources: No web server overhead for administrative tasks
Prerequisites
System Requirements
- Linux server (Ubuntu 20.04+, Debian 10+, CentOS 8+, Rocky Linux 8+)
- PHP 7.4 or higher (PHP 8.1+ recommended)
- MySQL 5.7+ or MariaDB 10.3+
- Web server (Nginx or Apache)
- Root or sudo access
- SSH access
Pre-Installation Requirements
# Ensure PHP is installed
php -v
# Ensure MySQL/MariaDB is running
sudo systemctl status mysql || sudo systemctl status mariadb
# Ensure web server is running
sudo systemctl status nginx || sudo systemctl status apache2
Installation
Step 1: Install WP-CLI
Download and Install WP-CLI
# Download WP-CLI
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
# Verify it works
php wp-cli.phar --info
# Make it executable
chmod +x wp-cli.phar
# Move to system path
sudo mv wp-cli.phar /usr/local/bin/wp
# Verify installation
wp --info
You should see output showing WP-CLI version and PHP information.
Enable Tab Completion (Optional but Recommended)
# Download bash completion script
sudo curl https://raw.githubusercontent.com/wp-cli/wp-cli/master/utils/wp-completion.bash -o /etc/bash_completion.d/wp-completion.bash
# Reload bash
source /etc/bash_completion.d/wp-completion.bash
# Add to .bashrc for permanent use
echo "source /etc/bash_completion.d/wp-completion.bash" >> ~/.bashrc
Step 2: Prepare WordPress Environment
Create Web Directory
# Create directory for WordPress site
sudo mkdir -p /var/www/mywordpresssite.com/html
# Set ownership (Ubuntu/Debian)
sudo chown -R $USER:www-data /var/www/mywordpresssite.com/html
# Set ownership (CentOS/Rocky)
sudo chown -R $USER:apache /var/www/mywordpresssite.com/html
# Set permissions
sudo chmod -R 755 /var/www/mywordpresssite.com
Create MySQL Database
# Login to MySQL
sudo mysql -u root -p
# Create database
CREATE DATABASE wordpress_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# Create user
CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'StrongPassword123!';
# Grant privileges
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'localhost';
# Flush privileges
FLUSH PRIVILEGES;
# Exit
EXIT;
Configuration
Complete WordPress Installation via WP-CLI
Navigate to Web Directory
cd /var/www/mywordpresssite.com/html
Download WordPress Core
# Download latest WordPress version
wp core download
# Download specific version
wp core download --version=6.4
# Download in different language
wp core download --locale=es_ES
# Verify download
ls -la
Create wp-config.php
# Create configuration file
wp config create \
--dbname=wordpress_db \
--dbuser=wordpress_user \
--dbpass='StrongPassword123!' \
--dbhost=localhost \
--dbprefix=wp_
# Add extra configuration (optional)
wp config set WP_DEBUG true --raw
wp config set WP_DEBUG_LOG true --raw
wp config set WP_DEBUG_DISPLAY false --raw
Install WordPress
# Complete WordPress installation
wp core install \
--url="https://mywordpresssite.com" \
--title="My WordPress Site" \
--admin_user="admin" \
--admin_password="SecureAdminPass123!" \
--admin_email="[email protected]"
# Verify installation
wp core version
wp core is-installed && echo "WordPress is installed successfully!"
Set Proper Permissions
# Set directory permissions
sudo find /var/www/mywordpresssite.com/html -type d -exec chmod 755 {} \;
# Set file permissions
sudo find /var/www/mywordpresssite.com/html -type f -exec chmod 644 {} \;
# Set ownership (Ubuntu/Debian)
sudo chown -R www-data:www-data /var/www/mywordpresssite.com/html
# Set ownership (CentOS/Rocky)
sudo chown -R apache:apache /var/www/mywordpresssite.com/html
# Allow WordPress to write to wp-content
sudo chmod -R 775 /var/www/mywordpresssite.com/html/wp-content
Configure Web Server
Nginx Configuration
sudo bash -c 'cat > /etc/nginx/sites-available/mywordpresssite.com <<EOF
server {
listen 80;
server_name mywordpresssite.com www.mywordpresssite.com;
root /var/www/mywordpresssite.com/html;
index index.php index.html;
access_log /var/log/nginx/mywordpresssite-access.log;
error_log /var/log/nginx/mywordpresssite-error.log;
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
location ~ \.php\$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
log_not_found off;
access_log off;
allow all;
}
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires 365d;
log_not_found off;
}
}
EOF'
# Enable site
sudo ln -s /etc/nginx/sites-available/mywordpresssite.com /etc/nginx/sites-enabled/
# Test configuration
sudo nginx -t
# Reload Nginx
sudo systemctl reload nginx
Apache Configuration
sudo bash -c 'cat > /etc/apache2/sites-available/mywordpresssite.com.conf <<EOF
<VirtualHost *:80>
ServerName mywordpresssite.com
ServerAlias www.mywordpresssite.com
DocumentRoot /var/www/mywordpresssite.com/html
<Directory /var/www/mywordpresssite.com/html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog \${APACHE_LOG_DIR}/mywordpresssite-error.log
CustomLog \${APACHE_LOG_DIR}/mywordpresssite-access.log combined
</VirtualHost>
EOF'
# Enable site
sudo a2ensite mywordpresssite.com.conf
# Enable mod_rewrite
sudo a2enmod rewrite
# Test configuration
sudo apache2ctl configtest
# Reload Apache
sudo systemctl reload apache2
Deployment
Plugin Management
Install and Manage Plugins
# Search for plugins
wp plugin search security --per-page=10
# Install plugin
wp plugin install wordfence
# Install and activate plugin
wp plugin install wordfence --activate
# Install specific version
wp plugin install jetpack --version=12.0
# List installed plugins
wp plugin list
# Activate plugin
wp plugin activate wordfence
# Deactivate plugin
wp plugin deactivate wordfence
# Update plugin
wp plugin update wordfence
# Update all plugins
wp plugin update --all
# Delete plugin
wp plugin delete wordfence
# Install multiple plugins at once
wp plugin install contact-form-7 wordpress-seo akismet --activate
Theme Management
# Search themes
wp theme search minimal --per-page=10
# Install theme
wp theme install twentytwentythree
# Install and activate theme
wp theme install twentytwentythree --activate
# List installed themes
wp theme list
# Activate theme
wp theme activate twentytwentythree
# Get active theme
wp theme status
# Update theme
wp theme update twentytwentythree
# Update all themes
wp theme update --all
# Delete theme
wp theme delete twentytwenty
Content Management
Post Management
# Create post
wp post create --post_type=post --post_title='My First Post' --post_content='This is the content' --post_status=publish --post_author=1
# List posts
wp post list
# List published posts
wp post list --post_status=publish
# Update post
wp post update 1 --post_title='Updated Title'
# Delete post
wp post delete 1
# Empty trash
wp post delete $(wp post list --post_status=trash --format=ids)
Page Management
# Create page
wp post create --post_type=page --post_title='About Us' --post_content='About our company' --post_status=publish
# List pages
wp post list --post_type=page
# Set front page
wp option update show_on_front page
wp option update page_on_front 2
Media Management
# Import media
wp media import /path/to/image.jpg
# List media
wp post list --post_type=attachment
# Regenerate thumbnails
wp media regenerate
# Import multiple images
for file in /path/to/images/*.jpg; do
wp media import "$file"
done
User Management
# Create user
wp user create johndoe [email protected] --role=editor --user_pass='SecurePass123!'
# List users
wp user list
# Update user
wp user update 2 --user_pass='NewPassword123!' --display_name='John Doe'
# Change user role
wp user set-role 2 administrator
# Delete user
wp user delete 2 --reassign=1
# Reset admin password
wp user update 1 --user_pass='NewAdminPass123!'
Database Operations
# Export database
wp db export backup.sql
# Export and compress
wp db export - | gzip > backup.sql.gz
# Import database
wp db import backup.sql
# Search and replace in database
wp search-replace 'http://oldsite.com' 'https://newsite.com' --dry-run
# Actual replacement
wp search-replace 'http://oldsite.com' 'https://newsite.com'
# Optimize database
wp db optimize
# Repair database
wp db repair
# Check database
wp db check
# Query database
wp db query "SELECT * FROM wp_users"
# Reset database (WARNING: Deletes all data!)
wp db reset --yes
WordPress Core Management
# Check for updates
wp core check-update
# Update WordPress core
wp core update
# Update to specific version
wp core update --version=6.4
# Update database
wp core update-db
# Verify core files
wp core verify-checksums
# Download language files
wp language core install es_ES
Monitoring
Site Health and Status
# Check WordPress version
wp core version
# Check if WordPress is installed
wp core is-installed && echo "Installed" || echo "Not installed"
# System info
wp cli info
# Check for issues
wp doctor check
# List options
wp option list
# Get specific option
wp option get siteurl
wp option get home
# Update option
wp option update blogdescription "My awesome site"
Performance Monitoring
# Cache operations
wp cache flush
# Rewrite rules
wp rewrite flush
# Transient operations
wp transient delete --all
# Check database size
wp db size --human-readable
# Count posts
wp post list --format=count
# Count plugins
wp plugin list --format=count
Site Maintenance
# Enable maintenance mode
wp maintenance-mode activate
# Disable maintenance mode
wp maintenance-mode deactivate
# Check maintenance mode status
wp maintenance-mode status
Troubleshooting
Common Issues and Solutions
WP-CLI Not Found
# Check if WP-CLI is in PATH
which wp
# If not found, add to PATH
export PATH=$PATH:/usr/local/bin
# Or create symlink
sudo ln -s /path/to/wp-cli.phar /usr/local/bin/wp
Permission Denied Errors
# Run WP-CLI as web server user
sudo -u www-data wp plugin list
# Or fix permissions
sudo chown -R www-data:www-data /var/www/mywordpresssite.com/html
Database Connection Errors
# Test database connection
wp db check
# Verify wp-config.php credentials
wp config get DB_NAME
wp config get DB_USER
wp config get DB_PASSWORD
wp config get DB_HOST
# Test MySQL connection manually
mysql -u wordpress_user -p wordpress_db
Plugin/Theme Installation Failures
# Check for write permissions
ls -la /var/www/mywordpresssite.com/html/wp-content
# Install plugin with verbose output
wp plugin install wordfence --debug
# Check disk space
df -h
Debug Mode
# Enable WP_DEBUG
wp config set WP_DEBUG true --raw
wp config set WP_DEBUG_LOG true --raw
wp config set WP_DEBUG_DISPLAY false --raw
# View debug log
tail -f /var/www/mywordpresssite.com/html/wp-content/debug.log
# Disable debug mode
wp config set WP_DEBUG false --raw
Security Best Practices
Secure WordPress Installation
# Remove default plugins
wp plugin delete hello akismet
# Install security plugins
wp plugin install wordfence --activate
wp plugin install limit-login-attempts-reloaded --activate
# Change admin username (create new admin, delete old)
wp user create newadmin [email protected] --role=administrator --user_pass='SecurePass123!'
wp user delete 1 --reassign=2
# Update salts and keys
wp config shuffle-salts
# Disable file editing
wp config set DISALLOW_FILE_EDIT true --raw
# Force SSL admin
wp config set FORCE_SSL_ADMIN true --raw
# Limit login attempts (if using plugin)
wp option update limit_login_lockout_duration 1800
File Permissions
# Secure wp-config.php
chmod 400 /var/www/mywordpresssite.com/html/wp-config.php
# Secure .htaccess
chmod 644 /var/www/mywordpresssite.com/html/.htaccess
# Remove write permissions from directories
find /var/www/mywordpresssite.com/html -type d -exec chmod 755 {} \;
find /var/www/mywordpresssite.com/html -type f -exec chmod 644 {} \;
# Allow uploads directory to be writable
chmod 775 /var/www/mywordpresssite.com/html/wp-content/uploads
Regular Updates
# Create update script
sudo bash -c 'cat > /usr/local/bin/wp-update.sh <<'EOF'
#!/bin/bash
cd /var/www/mywordpresssite.com/html
wp core update
wp plugin update --all
wp theme update --all
wp language core update
wp cache flush
EOF'
sudo chmod +x /usr/local/bin/wp-update.sh
Performance Optimization
Caching
# Install caching plugin
wp plugin install wp-super-cache --activate
# Or use WP-CLI cache commands
wp cache flush
wp cache add mykey myvalue
wp cache get mykey
wp cache delete mykey
Database Optimization
# Optimize database regularly
wp db optimize
# Clean up revisions
wp post delete $(wp post list --post_type=revision --format=ids) --force
# Clean spam comments
wp comment delete $(wp comment list --status=spam --format=ids) --force
# Clean trash comments
wp comment delete $(wp comment list --status=trash --format=ids) --force
Image Optimization
# Install image optimization plugin
wp plugin install ewww-image-optimizer --activate
# Regenerate thumbnails
wp media regenerate --yes
Automation and Backup
Automated Backup Script
sudo bash -c 'cat > /usr/local/bin/wp-backup.sh <<'EOF'
#!/bin/bash
# Configuration
SITE_PATH="/var/www/mywordpresssite.com/html"
BACKUP_DIR="/backup/wordpress"
DATE=$(date +%Y%m%d_%H%M%S)
SITE_NAME="mywordpresssite"
# Create backup directory
mkdir -p $BACKUP_DIR
# Database backup
cd $SITE_PATH
wp db export - | gzip > $BACKUP_DIR/${SITE_NAME}_db_${DATE}.sql.gz
# Files backup
tar -czf $BACKUP_DIR/${SITE_NAME}_files_${DATE}.tar.gz -C /var/www mywordpresssite.com
# Keep only last 7 days of backups
find $BACKUP_DIR -name "${SITE_NAME}_*" -type f -mtime +7 -delete
echo "Backup completed: $DATE"
EOF'
sudo chmod +x /usr/local/bin/wp-backup.sh
# Schedule backup with cron
sudo crontab -e
# Add line:
# 0 2 * * * /usr/local/bin/wp-backup.sh >> /var/log/wp-backup.log 2>&1
Deployment Script
sudo bash -c 'cat > /usr/local/bin/wp-deploy.sh <<'EOF'
#!/bin/bash
# Configuration
SITE_PATH="/var/www/mywordpresssite.com/html"
cd $SITE_PATH
# Backup before deployment
wp db export backup-pre-deploy.sql
# Enable maintenance mode
wp maintenance-mode activate
# Update WordPress
wp core update
wp core update-db
# Update plugins and themes
wp plugin update --all
wp theme update --all
# Flush cache
wp cache flush
wp transient delete --all
wp rewrite flush
# Disable maintenance mode
wp maintenance-mode deactivate
echo "Deployment completed successfully"
EOF'
sudo chmod +x /usr/local/bin/wp-deploy.sh
Advanced Usage
Multi-site Management
# Convert to multisite
wp core multisite-convert
# List sites
wp site list
# Create new site
wp site create --slug=newsite --title="New Site" --email="[email protected]"
# Delete site
wp site delete 2 --yes
# Update site
wp site url 2 https://newsite.example.com
Custom Development
# Generate plugin
wp scaffold plugin my-plugin
# Generate theme
wp scaffold theme my-theme
# Generate post type
wp scaffold post-type book --plugin=my-plugin
# Generate taxonomy
wp scaffold taxonomy genre --post_types=book --plugin=my-plugin
Bulk Operations
# Install multiple plugins from file
cat plugins.txt | xargs -I {} wp plugin install {} --activate
# Export all posts to CSV
wp post list --post_type=post --format=csv > posts.csv
# Bulk create users
wp user generate --count=100 --role=subscriber
Conclusion
WP-CLI dramatically simplifies WordPress management, enabling rapid installations, efficient maintenance, and powerful automation. This production-ready approach ensures consistent, reproducible WordPress deployments.
Key Takeaways
- WP-CLI provides complete WordPress management from command line
- Automation saves time and reduces errors
- Bulk operations manage multiple sites efficiently
- Integration with CI/CD pipelines enables modern deployment workflows
- Regular backups and updates maintain site health and security
Best Practices
- Always backup before major operations
- Test commands in staging environment first
- Use version control for wp-config.php (without credentials)
- Automate routine maintenance tasks
- Keep WP-CLI updated
- Document your deployment procedures
Next Steps
- Set up automated backups
- Create deployment scripts for your workflow
- Implement staging environment
- Configure monitoring and alerts
- Explore WP-CLI packages for extended functionality
WP-CLI is an indispensable tool for professional WordPress management. Master it to transform your WordPress workflow!
Happy deploying!


