Tuesday, January 6, 2015

How to migrate your PHP website to Amazon EC2

Amazon Elastic Compute Cloud (Amazon EC2) and is one of the most popular hosting solutions nowadays. Using EC2's virtual servers you don't need to invest in hardware up front, and you can easily manage performance, networking and security of your applications.

The AWS Free Tier provides the following free services for 12 months, and with a competitive price afterwards:
  • 750 hours per month of Linux, RHEL, or SLES t2.micro instance usage
  • 750 hours per month of Windows t2.micro instance usage
  • Run one instance at a time or multiple instances simultaneously
In this post I will explain how can you easily migrate your application from your local server to this high-performance cloud solution.

1) Sign up.
Sign up for free on Amazon Web Services. Credit card information is required, but you won't be charged during the first 12 months.

2) Set up EC2.
On your Amazon Management Console, apply for a EC2 account following these instructions. Keep the resulting key_pair.pem file somewhere safe. Make sure that you have set the security group with inbound rules for SSH, HTTP and HTTPS (see figure below).

3) Launch your instance. On your EC2 dashboard select the option Launch Instance and follow these instructions. For a PHP website I recommend choosing the Amazon Linux  t2.micro. Remember to connect your instance with the key_pair.pem file that you had generated before, and select the security group that you created on step 2.

4) Connect to your instance. From Linux or MAC just type the following command on your terminal:
$ ssh -i /path/key_pair.pem ec2-user@public_dns_name
Replace the elements in red with the path to your key pair and the public DNS name that you can find on your EC2 instances dashboard. For example:
$ ssh -i /home/arturo/Docs/arturo_key_pair.pem ec2-user@ec2-14-129-140-123.us-west-2.compute.amazonaws.com 
You will be granted access to your brand new Amazon Linux machine.

5) Install the Server and Database. In our case we are going to use PHP with a MySQL database. If you need to install any other package, such as Node.js, you can use the command sudo yum install package_to_install. In order to set up our web server and database, just follow the steps from 2 to 5 of this tutorial.

6) Test the web server. If you type your public DNS on your browser (in our example http://ec2-14-129-140-123.us-west-2.compute.amazonaws.com) you will see the Apache test page. Your web server is running!

7) Copy your files. You just need to copy your PHP project in /var/www/html. That's it!


No comments:

Post a Comment