You are on page 1of 64

Steps to upload Website to AWS-EC2

There are 3 main parts to look at to get this working.

1. Creation z
of the AWS-EC2 environment
2. Setup of the AWS-EC2 LAMP Stack environment
3. Setup of the Filezilla/FTP envornment

1
Part-1 Creation of the AWS-EC2 Instance

1. Logon to AWS as you normaly would


2. Select AWS console
3. Create EC2 istance
4. Select the EC2 Instance type/LAMP stack
5. z the security group
Setup
6. Launch the EC2 instance
7. Preview the EC2 instance details

2
Part-1 Logon to AWS as you normaly would

https://labs.vocareum.com/home/login.php

z
login to your AWS
account viia vocareum

3
Part-1 Select AWS console

Then click/select the AWS Console tab

4
Part-1 Select EC2

You will see the AWS Management Console


where you can select any of AWS Srevices you
want to use.

For this exercise we are going to select the AWS


EC2 service. Select/click on it

5
Part-1 Launc/create EC2 instance

When you select EC2 you will see the above screen. We are going
create our first instance. Select/click the Launch instance tab 6
Part-1 Select EC2 instance Type

A set up wizard should begin in which the user selects an Amazon


Machine Image or AMI and then configures it.

First, Step 1 allows the user to select the image. Choose the option
titled Amazon Linux AMI 2018.03.0 (HVM), SSD Volume Type — ami-
6b8cef13. It should be covered under the free tier choices.

7
Part-1 Select EC2 instance Type

Next, Step 2 prompts the user to choose an Instance


Type. Choose the version titled “t2.micro” under “Type”
column. It should have the subtext stating it is “Free tier
eligible”. It may be selected by default.

Once the Instance Type is selected, click “Next:


Configure Instance Details” down in the bottom
right of the wizard.

8
Part-1 Select EC2 instance Type

Step 3 screen you leave these setting as default


and skip Step 4 and Step 5 by clicking the Next button
down at the bottom right of the page, OR go directly
by selecting Step 6. Configure Security Group.

9
Part-1 Configure EC2 Security Group

From here we need to open some ports for our


LAMP web server. See next slide. Now there is only
one port being defined which is the SSH port.

10
Part-1 Configure EC2 Security Group

z
1. The SSH rule should be added by default, if not, select SSH from the
drop-down menu under Type, then under the source column, choose
“Anywhere”.
2. Next, choose add rule, then select HTTP from the Type drop-down
menu and again “Anywhere” under the source column. Check that your security group
3. After that, select Add Rule again, and this time chose HTTPS from the configuration looks similar to the
Type drop-down menu and again choose “Anywhere” as the Source. screenshot above before
4. Finally, add the last rule by choosing MYSQL/AURORA from the Type choosing “Review and Launch”.
drop-down menu and “Anywhere” under source.
11
Part-1 Configure EC2 Security Group

If you hit an error as shown above. It appears you You need to delete the entry and
were trying to save the entry with a blank source re-create with by choosing
field. MYSQL/AURORA from the Type
drop-down menu and “Anywhere”
You will still have to put in a valid source in the under source and set to 0.0.0.0/0
source field. If you want it to apply to the "world" just
insert 0.0.0.0/0. Then choose Review and Launch at
The bottom of the screen. 12
Part-1 Preview EC2 Instance Details

On the this screen review your security


group one last time and ensure it matches
the settings outlined previous screen.
z
Once everything looks fine choose Launch.
Once “Launch” is selected, the user is
prompted to create a new Key Pair group.

13
Part-1 Launch EC2 Instance
Choose “Create a new key pair”. Under “Key Pair Name” type in a
name of your choosing, then click the “Download Key Pair” button.

You will be then prompted to save this key. Make sure to save this
somewhere safe, but also somewhere you will remember.

Amazon does not allow users to log into machines via a username and
passwords, instead you will use keys.

So its important not to to misplace or lose the key. Once that is


z completed and your key is saved, select launch instance.
Create new key pair

Type the name you want to call your .pem key. I


have named mine as shown

Download the key and save in your local


computer in a folder you will remember

