How to install and work with WP-CLI

In today’s article we will take a look at WP-CLI. WP-CLI is a set of command-line tools for managing WordPress installations. This tool allows you to set up a new WordPress installation and upgrade the existing ones, install, activate and upgrade plugins, set up WordPress multi-site and much more, using the command-line.

For this article we will use Debian Wheezy Cloud VPS with PHP 5.4.4, MySQL 5.5 and Nginx 1.2 installed on it.

Log in to your Cloud VPS as user root and execute the following command to make sure that all services and other OS packages are up to date:

apt-get update && apt-get -y upgrade

If you don’t have php, mysql, nginx and curl installed on your machine, run the following commands to install them:

apt-get install php5-cli php5-fpm curl mysql-server mysql-client nginx-full

Installing WP-CLI is really fast and easy, just run the following command:

curl https://raw.github.com/wp-cli/wp-cli.github.com/master/installer.sh | bash

Next, add the following lines in your .bashrc file:

export PATH=$HOME/.wp-cli/bin:$PATH
source $HOME/.wp-cli/vendor/wp-cli/wp-cli/utils/wp-completion.bash

To start working with WP-CLI you can either run the following command or logout and login again.

source ~/.bashrc

Before starting to use the WP-CLI we will create a new MySQL database and new Nginx server block (virtual host).

To create a new database run the following commands:

mysql -uroot -p
CREATE DATABASE database_name;
GRANT ALL PRIVILEGES ON database_name.* TO 'database_user'@'localhost' IDENTIFIED BY 'database_user_password';
FLUSH PRIVILEGES;
\q

Do not forget to change ‘database_user_password’ and use a strong password for your database.
To create a root directory for your web site, simply run:

mkdir -p /var/www/yourwebsite.com/{public_html,logs}

Create a new Nginx configuration file for your website:

vi /etc/nginx/sites-available/yourwebsite.com

Add a new server block with the following content:

server {
  server_name yourwebsite.com;
  listen 80;
  root /var/www/yourwebsite.com/public_html;
  access_log /var/www/yourwebsite.com/logs/access.log;
  error_log /var/www/yourwebsite.com/logs/error.log;
  index index.html index.php;
  location / {
    try_files $uri $uri/ @rewrites;
  }
  location @rewrites {
    #rewrite ^ /index.php last;
    rewrite ^/(.*)$ /index.php?url=$1 last;
  }
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }
  location ~ /\.ht {
    deny  all;
  }
  location ~ \.php {
    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

Reload the nginx service for the changes to take effect:

service nginx reload

To demonstrate how WP-CLI works, let’s install WordPress and a few plugins.

Navigate to the root directory:

cd /var/www/yourwebsite.com/public_html

Download the latest version of WordPress to the server:

wp core download

Create a new configuration file:

wp core config --dbname=database_name --dbuser=database_user --dbpass=database_user_password

Populate the database:

wp core install --url=yourwebsite.com --title=your_web_site_title --admin_user=your_admin_user --admin_password=your_admin_user_password [email protected]

Now you should be able to access your new WordPress installation using your favorite web browser.

To install and activate a plugin (e.g. Contact Form 7) just run:

wp plugin install contact-form-7
wp plugin activate contact-form-7

You can view all installed plugins using the following command:

wp plugin list

For documentation and more detailed information and examples, you can check out wp-cli.org

Of course you don’t have to do any of this if you use one of our Linux Cloud VPS Hosting services, in which case you can simply ask our expert Linux admins to install WP-CLI for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Leave a Comment