Amazon EC2 stands for Amazon Elastic Compute Cloud it is a Amazon web service (aws) that provides resizable compute capacity in the cloud. It will be really selfish of me not to share the benefits of this wonderful cloud platform. We are not only going to setup an EC2 Server instance but I will educate you on the benefits of this great cloud platform that comes almost free to developers, If you have been wasting your time with hosting companies this is your chance to save money and pay for just what you use. Amazon EC2 provides developers the tools to build failure resilient applications and isolate themselves from common failure scenarios.
After reading this amazon aws tutorial You should know How to setup Amazon EC2 Instance , Install Apache , PHP, Node.js MySQL, PHPMyAdmin and lastly we are going to setup a website on the Instance. The good part is that Amazon give you the chance to start using this wonderful platform free for 12 months with 1GB RAM.
Creating Amazon AWS Account
The first step is to login to register on Amazon AWS page, You will require a valid credit card and a reachable mobile number don't worry you will not be charged for the first 12 month they are just using your card detail for record purpose and for future billing. Goto Amazon AWS Hompage and sign up.Step 1: Setup EC2 Instance
On completing your registration, The next page will display Amazon AWS console HomePage as below: Click on EC2 under The Compute Category.Step 2: Lunch An Instance
Click the Lunch Instance button to create new EC2 instance.Step 3: Select An Operating System(OS)
For more advantages we are going to be using Ubuntu as the OS on our new instance, I should be listed under free tier.Step 4: Choose Server Type
We need to choose the server type this time we are to use the t2.micro : free tierStep 5: Create EC2 Instance Key File for Authentication
Skip all steps till the step 6 .Configure Security Group by clicking Next.Now here is where things get confidential , You key file serves as you login credential to you instance, without the key file you may not be able to login to your instance. What we are doing in this step is to generate a new key file for our new instance, If you already have a key file you can choose to use it but be sure you still have it, But in this tutorial I assume you are creating your first instance so let generate a new key file. Remember to to add rules for accepting connection from HTTP, SSH and HTTPS[optional] from anywhere on specified ports so that we can have access to the instance anywhere on the internet with web browser and terminals. Lastly click the Review and Lunch button You can review your setting to see if it correct then click the Lunch Button.
Download Key Pair
After clicking the lunch button a pop-in window should display for the option to download key pair, Name your key then click Download Key pair Button before clicking the Lunch Instances button. If you do not download the Key Pair you will not have access to your instance .Scroll to the bottom of the page and click the View Instances Button.
By now you should see the running status on your instance.
Create an Elastic IP Address
Click Elastic IP on the left menu under NETWORK & SECURITY . Click the Allocate New Address button.
Connect to our EC2 Instance from the Terminal
Secure Shell (SSH) is a network protocol for secure data communication, remote shell services or command execution and other secure network services.{wikipedia}Connect to EC2 Instance with Windows OS
If you are using Windows OS your browser should save it in your Download Folder by default if not you can move it there. You will most likely be using PuTTy for SSH commands. PuTTy uses the extension .ppk for its key files, while Amazon creates a .pem file. You will need to run this .pem file through the PuTTygen tool to use it.Lunch Puttygen -> File -> Load Private.
Key Under Type of key to generate select SSH-2 RSA then click the Load button.
By default PuTTYgen displays only files with the extension .ppk. To locate your .pem file File Types to All Files on the select dialog. Select your .pem file and click Open. Click OK to dismiss the selection dialog box. Click Save private key button.
Sometimes, PuTTYgen displays a warning about saving the key without a pass phrase. Click Yes.
Connect using PuTTY
Download PuTTY. We will be needing the .ppk file we saved earlier to complete the SSH connection.1. Start PuTTY (from the Start menu, click All Programs -> PuTTY).
2. In the Category pane, select Session and complete the following fields:
In the Host Name box, enter ubuntu@IP_ADDRESS.
Be sure to use your own IP address (The one generated above).
In the Category pane, expand Connection, expand SSH, and then select Auth.
Complete the following: Click Browse .
Select the .ppk {i.e myFirstInstance.ppk} file that was generate by PuTTYgen Earlier from our instance key pair and click Open.
Optional: If you plan to start this session again later, you can save the session information for future use.
Select Session in the Category tree, enter a name for the session in Saved Sessions, and then click Save.
Click Open to start the PuTTY session.
If this is the first time you have connected to this instance, PuTTY displays a security alert dialog box that asks whether you trust the host you are connecting to.
Click Yes A window opens and you are connected to your instance.
Connect to EC2 Instance with MAC OS X
If you are using MAC the pair key we downloaded (myFirstInstance.pem) is possibly in the Download folder you need to move it to the Home folder for easy access. after doing this, Open the Terminal Window (it can be found under Application in Utilities). Now type this command with the generate IP Address in the terminal. Remember to replace this IP address with your own generated IP address
ssh -i myFirstInstance.pem ubuntu@IP_ADDRESS
80% of the time you will get permission error on MAC such as this
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'myFirstInstance.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "myFirstInstance.pem": bad permissions
Permission denied (publickey).
Permissions
0640 for 'myFirstInstance.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "myFirstInstance.pem": bad permissions
Permission denied (publickey).
The solution is to Give only READ permission for key pair .pem file, Which is in our case myFirstIntsnace.pem using this code.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "myFirstInstance.pem": bad permissions
Permission denied (publickey).
sudo chmod 400 myFirstInstance.pem
To make sure we are working on a good environment let use this code to update Ubuntu OS Packages.
sudo apt-get update
Install Apache Server to EC2 Instance
First of all let update and upgrade. In the terminal type:
sudo apt-get update && sudo apt-get upgrade
Now let install Apache, its documentation, and a collection of utilities.
sudo apt-get install apache2 apache2-doc apache2-utils
Now you can test if your server is running by typing your IP_ADDRESS in the browser URL to see if "it works!".
Here is how it look like with the instance and IP address created for this tutorial.
Install PHP on EC2 Instance
To install the latest version of PHP PHP7 for faster performance use the command below.
sudo apt-get install python-software-properties
sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
sudo apt-get update
Then we will now install PHP7:
sudo LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.0 php7.0-fpm php7.0-mysql -y
Optionally clean up unneeded packages afterwards:
sudo apt-get --purge autoremove -y
To check the PHP version you can type:
php -v
Install Node.Js on EC2 Instance
You should know that if you only want to run Node.js code on your instance and not PHP there is no reason to install PHP and Apache. Node comes with it own server. To install node to our EC2 instance :
sudo apt-get install nodejs
Install Node Package Manager:
sudo apt-get install npm
Create a symbolic link for node, as many Node.js tools use this name to execute.
sudo ln -s /usr/bin/nodejs /usr/bin/node
Now let check the installed version:
$ node -v
v0.10.25
$ npm -v
1.3.10
v0.10.25
$ npm -v
1.3.10
Install MySQL on EC2 Instance
To install MySQL on our instance we make use of this command:
sudo apt-get install mysql-server
MySQL terminal will ask you to choose the root user password. Remember to choose a password that you can remember and make sure it a safe password.
mysql --version
Install PHPMyAdmin on EC2 Instance
To install PHPMyAdmin input this command into the terminal.
sudo apt-get install phpmyadmin
You will be prompted to choose a sever, Apache is chosen as default you can just press the Enter Key to proceed.
sudo vim /etc/apache2/apache2.conf
Add the following to the bottom of the file:hint: press i or insert button to enable edit then press the Esc button to stop edit.
Press :wq and then press enter to save.
# phpMyAdmin Configuration
Include /etc/phpmyadmin/apache.conf
Then Restart apache
Include /etc/phpmyadmin/apache.conf
sudo service apache2 restart
Now navigate to you server IP_ADDRESS/phpmyadmin using your browser
just like mine below:
Setup Website on Amazon aws EC2 Instance
Before we can setup a website on our instance we need to have access to our files directory to do that we are going to use an FTP software. for this tutorial we are using Filezilla Client. Lunch Filezilla after completing the installation.Edit (Preferences) -> Settings-> Connection -> SFTP, Click Add key file.
Goto Files->Site Manager-> New Site in the host field enter your IP_ADDRESS leave the port and password blank.
Setting up Domain for EC2 Instance
We can not hand out ugly IP_ADDRESS to visitors before they can have access to our instance on the web, We need to purchase a Domain name and connect it with our instance. Read: How to buy and Setup a Custom Domain Through Blogger in 2 minutes It should give you some information on how to setup a domainNow I will assume you have already registered a domain name, go to domain DNS(Domain name settings). Add host value @ points to IP_ADDRESS.
Thanks!
Your feedback helps us improve tutorials.
very nice and amazing post, thanks for the sharing. plz continue this good work
ReplyDeleteI appreciate the thumbs up, Thanks.
Delete