Code

creating a WordPress staging instance

I always want to test out updates before making them on my live website, even if it’s just minor version updates to plugins, to check that the update completes and there are no obvious breaking changes. In the past, I just spun up a new Lightsail instance and imported a backup of my site to use as a staging environment.

This works ok for some updates, but the drawback is that the new environment is not always identical to the old, since AWS provides the latest Bitnami package for new instances; under the standard Create Instance flow, I can’t choose to spin up a new instance with an old bundle to match what my live site is running. That means that using this method, I can’t actually test the upgrade step from an older to a newer WordPress (or PHP) release, and I can’t test upgrading plugins on the old version.

For my latest upgrade, I needed to update several plugins and update WordPress to 5.6 and update PHP to version 7.4.x. The PHP update can only (reasonably) be done by spinning up a new instance, anyway, so I tried updating in one fell swoop, but the import (using All-in-One WordPress Migration) hung at a point that left the site broken and admin inaccessible, so I needed to make the updates more piecemeal and tried using snapshots to spin up an exact copy of the live site, instead. It wasn’t quite as simple as snapshot, create instance, and go, but it was close enough and allowed me a staging instance using the same bundle as the live site, so this is the way I’ll go for future updates.

Create a new instance from a snapshot

See the AWS blog if you want more detail / screenshots on this process.

First, log in to the AWS Lightsail console, select the instance to copy, navigate to “Snapshots” and create a snapshot. The snapshot creation process took a few minutes for me, which I expect would vary depending on the size of the instance, the amount of data you are using, and whether you have previous snapshots of the instance (snapshots after the first are basically patches, storing the difference between the current state and the previous snapshots).

Then, when the snapshot is complete, click the “⋮” menu button beside it and select “Create new instance”. Change any settings you need to (I removed a tag I use to mark my live instance and changed the name from the default to indicate what I was testing) and click the button to create the instance.

Stop system apache and start bitnami

I expected that, as a copy of my prod instance, the staging instance would have Bitnami up and running by default when the instance was running. Instead, it was running the system’s apache2 instead of Bitnami’s, and my WP site was not accessible, so I had to stop and disable the system apache and restart Bitnami. (H/T this post on Bitnami forums for the solution.)

To check whether bitnami is running, ssh into the instance and run:

sudo /opt/bitnami/ctlscript.sh status

If apache is not running, check whether the system apache is:

sudo service --status-all | grep apache

If the output includes

[ + ] apache2

then the system apache is running. Stop and disable it:

sudo service apache2 stop
sudo update-rc.d apache2 disable

Then start bitnami:

sudo /opt/bitnami/ctlscript.sh start

and when this completes, you should be able to access your WP site and admin. You can close the terminal window.

Because I have an SSL cert and an http -> https redirect set up (which both got copied by the snapshot), I got a security warning when accessing the staging instance via its IP address. I could probably prevent the warning by removing the redirect and using http, but it wasn’t necessary for my testing, and I stopped and deleted the whole instance after testing the updates (which only took a few minutes anyway), so I chose to ignore it.

If you use Jetpack, it will probably have put the site in Safe Mode because it’s a copy; for once, it’s correct, so go ahead and confirm.

Cleanup

Once you’ve completed your tests, stop the instance and delete it, to avoid being charged for an instance while you’re not using it; you can spin up a new one the same way the next time there’s an update to make. Keep or delete the snapshot as it suits you; it can serve as a backup and is relatively cheap, but it’s not free.

Leave a Reply