Installing WordPress using WP-CLI

/
57

In this post, we install and configure WordPress for local development using WP-CLI, and set up NGiNX with php-fpm.

Set up the MySQL database and user

Note Your steps may vary if you have configured things differently. Note the underscores instead of dashes. I've omitted the MySQL responses for clarity.'

$ mysql -u root -p
Enter password:

mysql> create database wpgraphql_nuxt_apollo_with_jwt;
mysql> create user 'tutorialuser'@'localhost' identified by 'tutorialuser123';
mysql> grant all on wpgraphql_nuxt_apollo_with_jwt.* to 'tutorialuser'@'localhost';
mysql> flush privileges;
mysql> exit;

Configure WordPress using WP-CLI

We're going to use the wp command from WP-CLI, the WordPress command-line interface.

The core download command downloads WordPress to the current directory, and core install runs throught the web install process, except on the command line.

$ cd wpgraphql-nuxt-apollo-with-jwt/wordpress
$ wp core download
Downloading WordPress 6.1.1 (en_US)...
md5 hash verified: 38a0a4f7b7ec8429b2fe96de6b5d6572
Success: WordPress downloaded.
$ wp core install

Initialize the config file; Note: we use --prompt=dbpass so we aren't putting the password in the command

$ wp config create --dbname="wpgraphql_nuxt_apollo_with_jwt" --dbuser="tutorialuser" --prompt=dbpass
1/12 [--dbpass=<dbpass>]: tutorialuser123
wp config create --dbname='wpgraphql_nuxt_apollo_with_jwt' --dbuser='tutorialuser' --dbpass='tutorialuser123' --dbhost='localhost' --dbprefix='wp_' --dbcharset='utf8' --dbcollate=''
Success: Generated 'wp-config.php' file.

Add some additional configuration for local development (without SSL)

$ wp config set FORCE_SSL_ADMIN false --raw
$ wp config set FORCE_SSL_LOGIN false --raw
$ wp config set FS_METHOD direct

Now we set up the WordPress installation (this is like using the web setup, just through the command line), again using --prompt, this time for all parameters.

$ wp core install --prompt

1/7 --url=<url>: http://tutorialwp.l0cal
2/7 --title=<site-title>: Tutorial
3/7 --admin_user=<username>: tutorialuser
4/7 [--admin_password=<password>]: tutorialuser456
5/7 --admin_email=<email>: l422y@l422y.com
6/7 [--locale=<locale>]: en_US
7/7 [--skip-email] (Y/n):
wp core install --url='http://tutorialwp.l0cal' --title='Tutorial' --admin_user='tutorialuser' --admin_password='tutorialuser456' --admin_email='l422y@l422y.com' --locale='en_US'
Success: WordPress installed successfully.

Additional system configuration (hostname, php-fpm, NGiNX)

I've added the entry for tutorialwp.l0cal in my /etc/hosts, this will be the domain that my WordPress install resides at.

127.0.0.1   tutorialwp.l0cal

Note Make sure you've got php/php-fpm installed and running, I use homebrew.

I have NGiNX installed locally using homebrew, so I've added the following configuration:

server {
    listen         80;
    server_name    tutorialwp.l0cal;
    root           /Users/larry/Development/edu/wpgraphql-nuxt-apollo-with-jwt/wordpress;
    index          index.php;

    location / {
                try_files $uri $uri/ /index.php?$args;
    }

     location ~* \.php$ {
            fastcgi_pass    127.0.0.1:9000;
            include         fastcgi_params;
            fastcgi_index   index.php;

            fastcgi_buffers         16 16k;
            fastcgi_buffer_size     32k;
            fastcgi_read_timeout    180;

            fastcgi_param SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param SCRIPT_NAME        $fastcgi_script_name;

            fastcgi_pass_header  Authorization;
  }
}

Now we restart NGiNX (brew services restart nginx) and visit the new WordPress installation @ http://tutorialwp.l0cal/

You should be greeted with a barebones WordPress installation with the newest theme already up and running.

I've run through the WordPress install process probably over a hundred times, and this is my preferred choice, simply because it is simple and efficient. The WP-CLI tool is a life changer, and has a lot of capabilities past the installation process. I encourage anyone who works with WordPress often to check it out, it even has extensions, including one that will help you troubleshoot and even one to generate random post types, post counts, taxonomies, terms, term counts and featured images.

Time is money, friend!


TOY MODE
π