All HowTo's Web Servers

Upgrade WordPress Fast from the CLI (quick and dirty)

This article describes the fastest way I know to upgrade a WordPress site. A little background: I recently had a WordPress site that was error’ing when attempting to administer it. There were missing functions, etc. I decided to simply upgrade the WordPress code-base to the latest and solve the problem that way. It worked in this situation.

This is a quick and dirty method. But sometimes that’s fine.

We’re doing this on a Redhat/CentOS 7 server with Apache running.

First, here’s my directory structure. I use sym-links to tell Apache which WordPress directory to use. This makes it easy to “roll back” if need be. As you can see, there’s a very simple structure of “/var/www/html” and then the domain name “www.example.com” and then the wordpress directory which is also the Apache “DocumentRoot”.

[root@server /var/www/html/www.example.com]# ls -l
lrwxrwxrwx. 1 apache apache        9 Jun 29  2021 wordpress -> wordpress-20220101
drwxr-xr-x. 5 apache apache     4096 Jan  1 13:05 wordpress-20220101

Backup the DB.

mysqldump myDatabase > ~/wordpress-20220101.sql

Download the latest version of WordPress:

cd /var/www/html/www.example.com
wget www.wordpress.org/latest.zip

Now my directory layout looks like this:

[root@server /var/www/html/www.example.com]# ls -l
lrwxrwxrwx. 1 apache apache        9 Jun 29  2021 wordpress -> wordpress-20220101
drwxr-xr-x. 5 apache apache     4096 Jan  1 13:05 wordpress-20220101
-rw-r--r--. 1 root   root   22772803 Aug 31 03:11 latest.zip

Unzip the latest WordPress archive and then rename it. The default directory will be “wordpress” so we need to change that to match our naming convention of “wordpress-yyyymmdd”:

unzip latest.zip
mv wordpress wordpress-20221005

Now my directory layout looks like this:

[root@server /var/www/html/www.example.com]# ls -l
lrwxrwxrwx. 1 apache apache        9 Jun 29  2021 wordpress -> wordpress-20220101
drwxr-xr-x. 5 apache apache     4096 Jan  1 13:05 wordpress-20220101
drwxr-xr-x. 6 apache apache     4096 Oct  5 09:52 wordpress-20221005
-rw-r--r--. 1 root   root   22772803 Aug 31 03:11 latest.zip

Remove the “latest.zip” archive:

rm latest.zip

Now we can start copying over our themes, plugins and the wp-config.php file:

rsync -avz wordpress.20220101/wp-content/themes/* wordpress-20221005/wp-content/themes/
rsync -avz wordpress.20220101/wp-content/plugins/* wordpress-20221005/wp-content/plugins/
rsync -avz wordpress.20220101/wp-config.php wordpress-20221005/
restorecon -rv ./wordpress-20221005
chown apache.apache -R ./wordpress-20221005/

Now we need to remove the sym-link that points to the old WordPress directory, and then relink to the new WordPress directory. There’s a small outage during this process.

rm wordpress
ln -s wordpress wordpress-20221005

Now my directory layout looks like this:

[root@server /var/www/html/www.example.com]# ls -l
lrwxrwxrwx. 1 apache apache        9 Jun 29  2021 wordpress -> wordpress-20221005
drwxr-xr-x. 5 apache apache     4096 Jan  1 13:05 wordpress-20220101
drwxr-xr-x. 6 apache apache     4096 Oct  5 09:52 wordpress-20221005

Now we can browse to the website and expect to see either the website working, or a prompt to upgrade the database.

If all goes bad, we have the old DocumentRoot/Wordpress code-base set aside, and we have the database dump. We should be safe.

Leave a Reply

Your email address will not be published. Required fields are marked *