Once you have saved the key then


choose Lauch instances 14
Part-1 Launch EC2 Instance

The instance can take a while to configure.


You will see a success message once it is
done.

Once its finished select View Instances from


the bottom right to view your own instances and
some of their configurations

15
Part-1 Preview EC2 Instance Details

At the moment their should only be one instance present. It make take
a few minutes for your machine to instantiate, when its ready to go, it will
display a green dot beside the word running under the “Instance State”
column.

z Wait till you see these two green icons

This is your domain or


URL to you AWS EC2

You have now completed Part-1 in


creating the EC2 Instance successfully

16
Part-1 Start/Stop EC2 Instances
z

PS: you stop instance once your website has been marked and completed.

Once you no longer want the EC2 instances to be active you can stop the instance - this suspends
the running of the instance and will save your credit points from being depleted.

To start the EC2 instance just choose from the Action tab Start and it will start the instance.

If you choose Terminate – the system will delete the whole EC2 instance and if you want to use
again you will have re-create the whole thing.

17
Part-2 we will now try and connect to the EC2 instance just created via SSH connection using
the .pem key we created earlier. Don’t loose the key otherwise you will not able to connect to EC2

Now that you have launched your EC2 instance, you can connect to it and use it the way that you'd use a
computer sitting in front of you.

There are 3 ways to connect to the EC2 instance. The next slides will describe the first two ways of
zto a Linux/EC2 instance:
connecting

1. Connect from Windows using PuTTY – for Windows laptops


2. Connect from Mac/Linux bash using an SSH Client – for Mac laptops
3. Connect Using Your Browser – will skip this

The reason you need to connect to the EC2 instance is to setup the environment for hosting a website.
We need to install Apache, MySql, PHP etc (LAMP stack) so that it will support the application that will
be hosted by EC2. Take note you only need to these steps once for every new instance you create
18
Before we proceed we zneed
to get some information that
we will be using later to
connect. Just take note where
this information can be
obtained so that you now
where to find it. Take note of the URL address

This command will be used to


Go to the View Instance connect to the EC2 instance later
screen as shown below and If you are using the Mac to
choose the Connect tab connnect

19
z

Part-2 From this slide onwards the steps are meant for
windows laptops. If you have a Mac laptop please go to
slide no-35

20
Part-2 Establishing Connection to EC2 via PuTTY - Windows

1. Start terminal mode in using putty


2. Locate directory where your Key (.pem) is stored
3. Convert .pem key to .ppk format key
4. Establish connection via SSH to EC2 via putty
z
5. Update the software in current system
6. Install Apache, PHP and MySql
7. Set the file and folder permission
8. Test if envornment is set correctly

21
https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

z You need to download PuTTY from the internet on


to you laptop and install the software. The url is
given above

1 Select this link to download the excutetable file and


use it install on your laptop

22
PuTTY does not natively support the private key (.pem) format for SSH keys. PuTTY provides a tool named PuTTYgen, which converts
keys to the required format for PuTTY.

You must convert your private key (.pem file) into this format (.ppk file) as follows in order to connect to your instance using PuTTY.

We will create a .ppk fiile WITHOUT any passphrase for simplicity in this exercise.

From the Windows Start menu, choose All


2 Programs, PuTTY, PuTTYgen.

Launch PuTTY Key Generator (PuTTYgen)


You will see screen as shown on your left
z
3 Ensure RSA option is selected

4 Next select Load and a pop explorer will appear for you to locate
the .pem (key) you saved when it was generated during the creation
of EC2 istance.

Note: when browsing for your .pem file be sure to select All Files in
the dropdown list that is located to the right of the File name field.
PuTTYgen will then load and convert your file.

23
Once you load the .pem file a pop up window will appear, asking you to
save the private key.

Click ok and click save private key button. Save the .ppk file in the
same folder where you kept the .pem file.
5
z 6

Note: Do not click generate key or any other buttons. Just


load the pem file and click save the private key button.

24
Once you load the .pem file a pop up window will appear, asking you
to save the private key

You will receive a warning message asking if you want to save


7 this key without a passphrase. Be sure to select Yes.

