WP-CLI is a command line tool for interacting with and managing WordPress sites. WP-CLI is very similar in functionality to what drush provides Drupal. If you are already familiar with using cli tools then this will be quick to pick up on. If not, then it may be a good time to start learning.
In this tutorial we’ll learn how to install wp-cli on a server and learn some basics. With WP-CLI you can speed up common maintenance, automate tasks, or even take backups.
Install wp-cli for All Users
Installing a tool like wp-cli on a server globally means that any user will be able to use the application. With WordPress being one of the most common CMS’ it’s helpful to have wp-cli installed for all users.
Pre-flight Check:
- Root-level command line access via SSH is required to follow this tutorial.
- PHP 5.3.29, or higher, will be required for wp-cli to function.
- WordPress 3.7, or later, is required for wp-cli support.
To start you should do a quick test to see if wp-cli is already installed on the server:wp
If you see the error below wp-cli is not installed, you can continue with the tutorial to install it on the server.
-bash: wp: command not found
- To begin the install we will use curl to download the wp-cli.phar file:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
- Then do a quick test of of download with the following:
php wp-cli.phar --info
Error: YIKES! It looks like you’re running this as root. You probably meant to run this as the user that your WordPress install exists under.If you REALLY mean to run this as root, we won’t stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS.
If you’d like to continue as root, please run this again, adding this flag: –allow-root
If you’d like to run it as the user that this site is under, you can run the following to become the respective user:
sudo -u USER -i — wp
In this case seeing the error above is a good sign, we were just verifying that the file executes correctly. - Next we will ensure that the file has the correct file permissions for execution:
chmod +x wp-cli.phar
- Finally we will move the wp-cli.phar executable to global location to ensure all users have access.
sudo mv wp-cli.phar /usr/local/bin/wp
In this step we are also renaming the file to feel more like a traditional cli tool.
To ensure you’ve done the process correct you can do one final test. You’ll still see the same error from the test above since we’re running as the root user.wp --info
Error: YIKES! It looks like you’re running this as root. You probably meant to run this as the user that your WordPress install exists under.If you REALLY mean to run this as root, we won’t stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS.
If you’d like to continue as root, please run this again, adding this flag: –allow-root
If you’d like to run it as the user that this site is under, you can run the following to become the respective user:
sudo -u USER -i — wp
Verify & Test wp-cli as a Site User
Now that wp-cli is installed globally you will want to test the tool from a user hosting a WordPress site. To do this you will login as root via SSH then:
su - wordpress
By executing this command you will then be logged in as the ‘wordpress’ user. Do take note that you’ll enter the actual username where it says ‘wordpress’.- Then you can run the following to get basic info about wp-cli:
wp --info
[wordpress@web01 public_html]$ wp –info
PHP binary: /opt/remi/php70/root/usr/bin/php
PHP version: 7.0.11
php.ini used: /etc/opt/remi/php70/php.ini
WP-CLI root dir: phar://wp-cli.phar
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config:
WP-CLI version: 0.24.1
As you can see from the above output running the wp-cli tool as a regular user does not trigger an error. You can also see that we’re running on a server with PHP 7.0.11 and wp-cli is at version 0.24.1.