WordPress Command Line Cheat Sheet

If you are self-hosting WordPress there are several commands you should be familiar with to help you manage your server and your WordPress environment. While some people feel that using cPanel is the easiest way to manage WordPress, having full command line access can be just as easy with some practice. Not to mention command line access gives you more direct control of your server. Without further ado, here is the WordPress Command Line Cheat Sheet.

My WordPress Environment

Before diving into commands, I’ll make a general note about my configuration. I manage self-hosted WordPress on several cloud virtual servers. I currently use Azure, AWS, and Digital Ocean. I usually run Ubuntu as my server OS. Most of my websites are hosted under Nginx, but I have a few left under Apache. On the database side I have some under vanilla MySQL, but I’ve started moving my newer servers to Percona.

Utilities You Need

There’s two common tools you need for managing your server. These are both based on SSH and they’re just a staple to have on hand:

WinSCP – Is an SCP client Windows that lets you transfer and edit files on your server.

WordPress Command Line Cheat Sheet

MySQL Database Backup

MySQLDump is a command line tool that is installed by default with MySQL. It is a simple tool that allows you to backup a full database to a .sql file. Unless you have a very large WordPress site these SQL files should be relatively small < 1MB. If it is large, you can easily pipe the file to gzip and compress it.

Restore a WordPress MySQL Database

The flipside of backing up your database is restoring it. This is a key WordPress command line tool when migrating your site from one host to another. The MySQL tool is the general admin tool for performing any action with your MySQL database at the command line. If you are not a die hard command line person, it may take some getting used to, but once you are comfortable with it you’ll be much more efficient at managing WordPress. When running the MySQL command you always specify the -p parameter for Password, and it will prompt you to enter it.

mysql -u root -p [database name] < [backupfile.sql]

mysql -u root -p wp_wireandfrost < wireandfrost.sql

Create a new WordPress Database

Another common database operation is setting up a new WordPress database. This also involves using the MySQL command, but I’m going to use it to execute multiple MySQL commands. You just specify the username and prompt for the password and you’ll enter the MySQL command tool.

mysql -u root -p

Just remember that all the MySQL commands should end with a ;

First we’ll create the database, then create the MySQL user account, grant that new user access to the new database, and flush MySQL permissions to pick up the change.

The database name, username, and password you enter here will then go into your wp-config.php file.

Create a New WordPress Website

Now this step is not entirely done at the command line, but you could do it using a text editor like Nano. I usually edit my text files in WinSCP. These are the steps for an Nginx website, I’ll update this to also include Apache. There are plenty of in depth tutorials on Nginx configuration, so I’m going to keep this to the point and just show you the steps you need for getting a new WordPress site available quickly.

Create a new file in /etc/nginx/site-available, I usually give it the same name as my website domain. Add the following text to that file:

Check Nginx Version and Syntax

Use the following command to validate your Nginx configuration files and ensure you have no errors.

sudo nginx -t

Use this command to check your Nginx version

nginx -v

Monitor Nginx Traffic

WordPress Command Line tools for monitor your websites are also key. Another useful tool is to be able to watch your HTTP traffic in real time. This can be helpful is you are trying to diagnose a problem or examine load on your server. Nginx makes this very easy out of the box because they combine all HTTP Access logging into one file. Using the tail command you can watch it in real time

tail -f /var/log/nginx/access.log

Monitor Server Processes

This is a pretty common linux admin command, but if you are new to linux you may not be familiar with it. Running topfrom command line will launch a process monitor. The top command is basically the same as the Windows task manager but in your command shell. You can see which process are using the most CPU and Memory and the overall health of your server. If you are having performance issues, this would be your first stop.

Other Options

These are some of the most commonly use WordPress command line tools, but if you want to self-host WordPress without having to do a lot of this EasyEngine is another route. As I mentioned in my review of it, EasyEngine helps you automate many of these steps. Check out that post if you’re interested.