Install WordPress With WP-CLI

WordPressHere is how to get a basic WordPress site up and running using WP-CLI. This is particularly helpful for spinning up local WordPress installations for development of plugins and themes. You can also use WP-CLI to get started with unit testing your WordPress plugins with PHPUnit. For now, let’s see about getting WordPress installed.

 

Setup Default Configuration File

I primarily use WP CLI for local development of plugins and themes and have some standard configuration options that I put in my `~/.wp-cli/config.yml` file. This saves some typing. Here is an example of my `config.yml` file:

core install:
  admin_user: devuser
  admin_password: devpass
  admin_email: dev@email.com
  title: WordPress Development
core config:
  dbuser: my-db-username
  dbpass: my-db-password
  dbhost: localhost
  extra-php: |
        define( 'WP_DEBUG', true );
        define( 'WP_POST_REVISIONS', 0 );

Formatting The config.yml File

The format of the configuration file mirrors the WP CLI commands. For example, if you are setting values for the command `wp core install` then your heading will be `core install:` and below that you provide values for the install command. Likewise, to provide values to use with the `wp core config` command, your heading will be `core config:` and then you list the values used with the config command.

You can store a configuration file like this in several locations depending on how you want to use it. I use these same settings for many of my development sites, so I keep it in `~/.wp-cli/config.yml` directory which is the `.wp-cli` directory in the home directory on my development laptop.

Download WordPress

First, let’s download WordPress to the document root of our web site. We will do the following three things:

  1. Make a directory for our new WordPress website called `wordpress`
  2. Change into the new `wordpress` directory we created
  3. Use WP-CLI to download WordPress
mkdir ~/sites/wordpress
cd ~/sites/wordpress
wp core download

Configure WordPress Installation

Next we need to set the values for the `wp-config.php` file like the database connection information. The database name you specify does not have to exist yet. We’ll use WP CLI to set that up in a moment. We only need to provide a database name an not any of the other database connection information like the username, password, and host because that’s coming from your `~/.wp-cli/config.yml` file. If you do not have those settings in that file, you would need to provide them on the command line.

wp core config --dbname=my-database-name

(NOTE: Remember to change `my-database-name` to the actual name of the database you’d like to use.)

Create The Database For Your Site

WP CLI has a command that will look in the `wp-config.php` file (that we created earlier) and use the database connection information to create the database for your WordPress site. In order for this to work, the database user that you specified needs to have permission to create databases on your system. In my local development environment, I have a single database user that has access to all my database and also has permission to create new schemas. A live / production setup would be more secure than this and would have it’s own database user with limited permissions. This is very convenient for development purposes, and WP CLI can take care of it all in one quick command:

wp db create

You don’t need to provide any of the database credentials because they are already in the `wp-config.php` file.

Create The WordPress Database Tables

Now we’re ready to create the WordPress database tables for our site. Again, many of the required parameters are getting pulled from the `~/.wp-cli/config.yml` file. So the only parameter we have to provide is the URL for where the site will live.

wp core install --url=host.domain.dev

The URL you provide can be any local URL, it doesn’t have to be a live URL that is publicly accessible.

Summary

We have used WP CLI to download, configure, and install a WordPress site. Keeping in mind that some of the values for the commands we issue come from your `~/.wp-cli/config.yml` file, here are all the commands you need to get up and running with a fresh WordPress site installed with WP CLI.

mkdir ~/sites/wordpress
cd ~/sites/wordpress
wp core download
wp core config --dbname=wordpress
wp db create
wp core install --url=host.domain.dev

As you can see, once you have WP CLI setup up, that’s a quick easy way to get WordPress installed and hopefully it will save you a lot of time.

If you are interested in more details about this, check out:

Leave a Reply

Your email address will not be published.