Jump to content
Robert

Managing Multiple Wordpress Sites

Recommended Posts

Intro

One of the biggest hurdles with WordPress for many site owners is to try and maintain it, keep it updated, etc. But there are a few efficient ways of keeping things updated in a fairly automated fashion. This post will serve a reference point for that.

Use at Your Own Risk!

This script is offered free of charge and is intended as a guideline only. If in doubt, speak with your system administrator or programmer. And ALWAYS backup -- at least at first -- before using it. Use this at your own risk.

FWIW, we use this regularly and are just sharing it here so that others may find it useful.

Things We Need

  1. You need SSH access to your server.
  2. You need the ability to create cronjobs
  3. You need the ability to use wget and tar commands
  4. The ability to execute shell scripts (bash script)
  5. mysqldump is an added bonus if you have it available.

** In the case that you have managed hosting, this guide can also be used by your hosting provider to implement for you.

Creating a Central Location

The idea here is to keep a current copy of WordPress, any plugins you use, and any themes in a single centralized location. For our purposes here, we will use the following format. Create your own structure, or feel free to use the one below. If you use a custom structure, be sure to edit the script to reflect your own structure.

/home/upgrades/repo/
/home/upgrades/repo/downloads
/home/upgrades/repo/plugins
/home/upgrades/repo/themes
/home/upgrades/repo/wordpress

Upload all of your common plugins in the plugins folder and themes in the themes folder

Creating the Script

In a plain text editor, like EditPad, you can paste in the following as a guide.

#!/bin/bash

# ##########################################################
## REMOVE ALL STANDARD PLUGINS AND WORDPRESS FILES
#  We cannot remove themes cause of the way they do logins
#  so themes will need to be downloaded manually. Plugins use
#  version numbers in their files so cannot download those.
## ##########################################################
rm -rf /home/upgrades/repo/wordpress


# ##########################################################
## DOWNLOAD, UNPACK and PREPARE WORDPRESS WORDPRESS
## ##########################################################
wget -P /home/upgrades/downloads/ https://wordpress.org/latest.tar.gz
tar -xzf /home/upgrades/downloads/latest.tar.gz -C /home/upgrades/repo/

# ##########################################################
## DELETE OLD AKISMET / COPY NEW AKISMET TO PLUGINS FOLDER
## ##########################################################
rm -rf /home/upgrades/repo/plugins/akismet
cp -apr /home/upgrades/repo/wordpress/wp-content/plugins/akismet /home/upgrades/repo/plugins/