z
You will now be asked where you want to save the .ppk file.
Save in the same folder as where you stored your .pem key file.

25
z
Next we launch PuTTY and provide the necessary
input to connect to AWS EC2 instance you just
created.
8 9
Insert the host name which is your AWS EC2
url which you can get from the EC2 dashboard
and select Connect tab

10

copy this line and paste it in the host box

26
z
11

Next select SSH on the left panel and


expand the list. Then select Auth

12

Then select Browse and locate the converted


.pem key to ppk key and insert the path to the
Remember each time you need to connect to ppk key here
EC2 via Putty you need to repeat slide 26
and slide 27 to establish connection

Select Open tab to


establish the connection
27
13 Once successful connection you see a login
z as prompt: type ec2-user and <enter>

You then see the logo as shown below

14
To ensure that all of your software packages are up to date,
perform a quick software update on your instance.

This process may take a few minutes, but it is important to


make sure that you have the latest security updates and bug
fixes. Tyep the following command – sudo yum update -y
28
z
You will see the Complete statement
displayed from the update

15
Now that your instance is current, you can install the Apache web server,
MySQL, and PHP software packages. Issue the following command

sudo yum install -y httpd24 php70 mysql56-server php70-mysqlnd

If you want the latest of version of Php7.2 issue the following command

sudo yum install -y httpd24 php72 mysql57-server php72-mysqlnd

29
z

You will see the Complete statement displayed


from the installation of Apache, Php, and MySQL

16
Next we start the Apache web service. Issue the following
command

sudo service httpd start

You will see the OK status once it has started correctly 30


z To allow the ec2-user account to manipulate files in this directory, you must
modify the ownership and permissions of the directory. There are many
ways to accomplish this task.

Add your user (in this case, ec2-user) to the apache group, to give
17 the apache group ownership of the /var/www directory and assign write
permissions to the group. Issue the following command

sudo usermod -a -G apache ec2-user

Log out and then log back in again to pick up the new group, and then verify
your membership.
Log out of PuTTY (use the exit command or close the terminal window): issue
exit
18

31
19
You establish connection again via PuTTY and repeat slides 26 and 27
z Then when you the login prompt type ec2-user again and enter

Change the group ownership of /var/www and its contents to


the apache group.

sudo chown -R ec2-user:apache /var/www

20

21 To add group write permissions and to set the group ID on future


subdirectories, change the directory permissions of /var/www and its
subdirectories.

Issue: sudo chmod 2775 /var/www


22 Then: cd /var/www
ls –li

32
z
To add group write permissions and to set the group ID on future
subdirectories, change the directory permissions of /var/www and its
subdirectories.

Issue: find /var/www -type d -exec sudo chmod 2775 {} \;

23

To add group write permissions, recursively change the file


24 permissions of /var/www and its subdirectories:

Issue: find /var/www -type f -exec sudo chmod 0664 {} \;


25
Logon to a super-user root to issue the next command

Issue: sudo su
26

Configure the web Apache/server to start with each system


boot/startup

Issue: chkconfig httpd on

33
Now, the ec2-user user (and any future members of the apache group) can add, delete, and edit files in the
Apache document root. Now you are ready to add content, such as a static website or a PHP application.

z
Test your LAMP server
Create a PHP file in the Apache document root (/var/www/hmtl)

Issue: echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

Then : cd html

Followed by ls –li and you will see phpinfo.php created for you
27

Once you have seen the above page being displayed you can now
28
29 delete the phpinfo.php file.

Issue: rm /var/www/html/phpinfo.php

You are ready to go to Part-3 – where you will upload your


webfiles to EC2. ---horray! job well done if you got this far.
Only the last bit left to setup Part-3
In a web browser, type the URL of the file that you just created. This
URL is the public DNS address of your instance followed by a forward You do not need to do Part-1 and Part-2 ever again. You
slash and the file name. For example: 30 only need to focus how to do Part-3

http://ec2-18-212-57-160.compute-1.amazonaws.com/phpinfo.php

You should see the PHP information page as


shown on the right: Please proceed to Part-3 Goto Slide-44
34
z

