Recipe: Migrating a WordPress Blog Over AWS Lightsail

The blog you are currently on used to be hosted on some Virtual Private Server. My hosting provider decided to quit the business and I had to leave. He kindly suggested that I look at AWS Lightsail as a migration option. I did, and this is where this blog is now hosted.

This blog is not new. It already had content. It also has customisations like plugins and themes. This post will focus on how to export your content and customisations from a former blog and import them on Lightsail. Its accuracy may vary depending on the level of customisation your blog has. Command line (CLI) experience is required.

If you have not already, you can follow the official Quick start guide: WordPress on Amazon Lightsail to get the basic setup in place. Make sure to pick a Database Management System (DBMS) that matches the one you will be exporting from. Once you have confirmed that (1) the DNS setup is right by reaching the page with a browser and (2) you can log into the machine with SSH, you are good to proceed.

Matching WordPress Versions

Ideally, your existing blog and Lightsail WordPress versions should match. The most straight forward path it to log into both of them and run the updates to be on the latest.

Exporting From Former Blog

From your existing blog, you will need to export 2 things: the database (DB), and the wp-content directory.

For the database, you can export it with a single command. If you are using mysql, the command will be something like

mysqldump -u DB_USER -p DB_NAME > blog.sql

If your database provider is mariadb or postgres, there are similar commands to do that.

To create an archive of the wp-content directory, go to the root of your former WordPress directory and run

tar -zcvf wp-content.tar.gz wp-content

Then you can use scp to copy both blog.sql and wp-content.tar.gz locally.

scp user@host:path-to-wp-content.tar.gz .
scp user@host:path-to-blog.sql .

Importing To Lightsail

WARNING: Be careful. Do at your own risk. Errors can break the Lightsail deployment.

Importing into Lightsail is not as direct as it could be because some permissions are lacking. We will work around them.

The DB

For the database, due to permissions, we cannot simply create a new one. So, we will need to operate with the one we have.

Finding the DB Credentials

To find the DB credentials, log with SSH into the Lightsail instance. Then run

vim stack/wordpress/wp-config.php

Search for DB_NAME, DB_USER and DB_PASSWORD to get the associated values.

Renaming the Lightsail WordPress Tables

If you know your former WordPress DB tables used a non default prefix, you can skip this step.

Otherwise, this step is important to avoid collisions with the tables you are going to import. Renaming the existing tables also means you can restore them if things go wrong.

On the Lightsail instance, you can copy paste the following into a script named rename_wp_tables.sql.

ALTER TABLE wp_commentmeta RENAME back_wp_commentmeta;
ALTER TABLE wp_comments RENAME back_wp_comments;
ALTER TABLE wp_links RENAME back_wp_links;
ALTER TABLE wp_options RENAME back_wp_options;
ALTER TABLE wp_postmeta RENAME back_wp_postmeta;
ALTER TABLE wp_posts RENAME back_wp_posts;
ALTER TABLE wp_term_relationships RENAME back_wp_term_relationships;
ALTER TABLE wp_term_taxonomy RENAME back_wp_term_taxonomy;
ALTER TABLE wp_termmeta RENAME back_wp_termmeta;
ALTER TABLE wp_terms RENAME back_wp_terms;
ALTER TABLE wp_usermeta RENAME back_wp_usermeta;
ALTER TABLE wp_users RENAME back_wp_users;

Then run it.

mariadb -u DB_USER -p DB_NAME < rename_wp_tables.sql

Importing the DB

From your local environment, upload your blog.sql file to the Lightsail instance.

scp blog.sql user@host:.

Then, from the Lightsail instance, import it.

mariadb -u DB_USER -p DB_NAME < blog.sql

Maybe: Update wp-config.php

If your imported table names have the wp_ prefix, you can skip this step. Otherwise, run

vim stack/wordpress/wp-config.php

Search for table_prefix and set it to your prefix value.

Note that to use the tables that were renamed earlier, you can set that value to back_wp_.

The Customisations

From your local environment, copy your wp-content.tar.gz archive over the the Lightsail.

scp wp-content.tar.gz user@host:.

Log into the Lightsail instance and extract it here. (Due to the permissions, we cannot simply extract it in the right location.)

tar -xvf wp-content.tar.gz

Backup the content of the Lightsail wp-content directory. If things go wrong you can restore them.

cd stack/wordpress/wp-content
mv languages{,BACK}
mv plugins{,BACK}
mv themes{,BACK}

Then move these 4 directories from your archive to the current location. (Note that uploads does not need to be backed up as it starts as an empty directory.)

mv ~/wp-content/languages .
mv ~/wp-content/plugins .
mv ~/wp-content/themes .
mv ~/wp-content/uploads .

Now, the blog will work, but updates won’t. That is because the files owner do not match the one used to run the blog and it will not be able to overwrite them. If you are still using the bitnami default user, that can be fixed by running

sudo chown -R bitnami:daemon languages
sudo chown -R bitnami:daemon plugins
sudo chown -R bitnami:daemon themes
sudo chown -R bitnami:daemon uploads

Wrap Up

It should work now. Head to the Lightsail WordPress /wp-admin page and log in with your former credentials, not the Lightsail ones. The Lightsail credentials were valid for the version of the blog that is in the backup tables / the ones with a different prefix. At this point it’s up to you to keep or delete these tables as well as the backup files in wp-content.

Should you be curious, I’m using the basic Lightsail configuration currently at 3.50 USD per month.

  • 512 MB RAM
  • 2 vCPUs
  • 20 GB SSD
  • 1TB Transfer

Given I’ve got at most 100 views per day, this is more than enough.

Related Posts

Leave a Reply

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