If you are running and maintaining a number of local and online installations, you should have noticed it’s a long time process. Even the famous 5 minutes WordPress install can be a long time if you are trying to investigate a subject or need a new installation to try out a new idea or materialize another conception about your production site. Thankfully, we’ve got the WordPress Command Line Interface (WP-CLI) that can help you reduce installation and maintaining times dramatically.
The idea behind WP-CLI is to use terminal commands to setup and configure WordPress installations, instead of clicking options on the browser.
The Command line interface is very popular among Linux users and used to be popular once among Microsoft users (the, hm, good-old MS-DOS times). Power users of MAC OS platform are keen of using the command line as well. For mainstream users though, the command line is a strange place and most operating systems make it look obsolete. The use of the command line interface is very popular among IT personnel, mainly those who maintain servers and databases since it provides immediate access to the filesystem of the server even when nothing else works. Command line can be a lot faster because it doesn’t request for a graphical user interface and can work on low specification machines as well, without noticeable difference. It can also be very powerful, as with a single command you can make bulk changes on the files (although this is very dangerous if you type the wrong command with the right privileges).
Speaking of security, command line is a safe passage, both for beginners and advanced users alike, since it does not allow the modification of critical system settings without the use of proper administrative rights.
Both Linux and MAC OS are based on the UNIX design (and share a lot of common features), that’s why they are considered safe environments for Web applications. Windows, on the other hand, was always aimed at desktop users and took more than twenty years to reach a mainstream level of security for hosting. Also, not all Windows editions can serve as a hosting environment, that’s why there is a special Server version of Windows.
Now, there are two ways to install a website: locally and on remote servers. In both ways, you can use WP-CLI effectively to reduce installation and maintenance times. First of all, let’s see how to install WP-CLI on your local environment. We’ll take Linux, for example, as it is the most popular among users. Keeping in mind that there are several Linux distributions available, we will use the most popular one: UBUNTU. The commands, though, will work on most UNIX like platforms (Linux, OSX). Just make sure you are using PHP version 5.3.29 or higher and WordPress 3.7 or higher. We are performing the following actions on a computer running Ubuntu 16.04, with complete the LAMP stack installed.
First, let’s download WP CLI:
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Check that it works:
$ php wp-cli.phar --info
Let’s make it executable and move it to a safe folder:
$ chmod +x wp-cli.phar $ sudo mv wp-cli.phar /usr/local/bin/wp
Check, to see if it works:
$ wp --info
At this point you should be ready to use WP-CLI. Let’s start!
You know that WordPress can’t work without a database. Let’s create a database using MYSQL
$ mysql -u user -p
This will let us enter the MySQL database’s command line interface.
mysql> create database wordpress_db;
Replace “wordpress_db” with the title you need. If the directory exists you will prompted to provide another title. After successfully creating the database, exit MYSQL command line with
mysql> exit
Now, let’s create a directory and place WordPress files in. To have our website available we should place it on
~/var/www/html/
(Note: Check whether your web server’s files reside on a different location and replace the above path.)
Let’s move there and create a new folder for our website:
$ cd /var/www/html $ sudo mkdir website
Note, we use “sudo” because we need special privileges to make changes in places out of our “home” directory. This is important for keeping our system safe. The “sudo” will prompt for a password. It is the same password you have for login to your system. At this point, we should have a new folder. Let’s move on and download WordPress with WP-CLI:
$ cd /website/ $ sudo wp core download
After a few minutes the download will have completed and all requested files will be on the folder. Since you are downloading from the official repositories of WordPress you always have the latest version.
Linux and other UNIX like systems are using special privileges for editing, modifying or simply owning the files of a directory. Special commands are used to provide access to the contents of a particular directory or else you are going to face issues while using WordPress; you might not be able to upload a picture or install a plugin. This would make the installation unusable so, let’s provide our directory with sufficient privileges with “chown” and “chmod”, two very powerful *NIX commands. Let’s move one step back to the previous folder and do the changes:
$ cd .. $ sudo chmod -R ugo+rwx website/ $ sudo chown -R www-data:www-data website/
Note that, this syntax will provide full access to your installation for any user of the system. This is extremely dangerous if you are installing on an online web server, but will make it a lot easier if you are using a local server since it will eliminate any error related to user privileges.
Now it’s time to create the wp-config.php configuration file
$ sudo wp core config --dbname=wordpress_db --dbuser=user --dbpass=password --dbhost=localhost --dbprefix=wp_ --allow-root
The last two parameters (–allow-root) will override the security alert from WP-CLI which, normally, won’t allow this command to be performed with these elevated privileges, but since we are installing on a local server, we don’t have to worry about other people accessing our website. Be sure to change the wordpress_db with the database name you’ve chosen earlier.
At this point if you open a browser and check:
/localhost/website/
you will see your new WordPress installation! That’s it! Log in to back end and start developing!
It might looks a bit complicated at first glance but if you have ready system with LAMP Stack installed, WP-CLI will make your life a lot easier and increase your production times.
Many other commands are available on WP-CLI where you can develop, maintain and test your installation. For more information visit http://wp-cli.org
Add Comment