Hey folks! Today I am going to share a complete guide to migrate your WordPress blog to EC2. This guide is made with one month of effort in migrating my own blog to Amazon Web Services. AWS offers you a free tier for one year and the resources provided in the free tier are good enough for a small blog with not so high traffic and can be easily used as an alternative to shared web hosting.
My reason of shifting to EC2 was the down time I faced with my previous hosting provider, your’s can be anything. AWS provides you with a dedicated server and that’s priced like hell when you go for hosting providers like Hostgator and GoDaddy. So why not use a free tier!
So lets start with the guide. I will divide the guide in steps for you, so that you can use the below links to skip the steps you have already done.
- Step 1: Setup an AWS account
- Step 2: Creating an EC2 Instance
- Step 3: Allocate an elastic IP address to your EC2 instance
- Step 4: Connect to your EC2 instance using SSH
- Step 5: Install LAMP server
- Step 6: Migrating data from old hosting provider
- Step 7: Configuring MySQL database
- Step 8: Changing URLs in the database using phpMyAdmin
- Step 9: Setting up the Permalinks
- Step 10: Editing the Apache Configuration file
- Step 11: Setting up Email Notifications
- Step 12: Mapping your domain to EC2
This is one of the easiest steps in this whole guide. But still I have to write this step because a lot of people are hesitated about setting up there account on AWS. One of the reason is the need of a credit card to use AWS. I personally suggest you to use a virtual credit card service like Instant Virtual Credit Cards or Entropay to overcome this hesitation. When you use a virtual credit card with there is no risk of AWS charging you without your wish. Though the $1 verification fees is mandatory.
Once you are signed up successfully you might need to wait for a couple of hours until you get the access to your Amazon Web Services Console.
And once you are verified, you will be notified via an email and you will get access to the console which looks like this:
First of all let me tell you the meaning of an EC2 instance, it is a virtual server on Amazon’s Elastic Compute Cloud (EC2) for running various applications like WordPress on the Amazon Web Services (AWS) infrastructure. Now its your choice to select the type of instance, I recommend you to go with the Micro Instance because its free tier eligible and is good enough to run a small blog/website.
Now follow these steps to launch an instance:
- Go to AWS management console.
- Click on EC2 tab>Launch instance.
- Choose the operating system, I chose a 64-bit Ubuntu 14.04, because I have used Ubuntu in the past.
- Choose the type of instance, I chose the micro instance.
- Click on Next: Configure Instance Details>Next: Add Storage>Next: Tag Instance>Next: Configure Security Group
- Click on Add Rule and choose HTTP. (This will allow the public to view your site)
- Click on Review and Launch>Launch
- Now create a new key pair and download it. (This is the most important step because key can be generated only once, thus I suggest you to save it on multiple locations and you can also save it on any Cloud Storage disk like Google Drive if you want)
- Lastly click on Launch Instances.
Now you have successfully created and EC2 instance. Instance might take a couple of minutes to start. Once your instances are up and running proceed to step 3.
Now we will have to assign an IP address to our instance, it is called and Elastic IP address as it won’t change until and unless you wish to do so.
But keep in mind that you will get only one free Elastic IP address with your free tier and you can’t keep it unassigned, if any elastic IP address is kept unassigned AWS will charge you on hourly basis, though it will be in some cents. I recommend you never to stop an instance before assigning the Elastic IP to a temporary instance.
You can comment below if you have any query regarding any step mentioned in this guide.
So here is how we will assign the elastic:
- Visit this link and click on Running Instances.
- Now click on Elastic IPs which is present under the Network and Security group in the left hand side of the page.
- Now click on Allocate New Address button and then an Elastic IP address will be assigned to you.
- Once you have got the IP address now its time to associate it with the instance. To do so just click on Associate Address and then choose your instance and click on the Associate button.
Step 4: Connect to your EC2 instance using SSH
Now its time to connect to your instance using SSH. To SSH into your instance you can use your terminal (On Mac OS and Linux). And if you are a Windows user then you can follow this guide to connect to your instance.
For SSH we will need the key we downloaded earlier. So please copy the key in the home folder.
So once you have copied it in the home folder. It’s time to open the terminal.
Write the below given command after editing the values.
sudo ssh -i keyname.pem ubuntu@your-elastic-ip-address
Then you will get a message like
The authenticity of host ‘18.104.22.168 (22.214.171.124)’ can’t be established.
ECDSA key fingerprint is f9:6b:ac:c3:c8:ab:d8:17:29:9e:2d:02:55:ca:e4:e5.
Are you sure you want to continue connecting (yes/no)?
Enter Yes and then type the below command again if you are not yet connected to your instance.
sudo ssh -i keyname.pem ubuntu@your-elastic-ip-address
Step 5: Install LAMP server
So now we will install some of the stuff required to make your EC2 instance to host your website.
We will start with LAMP and by the way it is called LAMP because it means package of Linux, Apache, MySQL and PHP.
Firstly we have to upgrade the system’s repository list and update the already installed packages.
So to upgrade and update write the following commands on your terminal while you are connected to your server.
sudo apt-get upgrade
sudo apt-get update
Finally its time to install LAMP server. So key in the below command to install LAMP.
sudo apt-get install lamp-server^
When you key in the following above command you will get a pop up asking you for the password you want to set for the root user. So you can keep it whatever you wish, but please make sure you remember it. We will need it for setting up MySQL.
Now to check wether LAMP is installed properly or not. We have to visit the elastic IP address we got earlier and if you see something like the below screenshot on your browser window then its time to skip to next step because we have successfully configured a web server on a EC2 instance.
Step 6: Migrating data from old hosting provider
So first we will make a zip file of all the files of your website.
- To do so login to your ftp client or the cpanel.
- Then visit the folder where you have stored your files. If you have a single site account then it must be the root folder of your web server.
- Then create a zip file and name it as data.zip and keep it in the root directory such that it can be easily downloaded (Recommended: http://your-domain/data.zip).
Now to transfer files from one server to another we will use wget. And to unzip the files we will use unzip. To install them you can use the below command:
sudo apt-get install wget && sudo apt-get install unzip
Now we will store our files in the html folder on our server. So change your directory by using the following command:
To start the download you will have to use the below command.
sudo wget http://yourdomain.com/data.zip
Now its going to take some time to download the files and thus this is the time you should take a break till the data gets downloaded on the server.
And when it is completed. It’s time to unzip it using the below command:
sudo unzip data.zip
Step 7: Configuring MySQL database
No we have the data and thus we need the database to make a connection. So we will first download our database and then upload it to our old host. So that we can download it directly from the server.
I recommend you to use phpMyAdmin provided by your hosting provider to export the database.
So follow the below steps:
- Click on export tab in your phpMyAdmin console.
- Now choose custom as export method.
- And choose compression as zip in the output section.
- Lastly click on GO to download it to your system.
- Once it is downloaded you have to upload it to your previous host such that you can download it using wget on your EC2 instance.
And now its time to download it to your EC2 instance using wget.
So type the below command:
sudo wget http://your-domain/database.sql.zip
Once it is downloaded, you have to unzip it.
sudo unzip database.sql.zip
Now we will have to create an empty database which we will later use to import our database.
To create database type
mysqladmin -uroot create blog -p
Now enter the password you chose while MySql installation and then type
mysql -u root -ptest blog
Now we need to edit the wp-config file with new name and password of database.
To do so we will use vim. Type the below command to install vim and edit the file.
sudo apt-get install vim && sudo vim wp-config.php
Now we will modify it using insert function (press i) to this:
Step 8: Changing URLs in the database using phpMyAdmin
Now we have to change the URLs in the database, so that you can test your website before mapping the domain. To do this we will have to use phpMyAdmin.
- To install phpMyAdmin type the below command:
sudo apt-get install phpmyadmin
Choose apache2 when this popup appears:
- Then type the password of your database.
- Lastly choose password for MySql configuration and you are done installing phpMyAdmin.
Now we will make some changes in the apache2 config file and add the phpMyAdmin config file to it. So get into the apache2 config file using the below command.
sudo vim /etc/apache2/apache2.conf
And now add the below line to the end of the file
Now its time to restart the apache server and login to your phyMyAdmin console.
Key in the below command to restart it.
sudo service apache2 restart
Now visit your phpMyAdmin console with the credentials you used.
In the left hand side choose Blog and then click on SQL in the Menu bar and type the following command after all the modifications.
Then click on GO.
UPDATE wp_options SET option_value = replace(option_value, ‘http://yourdomain’, ‘http://your-elastic-ip’) WHERE option_name = ‘home’ OR option_name = ‘siteurl’;
UPDATE wp_posts SET guid = replace(guid, ‘http://yourdomain’,’http://your-elastic-ip’);
UPDATE wp_posts SET post_content = replace(post_content, ‘http://yourdomain’, ‘http://your-elastic-ip’);
Now you may visit your Elastic IP address to see the migrated website. But some links and images might not work well, but don’t worry we are going to fix all that in this guide.
Step 9: Setting up the Permalinks
Now now as we know the (.) or the hidden files are not downloaded when we did the file transfer via wget, so we will need to copy the .htaccess file from our old host to new host.
So you need to access your .htaccess file on the old server and then copy the code from it and paste it on a new file on your ec2 instance.
Here is how to create a new .htaccess file. (Remember to create this in /var/www/html folder):
sudo vim .htaccess
Step 10: Editing the Apache Configuration file
This step will make all the links to work and our website will be totally migrated and working.
To edit the configuration file type:
sudo vim /etc/apache2/sites-available/000-default.conf
Now add the following code below the line under (DocumentRoot /var/www/html) on section <VirtualHost *:80>
Remember to update the first line with your elastic ip.
Now we have to change some permissions, you can do so by typing the below commands one by one.
sudo a2enmod rewrite
sudo service apache2 restart
sudo chown www-data:www-data /var/www/html/.htaccess
sudo chmod 664 /var/www/html/.htaccess sudo chown -R www-data:www-data /var/www/html
Step 11: Setting up Email NotificationsNow once you are done with testing its time to setup the email server which will be used to send notifications to you.
Now we just need to write the below command and we are done setting up the email notifications.
sudo apt-get install sendmail && sudo ln -s /usr/sbin/sendmail /usr/bin/sendmail
So now this step will be different for different providers, but the basic thing to change is same for all. But before mapping your domain to EC2 we will need to make the change in database and Apache configuration file which we made while testing our migrated website.
So now go back to phpMyAdmin and type the below code in the SQL tab:
UPDATE wp_options SET option_value = replace(option_value, ‘http://elastic-ip’, ‘http://yourdomain’) WHERE option_name = ‘home’ OR option_name = ‘siteurl’;
UPDATE wp_posts SET guid = replace(guid, ‘http://elastic-ip’,’http://yourdomain’);
UPDATE wp_posts SET post_content = replace(post_content, ‘http://elastic-ip’, ‘http://yourdomain’);
and edit to the apache2 config file, go to step 10 and replace the elastic-ip address with your domain name in the firs line it will look like:
Like every other blogger in this world, I also used some references to make this guide complete. Here are all of them:
So I hope this guide helped you in setting up your website on EC2. And if you have any questions or suggestions you can leave them below in the comment section or email them to me at firstname.lastname@example.org.
And don’t forget to share the guide with your friends, they should also get the free benefits of AWS 🙂