{"id":58045,"date":"2020-10-30T13:36:40","date_gmt":"2020-10-30T18:36:40","guid":{"rendered":"https:\/\/blog.cpanel.com\/?p=58045"},"modified":"2020-10-30T13:36:40","modified_gmt":"2020-10-30T18:36:40","slug":"wp-cli-install-and-manage-wordpress-on-the-command-line","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/wp-cli-install-and-manage-wordpress-on-the-command-line\/","title":{"rendered":"WP CLI: Install and Manage WordPress\u00ae on the Command Line"},"content":{"rendered":"\n
WordPress is incredibly easy to use with its intuitive graphical interface but it’s not ideal for server administrators and hosting providers who manage hundreds of sites. WP-CLI (WordPress Command Line Interface) is a powerful tool that can control every aspect of WordPress from the command line.<\/p>\n\n\n\n
When you’re tasked with updating or installing a plugin, managing multiple WordPress sites with the command line and scripting is more efficient. In this article, we\u2019ll show you how to install WP-CLI and highlight some of its most useful features, including how to install, update, and manage WordPress core, plugins, and themes on the command line.<\/p>\n\n\n\n
WP-CLI is a self-contained PHP app that can be installed both by server administrators and ordinary cPanel users.<\/p>\n\n\n\n
To install and use WP-CLI, you will need access to your server\u2019s command line. Administrators with root access can log in with SSH<\/a>. cPanel users can log in with SSH if it\u2019s available or cPanel\u2019s built-in Terminal<\/em>.<\/p>\n\n\n\n First, we need to download WP-CLI to the server with:<\/p>\n\n\n\n Before we can run it, we need to provide execute permissions with:<\/p>\n\n\n\n You can learn more about chmod and file permissions in How to Assign Permissions to Files and Folders in cPanel<\/a>.<\/p>\n\n\n\n To allow every user to run WP-CLI, we have to move it to a directory in the system\u2019s PATH<\/a>.<\/p>\n\n\n\n This:<\/p>\n\n\n\n All users should be able to run WP-CLI from as \u201cwp.\u201d<\/p>\n\n\n\n cPanel users without root access can\u2019t move files into a directory in PATH, but they can run it from their home folder or in a WordPress site\u2019s directory.<\/p>\n\n\n\n To run it from your home directory, you can tell WP-CLI which WordPress site to control with the \u201c–path\u201d option. For example:<\/p>\n\n\n\n If you move WP-CLI into the WordPress site\u2019s directory, you don\u2019t have to supply a location with \u201c–path,\u201d but you will need to specify the directory that contains the executable, using \u201c.\/\u201c for the current directory.<\/p>\n\n\n\n To make it easier to use, you can create an alias<\/a>, a command-line shortcut :<\/p>\n\n\n\n The shell will replace \u201cwp\u201d with \u201c~\/wp-cli.phar\u201d, allowing you to enter \u201cwp\u201d rather than the full path to the executable.<\/p>\n\n\n\n You can make the alias permanent with:<\/p>\n\n\n\n We\u2019re adding the alias command to your account\u2019s .bashrc configuration file so that it runs whenever you log in.<\/p>\n\n\n\n WP-CLI commands are composed of a primary command followed by subcommands for controlling particular aspects of a WordPress site.<\/p>\n\n\n\n For example:<\/p>\n\n\n\n Here \u201chelp\u201d is a command, which has subcommands such as:<\/p>\n\n\n\n This prints help information for \u201ccore\u201d management features. The tool has an excellent built-in help and documentation system. If you\u2019re unsure which commands you can run or what they do, help<\/em> should be your first recourse.<\/p>\n\n\n\n There are over 40 commands and hundreds of subcommands. You can read the full list<\/a> in the documentation, but we\u2019d like to highlight a few of the most useful.<\/p>\n\n\n\n The config <\/em>command can read and write WordPress\u2019s configuration, which is stored in the wp-config.php<\/em> file.<\/p>\n\n\n\n To see the configuration variables in a site\u2019s wp-config.php file:<\/p>\n\n\n\n To edit individual configuration variables such as the database name:<\/p>\n\n\n\n To generate a new wp-config.php<\/em> file with pre-configured values:<\/p>\n\n\n\n In our recent article, Fixing WordPress Login Errors With cPanel<\/a>, we explored how to change user passwords by editing the database. Here\u2019s a faster way to replace lost and forgotten WordPress passwords.<\/p>\n\n\n\n Although this method is faster, it is not as secure because the user\u2019s plaintext password is stored in your shell history. You can delete the shell history entries by using the up arrow to select the command and pressing Ctrl-U to delete it.<\/p>\n\n\n\n One of the most useful aspects of controlling WordPress from the command line is the ability to install everything from a plugin to a full WordPress site. <\/p>\n\n\n\n Let\u2019s start with a plugin:<\/p>\n\n\n\n To install without activating, omit the \u201c–activate\u201d option. To find the correct name for a plugin, open its page in the WordPress plugin catalog<\/a> and copy the URL slug. In the example, we used the Hello Dolly plugin and copied the URL slug from its web page: https:\/\/wordpress.org\/plugins\/hello-dolly<\/strong>\/.<\/p>\n\n\n\n To install a theme:<\/p>\n\n\n\n You can also \u201cuninstall,\u201d \u201cdelete,\u201d and \u201cupdate\u201d plugins and themes. The update feature is particularly useful on sites with many plugins:<\/p>\n\n\n\n To update all of a site\u2019s plugins at the same time:<\/p>\n\n\n\n Finally, to install a new WordPress site in seconds:<\/p>\n\n\n\n Running this on the command line stores the plaintext admin password in the shell history, but you can delete it as described in the previous section.<\/p>\n\n\n\n As we explained in How to Back Up and Restore MySQL\u00ae Databases in cPanel<\/em><\/a>, it\u2019s straightforward to dump your site\u2019s MySQL database in the cPanel interface. However, if you prefer to back up from the terminal, use:<\/p>\n\n\n\n The \u201c–add-drop-table\u201d option ensures that data is correctly replaced when restoring the backup. Export<\/em> creates an SQL file with a filename based on the date and database name. To specify a different filename, add it to the end of the command:<\/p>\n\n\n\n To restore the database, import the SQL file with:<\/p>\n\n\n\n Importing is a destructive action. It will irretrievably delete any data that was added to the database after the backup was made.<\/p>\n\n\n\n Finally, you can optimize or repair the database. Optimizing reorganizes the way data is stored to speed up reading and writing:<\/p>\n\n\n\n Repair<\/em> attempts to fix damaged database tables. It is often worth trying when you suspect database corruption or in a White Screen of Death situation where the WordPress interface is not working.<\/p>\n\n\n\n WP-CLI is an essential tool for anyone who hosts and manages large numbers of WordPress sites. It can substantially reduce the time and effort needed to carry out regular maintenance tasks.<\/p>\n\n\n\n In this article, we focused on running commands manually, but all the commands discussed and many more can be used in scripts to automate complex workflows. WP-CLI can also be combined with cron to schedule WordPress management tasks. We explained how to automate web hosting tasks with cron in How to Configure a Cron Job<\/em><\/a>.<\/p>\n\n\n\n If using the WP-CLI still seems a bit daunting, cPanel has added the most used WP-CLI commands to WP Toolkit for cPanel in Version 92. Launching a site, managing plugins and themes, and more, are all just a click away. Discover all the features of WP Toolkit.<\/a><\/p>\n\n\n\n<\/figure>\n\n\n\n
curl -O https:\/\/raw.githubusercontent.com\/wp-cli\/builds\/gh-pages\/phar\/wp-cli.phar<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
chmod +x wp-cli.phar<\/code><\/pre>\n\n\n\n
Setting Up WP-CLI as the Root User<\/strong><\/h3>\n\n\n\n
mv wp-cli.phar \/usr\/local\/bin\/wp<\/code><\/pre>\n\n\n\n
Setting Up WP-CLI as a cPanel User<\/strong><\/h3>\n\n\n\n
.\/wp-cli.phar config list --path=\/home\/user\/public_html\/<\/code><\/pre>\n\n\n\n
\/home\/user\/public_html\/wp-cli.phar config list<\/code><\/pre>\n\n\n\n
alias wp='~\/wp-cli.phar\u2019<\/code><\/pre>\n\n\n\n
echo \"alias wp='~\/wp-cli.phar'\" >> .bashrc<\/code><\/pre>\n\n\n\n
How WP-CLI Commands Work<\/strong><\/h2>\n\n\n\n
wp help <\/code><\/pre>\n\n\n\n
wp help core<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
4 Incredibly Useful WP-CLI Commands<\/strong><\/h2>\n\n\n\n
Reading and Writing WordPress\u2019s Configuration<\/strong><\/h3>\n\n\n\n
wp config list<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
wp config set DB_NAME new_name<\/code><\/pre>\n\n\n\n
wp config create --dbname=user2_wp --dbuser=user2_wp --dbpass=new_password<\/code><\/pre>\n\n\n\n
Changing WordPress User Passwords<\/strong><\/h3>\n\n\n\n
wp user update USERNAME --user_pass=\"new_password\"<\/code><\/pre>\n\n\n\n
Installing WordPress Core, Themes, and Plugins<\/strong><\/h3>\n\n\n\n
wp plugin install hello-dolly --activate<\/code><\/pre>\n\n\n\n
wp theme install twentytwenty --activate<\/code><\/pre>\n\n\n\n
wp plugin update --all<\/code><\/pre>\n\n\n\n
wp core install --url=example.com --title=\"A New Site\" --admin_user=frank --admin_password=astrongpassword --admin_email=frank@example.com<\/code><\/pre>\n\n\n\n
Back-Up and Optimize The WordPress Database<\/strong><\/h3>\n\n\n\n
wp db export --add-drop-table<\/code><\/pre>\n\n\n\n
wp db export --add-drop-table database-backup.sql<\/code><\/pre>\n\n\n\n
wp db import database-backup.sql<\/code><\/pre>\n\n\n\n
wp db optimize <\/code><\/pre>\n\n\n\n
wp db repair<\/code><\/pre>\n\n\n\n
Fast and Efficient WordPress Multisite Management with WP-CLI<\/strong><\/h2>\n\n\n\n