Install WordPress with WP-CLI on a VPS or Dedicated Server
This guide explains how to install WordPress automatically with WP-CLI on your VPS or dedicated server.
Order a Server
To host your WordPress site, HostMyServers offers several suitable options:
- Performance VPS - Ideal for high-traffic sites
- NVMe VPS - Excellent value for money
- Eco Dedicated Servers - For projects requiring more resources
- Performance Dedicated Servers - Maximum performance
Prerequisites
- SSH access as root or user with sudo privileges
- PHP installed (version ≥ 7.4) with required extensions
- MySQL or MariaDB installed and configured
- Web server (Apache or Nginx) configured
- A domain name pointing to the server (optional)
SSH Connection
Connect to your server via SSH:
ssh root@your_server_ip_address
Or with a user with sudo privileges:
ssh your_user@your_server_ip_address
Installing WP-CLI
-
Download the WP-CLI phar file:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.pharOr with wget:
wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -
Make the file executable:
chmod +x wp-cli.phar -
Move the executable for global use:
sudo mv wp-cli.phar /usr/local/bin/wp -
Verify the installation:
wp --infoYou should see information about PHP and WP-CLI versions.
Database Configuration
Create a MySQL/MariaDB database for WordPress:
-
Connect to MariaDB/MySQL:
sudo mysql -u root -p -
Create the database and user:
CREATE DATABASE wordpress_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wordpress_user'@'localhost' IDENTIFIED BY 'secure_password';
GRANT ALL PRIVILEGES ON wordpress_db.* TO 'wordpress_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;WarningReplace
wordpress_db,wordpress_userandsecure_passwordwith your own values.
WordPress Installation
-
Ensure the folder belongs to the web server user:
sudo chown -R www-data:www-data /var/www/your_domain -
Switch to the www-data user:
sudo -u www-data bash -
Navigate to the web directory:
cd /var/www/your_domain -
Download WordPress:
wp core download --locale=en_US -
Generate the
wp-config.phpfile:wp config create \
--dbname=wordpress_db \
--dbuser=wordpress_user \
--dbpass='secure_password' \
--dbhost=localhost \
--dbprefix=wp_ \ -
Install WordPress:
wp core install \
--url="https://your-domain.com" \
--title="Your Site Title" \
--admin_user="admin" \
--admin_password="StrongPassword123!" \
--admin_email="your@email.com" \
Permissions and Security
-
Set correct permissions:
# Directory permissions
sudo find /var/www/your_domain -type d -exec chmod 755 {} \;
# File permissions
sudo find /var/www/your_domain -type f -exec chmod 644 {} \;
# Special permissions for wp-content
sudo chmod -R 775 /var/www/your_domain/wp-content -
Secure the
wp-config.phpfile:sudo chmod 600 /var/www/your_domain/wp-config.php
Useful WP-CLI Commands
All commands below must be run as the www-data user. Connect with sudo -u www-data bash before running them.
WordPress Core Management
# Update WordPress
wp core update
# Check version
wp core version ```
### Plugin Management
```bash
# List plugins
wp plugin list
# Install and activate a plugin
wp plugin install plugin_name --activate
# Update all plugins
wp plugin update --all
# Deactivate a plugin
wp plugin deactivate plugin_name ```
### Theme Management
```bash
# List themes
wp theme list
# Install and activate a theme
wp theme install theme_name --activate
# Update all themes
wp theme update --all ```
### User Management
```bash
# Create a user
wp user create new_user email@example.com --role=editor --user_pass=Password
# List users
wp user list
# Reset a password
wp user update admin --user_pass=NewPassword ```
### Maintenance
```bash
# Flush cache
wp cache flush
# Optimize database
wp db optimize
# Export database
wp db export backup.sql
# Search and replace in database
wp search-replace 'old-domain.com' 'new-domain.com' ```
## Best Practices
- Always run WP-CLI commands as the www-data user
- Use strong passwords for admin and database
- Perform regular backups with `wp db export`
- Regularly update WordPress, plugins and themes
- Monitor web server logs to detect intrusion attempts
## Troubleshooting
- Check Apache logs: `sudo tail -f /var/log/apache2/error.log`
- Check Nginx logs: `sudo tail -f /var/log/nginx/error.log`
- Verify file and folder permissions
- Ensure PHP and its extensions are properly installed: `php -m`