# ##########################################################
## DELETE FILES AND DO SOME HOUSECLEANING
## ##########################################################
rm -f /home/upgrades/downloads/*


# ##########################################################
## BACKUP ALL DATABASES
## ##########################################################
mysqldump --user=YOURDBUSERNAME --password='DBPASSWORD' --host=localhost DBNAME01 > /home/upgrades/backups/sql/DBNAME01.sql
mysqldump --user=YOURDBUSERNAME --password='DBPASSWORD' --host=localhost DBNAME02 > /home/upgrades/backups/sql/DBNAME01.sql
mysqldump --user=YOURDBUSERNAME --password='DBPASSWORD' --host=localhost DBNAME03 > /home/upgrades/backups/sql/DBNAME01.sql


# ##########################################################
#
# UPDATE WORDPRESS SITES
# Replace Site01.com with your own domain. Same with Site02,
# Site03, etc. You can have as many sites as you like.
# 
# Also, be sure you ONLY DELETE THE THEME PARENT FOLDER.
# DO NOT DELETE your child theme. This script assumes you
# use Child Themes which you should always use anyway.
#
# ##########################################################

# FIRST WORDPRESS SITE
rm -rf /home/SITE01.com/htdocs/wp-admin/
rm -rf /home/SITE01.COM/htdocs/wp-includes/
rm -rf /home/SITE01.COM/htdocs/wp-content/plugins/
rm -rf /home/SITE01.COM/htdocs/wp-content/themes/THEME-PARENT-NAME/
rm -rf /home/SITE01.COM/htdocs/wp-content/themes/twentyfifteen/
rm -rf /home/SITE01.COM/htdocs/wp-content/themes/twentyseventeen/
rm -rf /home/SITE01.COM/htdocs/wp-content/themes/twentysixteen/
rm -rf /home/SITE01.COM/htdocs/wp-*.php
rm -rf /home/SITE01.COM/htdocs/xmlrpc.php
cp -apr /home/upgrades/repo/wordpress/* /home/SITE01.COM/htdocs
cp -apr /home/upgrades/repo/plugins/* /home/SITE01.COM/htdocs/wp-content/plugins/
cp -apr /home/upgrades/repo/themes/* /home/SITE01.COM/htdocs/wp-content/themes/

# SECOND WORDPRESS SITE
rm -rf /home/SITE02.COM/htdocs/wp-admin/
rm -rf /home/SITE02.COM/htdocs/wp-includes/
rm -rf /home/SITE02.COM/htdocs/wp-content/plugins/
rm -rf /home/SITE02.COM/htdocs/wp-content/themes/THEME-PARENT-NAME/
rm -rf /home/SITE02.COM/htdocs/wp-content/themes/twentyfifteen/
rm -rf /home/SITE02.COM/htdocs/wp-content/themes/twentyseventeen/
rm -rf /home/SITE02.COM/htdocs/wp-content/themes/twentysixteen/
rm -rf /home/SITE02.COM/htdocs/wp-*.php
rm -rf /home/SITE02.COM/htdocs/xmlrpc.php
cp -apr /home/upgrades/repo/wordpress/* /home/SITE02.COM/htdocs
cp -apr /home/upgrades/repo/plugins/* /home/SITE02.COM/htdocs/wp-content/plugins/
cp -apr /home/upgrades/repo/themes/* /home/SITE02.COM/htdocs/wp-content/themes/

# THIRD WORDPRESS SITE
rm -rf /home/SITE03.COM/htdocs/wp-admin/
rm -rf /home/SITE03.COM/htdocs/wp-includes/
rm -rf /home/SITE03.COM/htdocs/wp-content/plugins/
rm -rf /home/SITE03.COM/htdocs/wp-content/themes/THEME-PARENT-NAME/
rm -rf /home/SITE03.COM/htdocs/wp-content/themes/twentyfifteen/
rm -rf /home/SITE03.COM/htdocs/wp-content/themes/twentyseventeen/
rm -rf /home/SITE03.COM/htdocs/wp-content/themes/twentysixteen/
rm -rf /home/SITE03.COM/htdocs/wp-*.php
rm -rf /home/SITE03.COM/htdocs/xmlrpc.php
cp -apr /home/upgrades/repo/wordpress/* /home/SITE03.COM/htdocs
cp -apr /home/upgrades/repo/plugins/* /home/SITE03.COM/htdocs/wp-content/plugins/
cp -apr /home/upgrades/repo/themes/* /home/SITE03.COM/htdocs/wp-content/themes/

Saving and Uploading

Save this script as upgrade.sh and upload it to the /home/upgrades/ folder created in the first step above.

The Script Explained

Much of it is documented throughout already, but we will explain a few of the sections.

  1. The first section deletes the old WordPress archive previously downloaded.
  2. The next section grabs the latest version of WordPress, downloads it, extracts is, and then saves it in the /home/repo/wordpress folder. This ensures you have the latest copy.
  3. Akismet. Because WordPress ships with the latest Akismet embedded, we need to copy it to to our plugins folder. This routine does that.
  4. This routine deletes the downloaded tar file from the downloads folder.
  5. Backing up the databases. Never a bad thing to do before upgrading. Backed up databases are stored in the /home/upgrades/backups folder.
  6. Update each WordPress site. Note that the first part of this section for each site deletes the wp-admin, wp-includes, the plugins folder, the standard themes AND the parent theme folder. We specify the themes for each site to ensure we are not deleting the CHILD theme we are using. We delete all core WordPress files to ensure that no orphans are left behind. This also ensures you have a clean original copy should any of your files ever get hacked.

    The second half of each of the site updates involves copying the fresh copy of WordPress we just downloaded and extracted, as well as the plugins and themes we downloaded and placed into the respective folders.

Executing the Script

Here you have two choices. You can either execute the script regularly via cronjob, or simply ssh to your server as follows. Note, the ">" represents a command prompt:

> cd /home/upgrades
> ENTER

> bash ./upgrade.sh
> ENTER

That's it. The script will go through and replace all of the core WordPress files and folders, as well as your plugins and themes with the newer versions located in your "repo".

The entire process is fairly quick. We can upgrade about 60 sites this way in less than a minute or so.

Download the Script

If you'd prefer a pre-formatted copy, you can download the above file right here: upgrade-example.sh

Updating Sites on Multiple Servers

This is easily achieved by having your host install keys on each server you want to communicate with, and then using rsync to replicate your "/home/upgrades/repo/" structure. Then just customize the script to that server's database names and site specific information.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×