Amazon aws EC2 'Elastic Cloud' Setup with Ubuntu, PHP, Apache, Node.js, MySQL and PHPMyAdmin Installation ~ Hybrid Mobile Apps Development, React Native, Flutter, JavaScript, Darts, iOS, Android, NodeJS
Coding Savvy FB Twitter Google
» »

Amazon aws EC2 'Elastic Cloud' Setup with Ubuntu, PHP, Apache, Node.js, MySQL and PHPMyAdmin Installation

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.
Amazon aws EC2 'Elastic Cloud' Setup with Ubuntu, PHP, Apache and Node.js Installation

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.
Amazon aws EC2 'Elastic Cloud' Hompage

Step 2: Lunch An Instance

Click the Lunch Instance button to create new EC2 instance.
Lunch 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.
Ubuntu OS on EC2 Instance

Step 4: Choose Server Type

We need to choose the server type this time we are to use the t2.micro : free tier
t2.micro free tier

Step 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.
Ec2 security group setup

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 .
Download Key Pair
Then let the magic happen in the background:
Installing instance
Installation completed

Scroll to the bottom of the page and click the View Instances Button.
By now you should see the running status on your instance.
Installation completed
Now that the creation of our EC2 instance is completed, We need to create and Elastic IP so that we can connect to our instance, Amazon Generates Elastic IP address randomly so let do it.

Create an Elastic IP Address


Click Elastic IP on the left menu under NETWORK & SECURITY . Click the Allocate New Address button.
Create an Elastic IP
an Elastic IP
This newly allocated IP address will not automatically connect to our instance, we need to associate them together to have a perfect connection to the right instance. To do this mark the newly generate Elastic IP and click Associate Address on the Action Menu Drop Down.
Associate an Elastic IP
Immediately you click on the instance field you should see all the available instances by their ID, You have to click the ID of your new instance so that it can be selected and then click the Associate Button . And we are good to go.

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).
or it might be
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.
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 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

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 choose password
We can check our MySQL version with
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.
phpmyadmin  server selection
At the second screen, which asks “configure the database for phpmyadmin with dbconfig-common?”, select Yes, then hit enter to continue.
phpmyadmin  server selection
At the third screen enter your MySQL password, then hit enter to continue.
phpmyadmin  password selection
And finally at the fourth screen set the password you’ll use to log into phpmyadmin, hit enter to continue, and confirm your password.
phpmyadmin  password confirm
To finalize the installation of PHPMyAdmin we need to make apache load it configuration file. Let edit Apache configuration file through the vim editor. using this code
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
sudo service apache2 restart
Now navigate to you server IP_ADDRESS/phpmyadmin using your browser just like mine below:
phpmyadmin display
After login using the password set during installation of PHPMyAdmin:
phpmyadmin interface

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.
Filezilla Setting
Browse to the location of your .pem or .ppk file and select it. i.e myFirstInstance.pem or the file we generate using PuTTYgen myFirstInstance.ppk Note: If you are selecting a .pem file a dialog box will appear asking your permission to convert the file into ppk format. Click Yes then give the file a name and store it somewhere. If the new file is shown in the list of Keyfiles, then continue to the next step. If not, then click Add keyfile... and select the newly converted file.

Filezilla Setting
Filezilla Setting
Now to create profile for our instance on Filezilla:
Goto Files->Site Manager-> New Site in the host field enter your IP_ADDRESS leave the port and password blank.
Filezilla Setting
In the Logon type set it to Normal on the protocol select SFTP - SSH File Transfer Protocol set the username as ubuntu and leave the password and port field blank. Click Connect Button - If saving of passwords has been disabled, you will be prompted that the logon type will be changed to Ask for password. Say OK and when connecting, at the password prompt push OK without entering a password to proceed past the dialog.
Filezilla Setting
Filezilla Setting
Now Your should have access to your instance files.
GoDaddy Setting
Uploading Files to EC2 Instance After login you will find yourself in the ubuntu directory you need to go up the directory tree twice to get to the root folder Navigate into the var directory -> www -> html. The html directory is your server public directory.
Setting

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 domain
Now I will assume you have already registered a domain name, go to domain DNS(Domain name settings). Add host value @ points to IP_ADDRESS.
GoDaddy Setting
Now visit your domain, Your Amazon instance will show up.
Was this article helpful?
Thanks! Your feedback helps us improve tutorials.

You May Also Like...

2 comments:

  1. very nice and amazing post, thanks for the sharing. plz continue this good work

    ReplyDelete