In the previous article about WP-CLI we installed and configured the utility. We will now start using WP-CLI in earnest by installing WordPress.
It is worth reading this article, even if you already have WordPress installed. We will go over a few useful WP-CLI commands and provide some tips and tricks along the way.
The WordPress install process involves four steps:
WP-CLI can take care of three of the four steps. The one thing you need to do manually is creating the database. So, for this article we will assume that you have created a database and database user and that the user has the necessary permissions.
The first step is to download and extract the core WordPress files. Before you do so, please always make sure that the directory from which you are executing the wp core download command doesn’t contain website files:
[example@cpweb6-premium public_html]$ ls -1FA cgi-bin/ .well-known/
Here, we got an almost empty directory, so there won’t be any file clashes.
As an aside, the
wp core download command doesn’t overwrite files, unless you use the
--force option. It can be useful to overwrite files, for instance when some of the core files have been corrupted. For a fresh install, though, it is best to make sure that the directory doesn’t contain website files.
wp core download command has a few other options. A commonly used option is
--locale, which specifies the language version you want to download. By default, you will get the American English version. As we are on the other side of the pond we will instead grab the British English version:
[example@cpweb6-premium public_html]$ wp core download --locale=en_GB Downloading WordPress 5.3.2 (en_GB)... md5 hash verified: 216fb56bf62185027930728caee31335 Success: WordPress downloaded.
Another interesting option is
--skip-content. The option will install everything apart from the default themes and plugins. In other words, the option saves you a bit of time if you routinely purge the Hello Dolly and Akismet plugins and the default WordPress themes.
As you can see, the
wp core download command automatically extracts the WordPress archive:
[example@cpweb6-premium public_html]$ ls -1FA --group-directories-first cgi-bin/ .well-known/ wp-admin/ wp-content/ wp-includes/ index.php info.php license.txt readme.html wp-activate.php wp-blog-header.php ...
It is worth noting that the default install doesn’t include a .htaccess file. That means that, out of the box, WordPress doesn’t support pretty URLs. For instance, the sample post that is included in a new install will have a URL like example.com/?p=1 instead of example.com/YYYY/MM/title-of-post. The .htaccess file will be created when you change the Permalink Settings via the WordPress dashboard (under Settings).
There is, of course, also the option to change the permalink structure via WP-CLI. However, the wp rewrite structure command is one a few commands that is relatively complicated and not at all intuitive. For some things the WordPress dashboard is simply easier.
Next, you need to create the wp-config.php file. You can do that manually, by copying the wp-config-sample.php file to wp-config.php and then defining the database settings using your favourite text editor. However, it is quicker to use the config create command.
Before we create the config file we should briefly touch on the
--prompt subcommand. You can use this to avoid entering passwords on the command line. Instead, you can put the password in a plain text file and then use input redirection to read the password into your command.
To illustrate, you can define the database password like so:
However, the password will be visible in your Bash history. To instead read the password from a file you can use this syntax:
--prompt=dbpass < /path/to/password_file
In the below example we are in the /home/example/public_html directory. Our database password is stored in the file /home/example/dbpw.txt. The reason we put the file below the public_html directory is that the file won't be accessible through the browser - you don't want people to accidentally stumble upon your database password.
[example@cpweb6-premium public_html]$ wp config create \ --dbname=example_wpdb20 \ --dbuser=example_usr \ --prompt=dbpass < ../dbpw.txt Success: Generated 'wp-config.php' file.
We now got a database, the core WordPress files and a wp-config.php file. It is time to run the install script, and for that we will use the
wp core install command.
core install command has six options, all of which are relevant. The first five options define basic settings and let you create a user account:
--urldefines the domain.
--titleis the title.
--admin_userdefines the WordPress admin's username (both the display and login name)
--admin_emailis the admin user's email address.
--admin_passwordis the admin user's password.
In addition, you can use the
--skip-email option to prevent an email is sent to the admin user. The below command uses all six options:
[example@cpweb6-premium public_html]$ wp core install \ > --url=example.com \ > --title='Examples Rock!' \ > --admin_user=Catalyst2 \ > --firstname.lastname@example.org \ > --skip-email \ > --prompt=admin_password < ../pw.txt Success: WordPress installed successfully.
There are a few things worth noting in the above command:
wp config createcommand a minute ago.
You should now be able to log into the WordPress dashboard, which is the best way to verify that everything is working as expected. That said, we might as well check a few things while we are on the command line. First, let's check what version of WordPress we have installed and whether or not there are any updates:
[example@cpweb6-premium public_html]$ wp core version 5.3.2 $ wp core update Success: WordPress is up to date.
Next, we can check the website URL (which we defined as example.com):
[example@cpweb6-premium public_html]$ wp option get siteurl http://example.com [example@cpweb6-premium public_html]$ wp option get home http://example.com
And finally, let's have a quick peak at the users table as well:
[example@cpweb6-premium public_html]$ wp user list +----+------------+--------------+-----------------------+---------------------+---------------+ | ID | user_login | display_name | user_email | user_registered | roles | +----+------------+--------------+-----------------------+---------------------+---------------+ | 1 | Catalyst2 | Catalyst2 | email@example.com | 2020-03-03 12:43:39 | administrator | +----+------------+--------------+-----------------------+---------------------+---------------+