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.
wp
If you see the error below wp-cli is not installed, you can continue with the tutorial to install it on the server.
[root@host ~]# wp –info
-bash: wp: command not found
If you see actual command output, as shown below, then wp-cli is already installed.
- To begin the installation, 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 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 — wpIn 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.
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
Again, seeing the error above is a good sign, we were just verifying that the file executes correct.
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 log in 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