본문 바로가기
[AWS]/Highly Available, Scalable, AWS Stack

5. Deploy app on an EC2 instance

by SAMSUNG CLOUD-OKY 2022. 1. 17.
반응형

 

## run these after you SSH into the server

# clone the app
git clone https://savitras@bitbucket.org/savitras/aws-course-php-app.git

# change to app folder
cd aws-course-php-app/

# install app dependencies
sh apt-get.sh

# install composer
curl -sS https://getcomposer.org/installer | php

# install AWS SDK
php composer.phar install

# deploy php pages to apache document root
sudo cp *.php /var/www/html/

# deploy vendor folder (contains AWS SDK) to the apache doc root
sudo cp -r vendor/ /var/www/html/

# deploy image to apache doc root
sudo cp aws-icon.png /var/www/html/

# IMPORTANT: change 'region' in dyn_session.php to the region you want to use, if different from us-east-1. This will help you later in the scalability module
sudo vi /var/www/html/dyn_session.php

// Setup region where your DynamoDB table is setup, e.g. us-east-1
$dynamoDb = DynamoDbClient::factory(array('region' => 'us-east-1',

 

 

All right so in this video let's take a look at how to deploy the PHP application.

On this EC2 instance that we have.

Right.

And we have to run through a series of steps starting with git clone

we'll use git the version control system and will clone the application from a public repository and then

copy the application onto the EC2 instance.

Right.

After that we'll have to run through these two steps where we will install the dependencies of this application

and the dependencies include stuff like PHP or apache web server and the mysql client as well so

that the application can talk to the.

MySQL server

right.

And we install these using apt get which is Ubuntu's package manager and we can run these installation

commands manually but I have included a shell script in the application and we can just run this script

and this will install all the dependencies automatically.

Right.

But separately we'll install the AWS SDK and the SDK is a.

Is a library right.

It's a library published by AWS.

And this will help our application to talk to services such as DynamoDB and CloudFront.

So I want to show this to you separately.

That's why I've not included this in the shellscript and what we will do is we will use composer which is

a PHP package manager to install the AWS SDK right.

Later you might be using some other platform say Java dot net you will have to install the SDK in a different way

there.

But here for PHP we use composer.

Once we do this then we can deploy the application.

And by that we mean moving the application pages into the Apache document root.

So Apache has a document root from where it serves our application pages right.

So we simply move the pages into the Apache document root and in Ubuntu to the document root is /var

/www

/html So we need to move the pages into this folder.

And then our application will be server by about apache.

And finally we can then verify the application like we need to make sure that all the the deployment

the dependencies of the of the PHP page have been that have been installed successfully.

But we have to verify and for verification we will visit the public IP address

So this server will have a public IP address and we can use the browser.

So when we use the browser and visit the public IP like this right and then followed by the PHP page

and we can look at a bunch of pages starting with the index dot php page.

This is the home page.

This this will show a Hello World message and it will also look at the session dot PHP.

This is an important part of our application because we have to see how to how to best implement sessions

in AWS

This is something important.

We'll also take a look at image.

dot PHP and this is a page that will display an image which is stored locally and later we'll see how

to use other services such as S3 and CloudFront to display this image.

Right.

And we'll also look at another page called local DB dot php and this may not work right away

because we won't have a MySQL server at this at this stage but we can still look at this page as well.

So this is that what we want to do in this video.

So quickly let's take a look at how to implement how to deploy the application.

All right so let's let's deploy the application now.

So here is the EC2 instance right.

And let me S-sh into the instance now.

And the first thing we do is clone.

So I have for some notes here you'll find this with your course content.

Right.

And we will be copying be cloning the application from a public git repository like using the git clone

command

And this is the URL of the repository.

Right.

And you'll find that once this command finishes it will create a folder like this and we can change into

to this folder and take a look at our PHP application.

Great.

And after this we have to run this shellscript.

You can see what it does is it installs.

It uses apt get the package manager and installs dependencies of this application such as PHP and

Apache and also my sql client and some other some other dependencies as well that are that are required.

OK.

So you can run these manually but let's just run this script because it will do everything automatically

for us and all you have to do is say sh and apt get dot sh the file name.

Right.

So ssh dot .

the file name and this will this will run through all those commands one by one and install all the

dependencies and this may take a while

And now in the meantime let's see what what we have to do next.

Right.

So.

So the apt get shell script is running.

Next we install the AWS SDK using composer composer is a PHP package manager.

And for this we first need to install composer and we use this command.

We use curl essentially and install composer and remember to do this in the application folder by

the way you have to make sure that you change in the application folder and then and then run

these commands.

OK.

So we are waiting for the shellscript to finish any moment now and then and then when.

OK so let me clear this is done and let me paste this command which will install composer.

And after that we will use this command to install the AWS SDK.

Right.

And this command what it does it it looks at this composer.json file, which is a configuration

file.

And in this file you can see we are trying to install the AWS SDK and this is the version 3.3 9 which

which is at this time the latest version.

So this command that I'm running right now.

will use that composer.json file

and install the AWS SDK into this particular folder

Right.

So.

So that's why it's important that you run this command from within the folder the application

folder.

OK.

And if you look at the folder now you'll see there's a new folder called vendor and this vendor folder that

has been created by this previous command and this folder will contain the AWS SDK

So.

So that is done.

And next what you need to do is you need to deploy.

I mean to deploy the application which means move the application into the document root.

And we can do that using copy.

So first let me copy the PHP

applications

Right.

And remember to use Sudo because we don't have permissions the ubuntu user does not have permissions

in the in the apache document root.

So we have to do it using root and we can copy all the PHP pages into apache document root which

is /var/www/html/

That's done.

Let's also copy the vendor folder.

And for that because this is a folder we will use the minus r option of copy and copy this also into the

into the the Apache document root.

And finally also copy the PNG image here.

This is an image we will use in one of our one of our PHP pages and this page will display the image

on the browser.

OK so to run these three commands and this will complete the deployment of the application in the Apache

document root.

All right.

So now we can verify so what we have to do is use the public IP address.

This is the public IP address.

Right.

And from the browser window visit index.php

This is the home page and make sure that the page displays Hello world.

if it is not then something is wrong with the PHP side of things after that we

verify a few other pages.

The first one is this one.

This is a session page and here first you'll be shown a form once you enter your name it should show.

welcome message.

This means a session has been created and if you refresh this page you will see that it displays the

message instead of the form which means it is recognising that there's a session that has been created.

OK.

So that's one next let's also look at this one image in the EBS volume this is the image that we

copied into the Apache document root

And this page will show us the AWS icon image and and a little later will see how to implement

images better using S3.

OK.

Also look at this one.

This is the local database page where this page would expect a mysql server in the same instance.

But we don't have it yet.

And if it says failed to connect that's fine that's fine.

At this stage.

Right but at least what we've done is we verified that our pages are being server right these three pages are

being served and two of them are working.

And the last one we will we will see in the next one to.

We will be able to install on my sql server and make it work in the next module as well.

OK.

So so this completes this video.

What we have done is we have uploaded the application to install the dependencies using apt get the

shell script then we have separately installed the AWS SDK using composer.

I remember the vendor folder that was created in the application folder.

Then we deployed the application which means we moved several things the PHP pages the vendor folder and also

the image into the Apache document root which is /var.

/www/html and then we verified using the public IP address and we looked at the home

page.

the hello world page and the session page the image page and the localDB page

And if the local db page does not work that's fine

That's fine.

At this stage.

Once you've done all this you can then create the AMI rights and the next steps are going to be creating

the AMI of this instance.

Right.

This is the AMI by the way.

And all you have to do is from the instance actions right on this instance from the actions you can

create this AMI and this AMI will have all the work that we have done in this video deploying the application installing

the dependencies.

All those things can be baked into the AMI and later any time in this course when you're practicing

right.

You can use the AMI to create a server like this which will have our PHP application deployed on it.

OK.

So good luck with this work.

I'll see you.

I'll see you in the next video.

 

 

 

 

반응형

댓글