Categories
AWS Databases EC2 MySQL Wordpress

From Hostgator shared hosting to AWS EC2 using Bitnami’s WordPress AMI

I needed to move two WordPress sites from a Hostgator shared hosting account to AWS EC2 for two simple reasons: website performance and SSL certificates.

There are several WP plugins to ease the transfer but the main problem is that the entire website + database is almost 1GB in size so I have to do it the manual way: (S)FTP.

Before continuing, I suggest installing this plugin: https://wordpress.org/plugins/all-in-one-wp-migration/ and verifying that the file is larger than 512MB.

If it’s not larger than 512MB then you can follow Steps 2, 3, install the plugin on the new website, run the import and then finish with Steps  6, 7 – easy peasy.

DISCLAIMER: this is not a detailed/in-depth guide, it assumes you have basic knowledge of Unix command line, this is to simply give you an idea on what you need to do.

Step 1 – get the zip

In the Hostgator shared hosting account, you get cPanel so you’re able to compress the website and download the zip file. I recommend compressing the website folder instead of the individual files.

Don’t forget the database export via phpMyAdmin!

Step 2 – launch the EC2 instance

Go into your AWS console > EC2 > Launch Instance and select the AWS Marketplace tab, search for “wordpress” and select the first image:

On the next window I’m going to select a t2.micro instance since that’s more than enough for my needs.

You can launch this instance in the default VPC or create your own by following this awesome tutorial by Mike Tabor: https://miketabor.com/create-a-custom-vpc-with-private-and-public-subnets-on-aws/

For storage, again, is up to you. You can leave it a 10GB or increase to whatever size you need.

Add a tag “Name” since it will help you to easily identify your instance.

For the “Configure Security Group” you can select the first option, create new one, since you’ll need to open those ports either way: 22 for SSH, 80 for HTTP and 443 for HTTPS.

Next is launch and create new key pair or select an existing one.

Step 3 – assign a static public IP

Doing so will allow you to point your DNS to the new instance.

In the EC2 section of the console, on the left pane, scroll down to Network & Security and go to Elastic IPs.

Click “Allocate new address” > “Allocate” and you’ll get a new one. Now you just have to select it from the list and associate the address to your newly created instance (the Name tag will make it easy to identify).

Step 4 – blow it up (upload the website)

This is where you blow up the server by SSHing into it and uploading your website.
Bring up your terminal and SSH into your instance using the key pair:

ssh bitnami@PUBLIC-IP -i KEY-PAIR.pem

Now:

cd /opt/bitnami/apps/wordpress

The “htdocs” folder is where the WP installation is at, you need to replace this with the website you zipped.

So lets upload it, open a new terminal tab/window and type:

 scp -i KEY-PAIR.pem /path/to/local/file bitnami@PUBLIC-IP:/opt/bitnami/apps/wordpress/

Go back to the tab in which you’re logged into the EC2 instance and unzip the file you just uploaded:

unzip FILE

We now need to rename the existing htdocs directory:

mv htdocs htdocs-backup

And let’s do the opposite with the website we uploaded:

mv WEBSITE-DIRECTORY htdocs

It’s possible that the website directories and files we imported have different permissions than the ones needed in our bitnami image, we can fix that by running these commands:

 find /opt/bitnami/apps/wordpress/htdocs -type d -print0 | sudo xargs -0 chmod 775
 find /opt/bitnami/apps/wordpress/htdocs -type f -print0 | sudo xargs -0 chmod 664

We also need to reassign the directories and files to a new owner, in this case we assign it to bitnami of the group daemon:

sudo chown -R bitnami:daemon htdocs

Step 5 – upload the database

The Bitnami image comes with phpMyAdmin and you can find the instructions on how to access it here: https://docs.bitnami.com/virtual-machine/components/phpmyadmin/

You can now open phpMyAdmin, select the “bitnami_wordpress” database, select all tables, drop them and then import your database.

You’ll have to update the database credentials in your wp-config file. SSH into the instance, show the contents of the old wp-config.php file and then copy them:

 cat /opt/bitnami/apps/wordpress/htdocs-backup/wp-config.php

With the credentials on your clipboard, open the wp-config.php of your website, paste the new credentials and delete the old ones:

nano /opt/bitnami/apps/wordpress/htdocs/wp-config.php

Step 6 – point DNS to new server IP

As last step, you need to point your DNS A record to the new server IP address, it may take some time to propagate.

I suggest you verify that the migration went well by accessing the website in the new server, the problem is that your WP is pointing to your domain and it might redirect you back to the domain. A temporary fix is to add an entry to you “hosts” file with the public IP and domain name.

Step 7 – remove bitnami badge

https://docs.bitnami.com/aws/components/bninfo/

BONUS – generate and install SSL certificate + force SSL in WordPress

You can find the instructions for the SSL certificate here: https://docs.bitnami.com/aws/how-to/generate-install-lets-encrypt-ssl/

And to force SSL (on everything: images, javascript, etc) in WordPress you can install this plugin: https://wordpress.org/plugins/really-simple-ssl/

If you get a popup asking for FTP credentials when adding a new plugin, you can simply add the following line to your wp-config.php:

define('FS_METHOD', 'direct');

===

And that’s it! I know it wasn’t super detailed but it gives you a quick overview of what you need to do.

Leave a Reply