Install WP-CLI with LAMP on CentOS and Ubuntu

By Goutham

December 23, 2016

WordPress, WP-CLI

Introduction

In this world of Internet, any person who is into website building and CMS(content management system) are definitely familiar with WordPress. But most of them recognize only its graphical interface, if you are one among them you have came to the right place. This article will help you install WP-CLI (command line interface) in Ubuntu and CentOS, which helps you control your site from the command line. WP-CLI gives you more control when you are managing multiple sites.

Pre-flight check

Assuming you are working on a brand new server or VPS without any previous installation. WordPress CLI needs PHP, Apache, MYSQL to run on a Linux based server (LAMP).

First, we will install Apache to create the server as HTTPD server. The below command helps you install Apache.

yum install httpd

Now we need to restart httpd service using the below command.

service httpd start
Let's see how to install MYSQL

Below command will install MySQL service on the server.

yum install mysql-server
service mysqld start
Installing PHP which support MySQL
yum install php php-mysql

Now we will create a MySQL database and database username for WordPress.

Step 1 - Creating Password for MySQL root

First, you need to create a password for the MySQL root

/usr/bin/mysqladmin -u root password ‘new-password’

Now login to MySQL through root and enter the password which we created just now

mysql -u root -p
Enter password:
Enter root password
Step 2 -  Creating the Database
mysql> create database db_name;
Step 3 - Verifying the Database

If you check with below command, it displays you the Databases which were created.

mysql> show databases;
Step 4 - Create the User
mysql> create user db_user;
Step 5 - Assigning privileges to the user
mysql> grant all on db_name.* to ‘db_user’@’localhost’ identified by ‘db_password’;

Finally, we need to quit from MySQL database server.

mysql> exit

The above step completes creating a Database and assigning an user to it.

WordPress Download

Let’s proceed with downloading WordPress

We will download it in /var/www/html
cd / var / www / html
wget http://wordpress.org/latest.tar.gz
tar –xzvf latest.tar.gz
cd wordpress
etc. * / var / www / html
cd ..
mv wp-config-sample.php wp-config.php
service httpd restart

Now edit the wp-config.php file with database name, username and password which we entered while installing MySQL

There wp-config.php

// ** MySQL settings – You can get this info from your web host ** //
/** The name of the database for WordPress */
define(‘DB_NAME’, ‘wordpress’);

/** MySQL database username */
define(‘DB_USER’, ‘username’);

/** MySQL database password */
define(‘DB_PASSWORD’, ‘password’);

/** MySQL hostname */
define(‘DB_HOST’, ‘localhost’);

If everything is successfully done you can access your WordPress setup page by typing your localhost IP or Domain in your browser.

In this page, you can enter the site title and assign WordPress Admin username and password.

When you install WordPress you will be directed to your WordPress admin panel. But this is not my intention to write this article. We will install WP-CLI on the server now.

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

The following command will show you how  wp-cli install's.

php wp-cli.far –info

To use WP-CLI from the command line by typing wp, make the file executable and move it to somewhere in your PAT. You can check it from below.

chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp

We successfully installed WP-CLI. Now, go to var/www/html, this is the path where we installed our WordPress.

cd / var / www / html /
Checking WordPress version
wp core version

Let's see how to update wordpress with WP-CLI command.

wp core update
WORDPRESS CLI commands

To install and uninstall theme

wp theme install (themename)
wp theme uninstall (themename)

Commands to Activate/Deactivate themes and plugins.

wp (theme or plugin) (activate or deactivate) (theme or plugin)name

To update wp core you need to take a backup of your database and files.

Commands to take Database backup

wp db export

A "tar" should be created to take backup of all WordPress files.

tar –cvf filename filename.tar.gz

To make you familiar with WP-CLI, here is some interpretation of the command line interface.

Know the status of WP plugin
wp plugin status
Installed plugins:
UI akismet
I hello
UA jetpack
A limit-login-attempts
A SGCachePress

I = Inactive, A = Active, M = Must Use, U = Update Available
Here are the few WP-CLI commands which can useful to you.

  • wp core [download|config|install|install_network|version|update|update_db]
  • wp db [create|drop|optimize|repair|connect|cli|query|export|import]
  • wp eval-file
  • wp eval
  • wp export [validate_arguments]
  • wp generate [posts|users]
  • wp home
  • wp option [add|update|delete|get]
  • wp plugin [activate|deactivate|toggle|path|update|uninstall|delete|status|install]
  • wp post-meta [get|delete|add|update]
  • wp post [create|update|delete]
  • wp theme [activate|path|delete|status|install|update]
  • wp transient [get|set|delete|type]
  • wp user-meta [get|delete|add|update]
  • wp user [list|delete|create|update]
Conclusion

By the time you complete reading this article you should be ​ready to install WP-CLI and work with it in no time. Good Luck!!

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Create a website in 3 simple steps

Choose a website template, add features, then customise! - Free Online Website builder.