WordPress Update Using Linux CLI

If you are hosting a WordPress installation on your Linux server, than you may find it useful to automate the update using a bash script. While WordPress is capable of doing a web based update, we strongly believe in the principle of least privilege. Using this principle ensures tight security, however this comes at the expense of flexibility in terms of updates. Without further ado, below is our bash update script to update WordPress from the command line.

#!/bin/bash
#
# Update WordPress website.  
#
#
if [ -n "$1" ];then
	cd /var/www
	# Backup website 
	cp -r $1 $1.bkp
	# Download and prepare latest
	wget https://wordpress.org/latest.zip
	unzip -o latest.zip 
	rm -r wordpress/*.txt wordpress/*.html 
	cp -avr wordpress/* $1
	# Set website owners
	chown -R root:root /var/www/$1
	chown -R www-data:root /var/www/$1/wp-content/uploads
	# Set file permissions
	chmod -R 0644 /var/www/$1 
	# Set directory permissions
	find /var/www/$1/ -type d -exec chmod 755 {} \;
	# Cleanup downloads
	rm -r latest.zip wordpress
else
    echo "Usage: ./wp-update-website mywebsitefolder"
fi
Note: the script above is named wp-update-website and is setup to be run by passing the directory name of your WordPress website folder. It is important to understand that the script will need to be modified to fit your particular environment.
Did you find this article useful? Why not share it with your friends?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.