Managing WordPress servers

As in all my entries, I am going to create this “cheat sheet” to consult it later instead of searching through all the saved urls of documentation, books, documents, etc., and if it can also be of help to someone else, so much the better. 😉

If you administer any server with your WordPress installations (read here yours, as well as your clients’), these commands may be useful to you on occasion.

Stack on the server

As I have already said in more than one occasion, at the moment my only choice of panel for servers that I manage is GridPane, that doesn’t mean that I don’t use hosting services like SiteGround, Kinsta or Nexcess(disclaimer: referal links. I use all these services with clients).

Because of this, when using GridPane, some commands will start with gp (from GridPane) and are unique to this specific stack. Others will be more generic and applicable to any server, as well as those of wp-cli that have a similar equivalent without gp and the domain.

Server or directory space

If we want to know the available space on our server, we will run df(disk free), in most of these commands, the -h(human) option will give us an easier to read output (in Kb, Mb. Gb, etc…):

df -h
df

If we want to see the size of a specific directory with all its directories, we will use the command du(disk use) with the same parameter -h and adding the parameter -c to give us the total. But also with the added parameter –max-depth=1 so that it only shows us a directory within the selected one (or if we prefer with 0 or with 2 so that it goes down another level, etc.) and as final parameter, the directory that we want to consult, for example:

du -hc --max-depth=1 /var/www/fotodng.com/htdocs/wp-content/
du

With these commands we can easily find space problems on our server or WordPress installation by simply changing the directory and the depth level to inspect.

Search for texts in our installation

Sometimes we have to look for a certain text in our installation, either because we do not know where that string comes from, that error, etc.

In this case we will be able to search if the text is in the database or in the files (in some plugin or theme).

For the first case, we will use wp-cli with the db search option and as in GridPane we connect as root and we would have to use the –allow-root option, we will use the gp command(here the explanation why). If we want to search for example Carlos Longarela, we would execute (version with gp and the standard one):

gp wp fotodng.com db search "Carlos Longarela"

wp db search "Carlos Longarela" --allow-root
db search

The wp-cli search-replace command will not be discussed here, it will be covered in a later article in which I will talk about migrations between servers.

If we want to search the text directly on the files, we can use grep with the modifiers -r(recursive) and -n(number) to show the line number of the match and if we want it to be case insensitive we add -i(ignore-case):

grep -rni "Carlos Longarela" /var/www/fotodng.com/htdocs/wp-content/*
grep

If we want to search for files, we will use find followed by the directory to search and if we only want to search for directories -type d and -type f if we only want to search for files, for example:

find /var/www -name calendario_server*.*
find

Memory and processes

If we want to know the processes running on our server, we will use top, which allows us to see the total memory consumed, CPU, “stolen” processor in case we have “noisy neighbors” (virtual machines that steal CPU cycles) and many other options. Here is a good article on the use of top.

I also recommend the use of htop, which is very similar to the previous one and about which they have also written a fantastic article in GridPane.

htop

In addition to these two interactive commands, we can view the CPU information with lscpu or the total, free and used memory with free -h.

To view information about the disk partitions, run lsblk or if you want to get more detailed information about each partition run fdisk -l.

To get the OS name and version, run lsb_release -a.

File and directory permissions

If we want to have our WordPress configured correctly we should take a look at Hardening WordPress.

First of all, it tells us that the directories of our installation must belong to the user account and although we will find in many sites on the Internet how to configure the owner to be the user of the web server (usually www-data); this is a security issue, so you should not use the recommendation of chown www-data:www-data -R *

Instead we should do (within the WordPress directory):

chown <username>:<group>  -R *

If we are working with GridPane we already have an option for the system to configure these permissions, if for any reason we have changed them (when stepping on files from another installation).

reset permissions

If we must “manually” change the permissions of our files and directories, they should be set to 644 for files and 755 for directories.

We will change them recursively with these two commands:

find /var/www/misitio.com/htdocs/ -type f -exec chmod 644 {} \;

find /var/www/misitio.com/htdocs/ -type d -exec chmod 755 {} \;

View logs

In any debug task or health check of our WordPress installation, we will see different log files, sometimes large.

We have already seen how to search for certain text strings with grep that we can also apply to search in a single log file (or in several) looking for a certain error or event.

If we want to see the complete content of a file, we will use the cat command, if we also add the -n parameter it will number the lines, for example:

cat -n /var/www/misitio.com/wp-config.php

But there are times when the files, especially log files, are very large and we only want to see a certain number of lines.

For this task we have two commands, depending on whether we want to see the first lines(head) or the last lines(tail). Both will show the first 10 or last 10 lines of the file. If we add the -n parameter and a number, it will show the X lines at the beginning or end of the file.

head -n 25 /var/www/misitio.com/wp-config.php

Finally, with the -f parameter for the tail command, it will stay running and will show us the new lines of the file. This is very useful to monitor in real time the log files and to see the accesses to our web or the errors.

tail -n 20 -f /var/log/nginx/fotodng.com.access.log
tail logs

If we want to have a real time monitoring of several logs (accesses, errors, mysql, etc.) and with colors (configurable by log types) to quickly see the information, multitail will be very useful.

Final considerations

This has been a very brief guide of commands that I consider useful for debugging issues and managing our WordPress installations on a day-to-day basis.

I have omitted directory navigation, copying and creating files and other basic commands, I have simply put here a selection that I may expand at some point in some other entry (maybe multitail).

I have listed the options I use most often, but each command has many more options. Remember that you can see the help and all the options of each one with man, for example, man tail to see the help of the tail command.

Also remember that we can “chain” commands with the symbol |(pipe) or send the output of a command to a file > (output redirection), for example, to send the results of a grep search to a text file:

ls -l | more

grep -rni "Carlos Longarela" /var/www/fotodng.com/htdocs/wp-content/* > resultados-cl.txt

Finally, I want to mention that all these commands have been tested from a Windows machine with Windows Terminal and WSL2 installations of Ubuntu and Kali Linux, and the commands executed on own or client machines connected via SSH or Mosh.

Join my superlist ;)

I won't share your details with anyone or bombard you with emails, only when I publish a new post or when I have something interesting to share with you.

Leave a Comment