Part-2 From this slide onwards the steps are meant for
Mac laptops. If you have a windows laptop go to slide 20

35
Part-2 Establishing Connection to EC2 via SSH - Mac

1. Start terminal mode in Mac


2. Locate directory where your Key (.pem) is stored
3. Establish connection via SSH to EC2
4. Update the software in current system
5. z
Install Apache, PHP and MySql
6. Set the file and folder permission
7. Test if envornment is set correctly

36
z
In the Mac launch your terminal icon to get into
terminal mode and you will see screen on your right
1
Issue ls command and you will see files
of the current folder you are in

Take note the key file should only have .pem extension and
not .pem.txt – if this is the case rename and remove .txt

2 3
Next cd to the directory where your key was stored earlier. In my
case it was on the desktop and my key is MyAWS-ECS2-Key2.perm
5
Then chmod 400 MyAWS-ECS2-Key2.pem on the key – change the
Type”yes” here and enter
permission of the key. Your key filename wll be different. 4
Next we will connect to EC2 via ssh command (your ip address will be different)
ssh -i "MyAWS-ECS2-Key2.pem" ec2-18-212-57-160.compute-1.amazonaws.com
37
z

Once you have successfully logon via


6
SSH you will see the above logo on your Issue a pwd to see in
sceen which directory are you in 7

To ensure that all of your software packages are up to date, perform a quick
software update on your instance.

This process may take a few minutes, but it is important to make sure that you have
the latest security updates and bug fixes. – sudo yum update -y

38
z

Next we start the Apache web service. Issue the following command

sudo service httpd start


Now that your instance is current, you can install the Apache web server,
MySQL, and PHP software packages. Issue the following command
Unfortunately httpd unrecognised means we have to install httpd
again
sudo yum install -y httpd24 php70 mysql56-server php70-mysqlnd

If you want the latest of version of Php7.2 issue the following command

sudo yum install -y httpd24 php72 mysql57-server php72-mysqlnd

39
z

11
10

We install httpd again by issuing the command below. If you did not Once installed we need to start http service by the following command
encounter error at step 9 then you can skip steps 10 and 11
sudo service httpd start
sudo yum install httpd or
sudo yum install httpd24

40
Apache httpd serves files that are kept in a directory called the Apache Log out and then log back in again to pick up the new group, and then verify
document root. The Amazon Linux Apache document root is /var/www/html, your membership.
which by default is owned by zroot. This is where you must store all your webfiles Log out (use the exit command or close the terminal window): issue
later. exit

You can see the owner of html as root when you issue the following commands:
15

pwd followed by
ls –li /var/www 12

13
Then log in again to pick up the new group. To logon in again issue this command

ssh -i "MyAWS-ECS2-Key2.pem" ec2-18-212-57-160.compute-1.amazonaws.com

16

To allow the ec2-user account to manipulate files in this directory, you must
modify the ownership and permissions of the directory. There are many
ways to accomplish this task.

Add your user (in this case, ec2-user) to the apache group, to give
the apache group ownership of the /var/www directory and assign write
permissions to the group. Issue the following command

sudo usermod -a -G apache ec2-user


14

41
Change the group ownership of /var/www and its contents to
the apache group. To add group write permissions and to set the group ID on future
subdirectories, change the directory permissions of /var/www and its
z
sudo chown -R ec2-user:apache /var/www subdirectories.
Then issue ls -li
Issue: sudo chmod 2775 /var/www 21

Then: find /var/www -type d -exec sudo chmod 2775 {} \;


17 22
18
To add group write permissions, recursively change the file
permissions of /var/www and its subdirectories:

Issue: find /var/www -type f -exec sudo chmod 0664 {} \;

Then: ls –li
23

19
20

Logon to a super-user root to issue the next command

Issue: sudo su
Configure the web Apache/server to start with each
system boot/startup

Issue: chkconfig httpd on 42


Now, the ec2-user user (and any future members of the apache group) can add, delete, and edit files in the
Apache document root. Now you are ready to add content, such as a static website or a PHP application.
z

Test your LAMP server


Create a PHP file in the Apache document root (/var/www/hmtl)

