Getting Site Moved to Amazon EC2 - A Step By Step Guide

shabbir's Avatar author of Getting Site Moved to Amazon EC2 - A Step By Step Guide
This is an article on Getting Site Moved to Amazon EC2 - A Step By Step Guide in Web Development.
We will use Amazon Linux AMI an the instructions are according to Amazon Linux AMI but I don't think they would vary too much for other Linux kernels.

Step 1: Getting EC2 Instance Up and Running

First Get Started With Amazon EC2 Instance and then Launch your Amazon EC2 Instance. Next is to login to the EC2 Instance over SSH and execute commands from the terminal.

As logged into your Amazon Instance, just issue the pwd command and you should see the output as /home/ec2-user.

Note: If you are not able to connect to port 22 of your instance over SSH, make sure you have the the right rules for Security Groups.

Step 2: Installing and Configuring Apache and MySQL

Access to Shell does mean you can configure your Amazon EC2 Instance as you want and the next step is to install Apache with PHP support and MySQL.

Default installation configuration does mean that .htaccess does not work in Apache (at least did not work for me) and so if you want to be using .htaccess, you have to edit the Apache configuration.

Edit the Apache configuration file /etc/httpd/conf/httpd.conf and change

AllowOverride None to AllowOverride All.

Once done, restart the Apache server with the following command

sudo service httpd restart
If you want to be hosting only one domain in your Amazon EC2, you can place the html and php inside /var/www/html but if you want to have more than one domain, you will need to add the VirtualHost Configuration to the Apache config files.

Edit the conf file once again and confirm if Apache is listening to Port 80.

Listen 80
Once confirmed move to the end of the file and find

NameVirtualHost *:80

If the above line is commented, uncomment it and then for each domain that you want to be hosting in your Apache, you have to add the following block in the configuration file and then restart the Apache.

<VirtualHost *:80>
     DocumentRoot /var/www/folder_for_the_domain/html
     ErrorLog /var/www/folder_for_the_domain/logs/error.log
     CustomLog /var/www/folder_for_the_domain/logs/requests.log
Before restarting the server make sure you have created all the folders like folder_for_the_domain, folder_for_the_domain/html and folder_for_the_domain/logs or else Apache will fail to start.

If you want to be using different IP for the domains, you have to change the first line to the respective IP like this


Step 3: Moving PHP/HTML Files to EC2 Instance

If you are used to upload files with FTP, its time to change. Installing FTP application on Amazon EC2 instance is not recommended and on top of that, you cannot move content from another server to this server directly over FTP and you have to be downloading the backup and upload to the new server but here we will directly pull the content from the server without actually downloading it.

If you are using cPanel or any other control panel for your site, generate a home directory and MySQL backup or a complete site backup. Once the backup is created, use file manager to move it to the public_html folder for public access. Don't worry as file name is not known to others, no one else can download that backup.

In the Shell access of Amazon EC2 Instance type the following command.

It will download the backup from your domain to the EC2 instance.

Note: If you are using a Micro instance, make sure the zip file is not bigger than the allowed disk space because cPanel files can become un-necessary big for having multiple backups lying in different folders can get included.

Note: As the download is complete, you can safely delete the backup file from your cPanel file manager or can move it back outside of public_html folder.

Next we will unzip and untar the files we have downloaded and then place the html file in the html folder and import the MySQL database.

gunzip backupfilename.tar.gz
tar -xvf backupfilename.tar
Depending on the backup file, the path to the files and database can vary. For cPanel complete backup, the public_html folder is a tar file named homedir.tar and MySQL database is inside the mysql folder.

Move the homedir.tar to your html folder assigned for this domain in Apache configuration and then untar the content.

mv homdir /var/www/folder_for_the_domain/html
tar -xvf homedir.tar

Step 4: Importing MySQL Database(s)

To Add a MySQL database and user for your site, just follow the following instructions.

Login to MySQL with root access and follow the steps below.

mysql -u root -p
1. Create User

CREATE USER 'site_user'@'localhost' IDENTIFIED BY 'strong_password';
2. Create Database

3. Grant all privileges to the user for the database created.

GRANT ALL PRIVILEGES ON `site_db`.* TO "site_user"@"localhost";
4. Flush the privileges to grant access of the user to database.

Now logout of MySQL command line tool and then login back again with the following command.

mysql -u site_user -p -D site_db

Enter MySQL password and then we will import the MySQL backups using the source command.

source /path/to/mysql/backup/file
This should import the mysql database backup into your site_db database. Now change your site's database connectivity to connect to the new database you just created.

Step 5: Finalizing DNS and NameServers for the Instance

We have imported, files and Database into our EC2 instance and finally we need to make sure our domain points to the Amazon Instance. First we have to grasp an Elastic IP and assign it to the Amazon EC2 instance we just created. Elastic IP address is a static IP associated with an AWS account and is independent to EC2 instances, so we can assign this IP to any EC2 instance as and when needed. The IP remains associated with the account until explicitly choose to release it.

In the EC2 console, Click Elastic IPs in the left navigation area and select the Elastic IP to associate it to the EC2 instance we just created. For the domain, assign the A record to point to the EIP. If you want to be using Nameservers, you can use a third party DNS service or use Amazon's Route 53.

A point to note is that EC2 Server is not able to send email as we don't have any mail program setup done. You can and should use Amazon's SES to get SMTP access details to send emails.