Skip to main content

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:

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

  1. Download the WP-CLI phar file:

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

    Or with wget:

    wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  2. Make the file executable:

    chmod +x wp-cli.phar
  3. Move the executable for global use:

    sudo mv wp-cli.phar /usr/local/bin/wp
  4. Verify the installation:

    wp --info

    You should see information about PHP and WP-CLI versions.

Database Configuration

Create a MySQL/MariaDB database for WordPress:

  1. Connect to MariaDB/MySQL:

    sudo mysql -u root -p
  2. 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;
    Warning

    Replace wordpress_db, wordpress_user and secure_password with your own values.

WordPress Installation

  1. Ensure the folder belongs to the web server user:

    sudo chown -R www-data:www-data /var/www/your_domain
  2. Switch to the www-data user:

    sudo -u www-data bash
  3. Navigate to the web directory:

    cd /var/www/your_domain
  4. Download WordPress:

    wp core download --locale=en_US
  5. Generate the wp-config.php file:

    wp config create \
    --dbname=wordpress_db \
    --dbuser=wordpress_user \
    --dbpass='secure_password' \
    --dbhost=localhost \
    --dbprefix=wp_ \
  6. 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

  1. 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
  2. Secure the wp-config.php file:

    sudo chmod 600 /var/www/your_domain/wp-config.php

Useful WP-CLI Commands

Note

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`