Issue: echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

Then : cd /var/www/html 24
Followed by ls –li and you will see phpinfo.php created for you 26

25

Once you have seen the above page being displayed you can now
delete the phpinfo.php file. 27
Issue: rm /var/www/html/phpinfo.php

In a web browser, type the URL of the file that you just created. This You are ready to go to Part-3 – where you will upload your
URL is the public DNS address of your instance followed by a forward webfiles to EC2. ---horray! job well done if you got this far.
slash and the file name. For example: Only the last bit left to setup Part-3
http://ec2-18-212-57-160.compute-1.amazonaws.com/phpinfo.php You do not need to do Part-1 and Part-2 ever again. You
only need to focus how to do Part-3

You should see the PHP information page as


shown on the right:
Please proceed to Part-3 Goto Slide-44 43
z

Part-3 From this slide onwards the steps are meant for
windows and Mac laptops.

You will be using these steps frequently when you upload


your website files to the EC2-instance’s target folder
/var/www/html

44
Part-3 Uploading files/folder via Filezilla to EC2

1. Install Filezilla on the Mac/or Windows


2. Create a New site profile
3. Establish connection to EC2
4. Locate local websfiles to be uplaoded
5. z
Prepare remote target destination at EC2
6. Upload files and check
7. The test from browser
8. Disconnect FTP link to EC2

45
Part-3 Download and Install FileZilla
z 1 Go to https://filezilla-project.org and
download the FileZilla Client for your laptop

Select this as we want client FTP and the it will detect automatically
2 whether you are downloading to Windows or for Mac

Downlaod the software and install it on your laptop

46
Part-3 Launch FileZilla
z
We will now setup the connection profile to EC2 via SSH so that we
need not type this each time when we want to connect to EC2.

3 Launch FileZilla from your laptop

4
You will see screen as shown. Then
select File and choose Site Manager.

47
Part-3 Create New Site Profile
z
z
For
Forthe
theHost
Hostpaste
pastethe
theEC2
EC2URL
URLaddress
addresswhich
whichlooks
lookslike
likesomething
somethingllike
llike
ec2-18-212-57-160.compute-1.amazonaws.com
ec2-18-212-57-160.compute-1.amazonaws.comyours
yourswill
willbe
bedifferent.
different.

99
88 For
Forthe
theProtocol:
Protocol:Select
SelectSFTP
SFTP
here
herefrom
fromdrop
dropdown
down

For
ForLogon
LogonType:
Type:Select
SelectKey
Keyfile
file
10
10 here
herefrom
fromdrop
dropdown
down
Type
TypeAWS-EC2
AWS-EC2here
here
For
ForUser:
User:Type
Typedefault
defaultusername
username
77 11
11 for
forEC2
EC2which
whichisisec2-user
ec2-user

12
12
For
ForKey
KeyFile:
File:Select
SelectBrowse
Browseand
andlocate
locate
On
Onyour
yourSite
SiteManager
Managerpanel
panelwe
wewill
willcreate
createaaprofile
profilefile
file where
whereyou
youstored
storedyou
youkey
keypair
pair(.pem
(.pemfile)
file)
and
andcall
callititAWS-EC2.
AWS-EC2.I Ihave
haveseveral
severalprofiles
profilesyou
youwill
willhave
have when
whenyou
youcreated
createdyour
yourEC2
EC2instance
instanceearlier
earlier
just
justone
one(first
(firsttime).
time).Select
SelectNew
NewSite
Site 13
13
66 Type
Typeaadescription
descriptionififyou
youwish
wishhere
here--optional
optional

14
14
Select
SelectOK
OK to
tosave
savethe
theprofile
profile 48
Part-3 Connect to EC2
z

17
Then select/highlight
AWS-EC2

15 16

Go to select File and choose Site


Manager again

Then select Connect


18

49
Part-3 Connect to EC2
z


19 For this pop-up select the box
to always trust this host and add 20
this key to cache
Select OK

50
Part-3 View folders after connection
z

On successful connection you will see


this message as shown on the left

These panels will show which folder you are


