WordPress is the most widely used open-source content management system (CMS) based on PHP and MYSQL. WordPress is installed on more than 60 million websites. There are plenty of ready-to-use themes and plugins available for WordPress. You can use WordPress as your blogging platform, as an e-commerce site (with WooCommerce) or as a CMS system for your company website. Matt Mullenweg and Mike Little released the first WordPress version on May 27, 2003, under a GPLv2 license.
WP-CLI is a command line tool that provides a command line interface to manage your WordPress site. From zero to Hero, you can use it to create your WordPress site, manage the WP database, alter the site description, install and manage themes and plugins etc. This is very cool stuff that can save you a lot of time. You can update plugins, setup multisite installations and much more without having to log in to WordPress in a web browser. You can do it all from the shell terminal/SSH.
Prerequisites
What is covered in this tutorial?
Installation and Configuration of a LEMP Server (Linux, Nginx, MySQL, PHP)
In this tutorial, I will use Nginx as web server, MySQL as the database system and PHP-FPM to run WordPress. Nginx is one of the fastest web servers, it provides a high performance with low memory usage.
So let’s get started, first install all packages with one “apt-get” command:
NOTE: You will be prompted for the MySQL password. Please enter a secure password for the MySQL root user and remember that password. We need it again later
Now edit the php.ini file in the directory “/etc/php/8.1/fpm/”.
Please uncomment on line 773 “cgi.fix_pathinfo” and change the value to 0:
Save and exit.
Now configure Nginx to use PHP-FPM. Please go to the directory “/etc/nginx/sites-available/” and create a new file for the virtual host configuration.
Paste the configuration below:
Replace “wpcli.co” in the server_name setting with the domain name that your WordPress installation shall use.
Now activate the virtualhost by creating a symlink to the file wpcli.co in the directory “/etc/nginx/sites-enabled/” :
Then create a new directory with the name “/home/vagrant/wordpress/” and create a new file to test the PHP installation with phpinfo(). I will use a user named “vagrant” here, so “cd ~” is the same than “cd /home/vagrant”:
Restart the nginx web server and php-fpm:
Test the PHP and Nginx installation by visiting the site “http://wpcli.co/” in a browser. Replace wpcli.co with the domain name of your website/server.
Install WP-CLI
To install wp-cli, you have to download the .phar file (.phar is PHP archive format similar to .jar for java) from GitHub with the curl or wget command, I will use curl here:
Make the wp-cli.phar file executable:
And move it to the bin directory, the command renames wp-cli.phar to “wp” for easier usage:
Now test the wp-cli with command:
You can see here that I’m using PHP 8.1
Basic WP-CLI Commands
Until this step we have:
We will install WordPress with the domain “wpcli.co” in the directory “/home/vagrant/wordpress/“. We will do this with WP-CLI.
1. Install WordPress and Configure the Database
Please go to the web directory:
Download WordPress by using wp-cli, you can download a different WordPress version, I will use the version 6.1.1 here:
If you want to use a different version, change the version number.
Next, Create the database for WordPress through the MySQL shell. Please connect to the MySQL server with the following command:
Once you have been logged into MySQL, create a new user and database:
I’ve created a new database with the name “wordpressdb”, a new user “iamuser”, and I’ve granted access to the user to the database.
The database has been created, now I’ll create a “wp-config.php” file with the database credentials of the MySQL user that we added above.
When you see this line, then the file has been created successfully.
The options explained:
Now it’s time to install WordPress with the command “wp core install“, the command requires these parameters:
Let’s do it:
The command results in:
Now visit your domain name: wpcli.co with the web browser again.
(If you want to test the admin, please go to wpcli.co/wp-login.php instead).
NOTE:
If you get an error message like “sh: 1: /usr/sbin/sendmail: not found”, please install “sendmail” with this apt command:
2. Managing WordPress Themes with WP-CLI
Search for available WordPress themes:
Install a WordPress theme, in this case, the theme with the name “MyKnowledgeBase”:
Once the theme is installed, activate it:
Gest a list of all installed themes:
3. Manage WordPress Plugins with WP-CLI
Search for plugins:
Install plugins:
Activate a plugins:
See all installed plugins:
Get the status of the plugin, Active or Inactive:
4. Manage the WordPress Database
Connect to the MySQL shell by using the WordPress credentials:
Show all tables in the WordPress database:
Export the WordPress database to a .sql file. This is very useful for backups:
Import a WordPress database:
Execute a MySQL Query under the WordPress MySQL user:
5. Other Commands
WP-CLI has many more commands and options. You can use “wp –help” to see all commands. There are commands to manage WordPress users, user roles, posts, the menu, widgets etc.
This content was originally published here.