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:

  1. Putty – Is an SSH client for Windows.
  2. 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.

mysqldump –opt -u [user name] -p[password] [database name] > [backupfile.sql]

mysqldump --opt -u root -pmypassword wp_wireandfrost > wireandfrost.sql

mysqldump --opt -u root -pmypassword wp_wireandfrost > wireandfrost.sql | gzip -9 > wireandfrost.sql.gz

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

Wordpress Command LineAnother 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.

create database wp_wireandfrost;

create user 'wpuser'@'localhost' IDENTIFIED BY 'wp_password';

grant all privileges on wp_wireandfrost.* to wpuser@localhost;

flush privileges;

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:

server { 
listen 80; 

root /var/www/wireandfrost; 
index index.php index.html index.htm; 

server_name www.wireandfrost.com wireandfrost.com; 

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

location ~ \.php$ { 
try_files $uri =404; 
fastcgi_split_path_info ^(.+\.php)(/.+)$; 
fastcgi_pass unix:/var/run/php5-fpm.sock; 
fastcgi_index index.php; 
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
include fastcgi_params; 
} 
}

The 2 lines you need to customize are root and server_name. They need to match the location for your WordPress files, and your domain name.

After saving the file, run the following two commands to enable the site and restart Nginx.

sudo ln -s /etc/nginx/sites-available/wireandfrost /etc/nginx/sites-enabled/wireandfrost

sudo service nginx restart

Nginx will now be handling web requests for your domain name.

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 LineWordPress 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

Wordpress Command LineThis is a pretty common linux admin command, but if you are new to linux you may not be familiar with it. Running top from 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.

Let’s Encrypt

Create new Let’s Encrypt Certificates

sudo /opt/letsencrypt/letsencrypt-auto certonly --standalone -d icespotter.com -d www.icespotter.com

 

I’m an avid Technologist, while I do work on architecting solutions that span from the desktop to the data center, my passion is Web Infrastructure.
We will be happy to hear your thoughts

      Leave a reply