connected to at AWS-EC2 - which will be your
default home directory - /home/ec2-user
These panels will show which folder you
are in your local laptop (same as file
explorer)

This panel will show you the status and progress of every file
upload you do from your desktop to AWS-EC2

51
Part-3 Set Target Folder to Receive Files/Folders
z

When you want to upload to AWS-EC2 you must


deposit your files in only ONE folder which is
the /var/www/html folder

21

You now navigate by selecting the root ‘/’; followed


by var followed by www followed by html. You MUST
see Remote site pointing to /var/www/html

52
Part-3 Locate your Webfiles Loacally
z

Next you must locate where you kept all your website
files in your local laptop/system.

Ensure that your home page file is called index.html

22 Navigate to the folder where you have your


website files. Mine will be different from yours.

Select all the files AND folders that you want to


upload to AWS-EC2. (In Mac <Shift>+<down arrow>)
in this bottom panel
23

53
Part-3 Check Local and Remote setup Before upload
z
Before you do this next step to upload PLEASE CHECK that
your Remote Site is pointing to /var/www/html – otherwise it
will upload to wrong place and overwrite other files and
folders!!!!!
24

25
Once you have confirmed the remote site is okay then right
click and select Upload to start the transfer.

54
Part-3 Commence Upload
z

You will also see the files appearing in ths panel


representing the remote folder /var/www/html

The system will start to transfer all the selected files from your
local laptop to AWS-EC2 and you can see the status of the
transfer at this bottom panel

55
Part-3 Test Website from Browser
Once the transfer is complete and successful. You next go to your browser and
z paste your AWS-EC2 URL and hit enter.
open a new tab and

Your URL can be copied from AWS-EC2 Mangement panel in case you don’t
know and it should look like this (yours will be different)

ec2-19-299-57-160.compute-1.amazonaws.com
26
You will now see your website hosted on AWS-EC2

Job well done and complete!! Phew ! that was a long one…..

56
Part-3 Disconnecting Link to EC2
z

27
Once you have finish you can disconnect from AWS-EC2 by
selecting the Server tab followed by Disconnect

This will close it cleanly and you can close fileszilla

57
z

if YOU
WANT 58
Misc
z Your public DNS/AWS URL will look something similar to this line:

ec2-54-175-246-149.compute-1.amazonaws.com

Useful Reference Links

Using PuTTY to Access EC2:- https://linuxacademy.com/guide/17385-use-putty-to-


access-ec2-linux-instances-via-ssh-from-windows/
How to Host Website with EC2: https://dzone.com/articles/aws-ec2-how-to-launch-
an-instance-and-host-a-websi
LAMP webserver on EC2 Liniux: https://medium.com/@oreillyalan88/lamp-linux-apache-
mysql-php-web-server-on-an-amazon-ec2-linux-instance-e37eb023e996

How to Host Website on EC2: https://www.youtube.com/watch?v=hOYg-ClP84g

How to Host Website on EC2 using PuTTY:


https://www.youtube.com/watch?v=dhRwKPrum44&feature=youtu.be

How to Create EC2 Instance to Host: https://www.youtube.com/watch?v=Gwh4vxMVgac

How to Create EC2 Instance Step-by-Step: https://www.guru99.com/creating-amazon-


ec2-instance.html 59
z

60
Output of Some Commands
z

61
Output ofI Some
How can Commands
check all the software installed? (nginx, apache,
Then you
php,
have
etc).
to check the following versions and stuffs.
z 1.PHP version installed on the server
2.Mysql version installed on the server
3.PHP modules installed on the server
4.Apache modules installed on the server

62
Output ofI Some
How can Commands
check all the software installed? (nginx, apache,
Then you
php,
have
etc).
to check the following versions and stuffs.
z 1.PHP version installed on the server
2.Mysql version installed on the server
3.PHP modules installed on the server
4.Apache modules installed on the server

63
Output ofI Some
How can Commands
check all the software installed? (nginx, apache,
Then you
php,
have
etc).
to check the following versions and stuffs.
z 1.PHP version installed on the server
2.Mysql version installed on the server
3.PHP modules installed on the server
4.Apache modules installed on the server

64

You might also like