My friend’s 1 year free tier from AWS just expired. Instead of paying for the subscription, she ask if she can move her WordPress blog to my EC2 server.

I don’t get a lot of traffics on my server, thus hosting another WordPress should be fine.

Backing up WordPress

WordPress is basically a bunch of files and a database. Backing up all the files and database, transferring them to the new server, then we should be good.

Backup Database

WordPress recommend using phpMyAdmin to do this. But I decided to use mysqldump, which should be installed in Ubuntu LAMP server already.

# mysqldump Usage
mysqldump [OPTIONS] database [tables]
mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
mysqldump [OPTIONS] --all-databases [OPTIONS]

As you can see, you need to know your WordPress database name to back it up. My database name is just wordpress, so I ssh to my server and ran:

mysqldump wordpress > wordpress.sql

This creates a wordpress.sql file in the current working directory.

Backup WordPress Files

I installed my WordPress on /var/www/wordpress, so this is the folder I need to backup.

# Mind the dot at the end
tar -zcvf wordpress-backup.tar.gz -C /var/www/wordpress .

This will create a wordpress-backup.tar.gz file that contains all the WordPress file in the current working directory.

Download the Backups

To download the backup files, we need to use SFTP to connect to the EC2 instance. I used Cyberduck, it’s free and easy to use.

Since EC2 uses public key authentication, we need to check the Use Public Key Authentication checkbox at the bottom, then select your instance’s .pem file. Username should be ubuntu if you setup your EC2 using the Ubuntu 14.04 image.

cyberduck

After you connect, simply locate the wordpress.sql and wordpress.tar.gz file and download them to your computer.

Moving to New Server

Using Cyberduck and SFTP, connect to your new server and upload the 2 backup files. The procedure is very similar to how we download the backup files. Just change the server address and .pem file.

Then ssh to your new server.

Restore the Database

We will use mysql to restore the database, but before we do that, we need to create a new empty database.

Login to mysql using:

mysql -u root -p

You will be asked for your MySQL database password.

In the mysql command prompt, we then run:

# you should choose a better database name than wordpress2
CREATE DATABASE wordpress2;

This will be where we save the restored database.
Then we need to create a new user for the restored WordPress, so it can access this database.

# create a new user
CREATE USER [email protected] identified by 'password';

# give the new user access to wordpress2 database
GRANT ALL PRIVILEGES ON wordpress2.* TO [email protected];

# make sure the changes take effect
FLUSH PRIVILEGES;

# exit mysql prompt
exit;

Be sure to match your database name with the one you created in the previous step, and choose a better password.

Now we can restore the database using:

mysql wordpress2 < wordpress.sql

Restore the Files

First, uncompress the wordpress.tar.gz file we uploaded using:

tar -xvzf community_images.tar.gz

This will create the same folder in the current working directory as the one you compressed previously.

If this folder is not in the /var/www/ directory, use the mv command to move it there.

You should also run:

sudo chown -R www-data:www-data /var/www

In case the uncompressed folder has a different ownership.

Add a Virtual Host

Depends how your server is setup, you need to update your apache or Ngnix so it recognizes the newly added WordPress blog. I’m using apache, and this is what I did:

# cd to the vhost config directory
cd /etc/apache2/sites-available

# copy my wordpress's config file for the new wordpress
cp my-wordpress.conf new-wordpress.conf

# edit the new-wordpress.conf
vim new-wordpress.conf

I modified 4 rows in new-wordpress.conf:

// new-wordpress.conf

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName domain-of-my-friends-wordpress.com
    ServerAlias www.domain-of-my-friends-wordpress.com
    DocumentRoot /var/www/wordpress2
    ...

Then reload apache:

sudo service apache2 reload

Change DNS of the domain

You now need to update your DNS with the IP address of the new server. Depends on which domain registrar, you will use different tools. Simply updating the IP address value in the A record to the IP address of the new server should do the job.

See the Result

Wait for your DNS to update, then enter the domain in your browser. You should see the same WordPress site like nothing have changed!