0% found this document useful (0 votes)
45 views45 pages

EC2 Ubuntu

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views45 pages

EC2 Ubuntu

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 45

How to Install Apache on AWS EC2 Instance

Ubuntu 18.04

Prerequisites:
 AWS Account with IAM user

 Create KeyPair

Steps:

1. Login to your AWS console

2. Type EC2 on the service box

3. You will arrive at the EC2 console.

 Launch Instance

 Add a name for you instance

 Pick Ubuntu

 For AMI: make sure it is Ubuntu Server 18.04 LTS

 Scroll down to attach the key pair. We will use the key pair to ssh
into our Ubuntu server.

 Create instance
 Once you see “running” under instance state, you can move to
the next step

4. Open your terminal

 Go to where you stored the key pair file.


- Chmod 0400 [name of the .pem file] → Sets permissions to
allow the owner to read
- ssh into your ubuntu server → ssh -i [name of the .pem file]
[ec2-user@ipv4 address]

 Update all packages on the server


- sudo apt update

 Install an Apache HTTP Web Server


- sudo apt install apache2
 Enable the Apache Web Server on FW to open port 80 and 443
for network traffic.
- sudo ufw allow “Apache Full”

 Verify that Apache is running


- sudo systemctl status apache2
- Input the public ipv4 address of Ubuntu EC2 instance on a
browser.

 Create an HTML page that displays “Welcome”


- sudo su
- vi /var/www/html/index.html
- Press i for edit
- Make some changes
- Press ESC :wq! to save your results
- Refresh the website and you will see your changes!
How to Set Up Apache Web Server in AWS
EC2 Linux (Ubuntu) Instance?


Process of setting up Apache Web Server in AWS EC2 Linux


Instance. This tutorial has been done on a system
running Windows 10 Home (Version 20H2).

Implementation:

The steps taken to complete this tutorial are being stated below:
Step 1: Go to portal.aws.amazon.com to create a new AWS
account. Provide email address and password. Click Continue.

Step 2: Provide other details like Name, Phone Number, etc., and
click Continue when done.
Step 3: Provide billing information and click ‘Verify and
Continue’.

Step 4: Confirm your identity and click ‘Send SMS’. Confirm the
OTP.
Step 5: Select a plan. Here, we choose ‘Basic support – Free’ and
click on ‘Complete sign up’.

Step 6: AWS account has been successfully created. Click ‘Go to


the AWS Management Console.
Step 7: Click on My Account > AWS Management Console.

Step 8: Sign in as Root User.


Step 9: Navigate to Services and select EC2 under Compute
category.

Step 10: On the left pane, click on Instances under the Instances
section. Click ‘Launch instances’.
Step 11: Now, we choose an Amazon Machine Image (AMI).
Search for ‘ubuntu server 18.04 LTS’ and click Select.

Step 12: Choose Instance Type. Here, we have chosen ‘t2.micro


which’ is Free tier eligible. Click ‘Next: Configure Instance Details.
Step 13: Don’t change anything and click ‘Next: Add Storage.

Step 14: Set the size as 8 GiB and click ‘Next: Add Tags’.
Step 15: Click ‘Next: Configure Security Group’.

Step 16: Choose ‘Create a new security group’ and add SSH,
HTTP, and HTTPS Types. Click ‘Review and Launch’ when done.
Step 17: Click Launch.

Step 18: Select ‘Create a new key pair’ and enter any key pair
name in the given field. Leave the ‘Key pair type’ as RSA. Click
‘Download Key Pair’ to download the .pem file. Store it in a secure
location as it will be used later. Click ‘Launch Instances’.
Step 19: Click ‘View Instances’.

Step 20: We can now see our instance named ‘inst1’ running.
Step 21: Go to putty.org to download PuTTY. Click on ‘here’.

Step 22: Under MSI (‘Windows Installer’) click the first link (next
to 64-bit x86) to download PuTTY.
Step 23: Go to puttygen.com to download PuTTYgen. Click on
‘PuTTY Installation Download page’.

Step 24: Under ‘puttygen.exe’ section click the ‘puttygen.exe’


link next to 64-bit to download it.
Step 25: Launch the setup to install PuTTY. Click Next.

Step 26: Specify the install location for PuTTY. Click Next.
Step 27: Click Install.

Step 28: After installing PuTTY, launch the PuTTYgen software.


The following window appears.
Step 29: Click File > Load private key.

Step 30: Select the previously downloaded .pem file


(ubuntuKey.pem) and click Open.
Step 31: Click Ok.

Step 32: Click ‘Save private key’.


Step 33: Enter a name for the generated .ppk file and click Save.

Step 34: Open PuTTY and enter the public IP of the instance in
Host Name filed and keep Port as 22.
Step 35: Select Auth under SSH and click on Browse.
Step 36: Select the .ppk file (ubuntuppk.ppk) and click Open.

Step 37: Click Accept.


Step 38: Login as ‘ubuntu’.

Step 39: Ubuntu has started as we can see below.


Step 40: Update packages with the command ‘sudo apt-get
update’.

Step 41: Install Apache2 using ‘sudo apt-get install apache2’.

Step 42: Verify that Apache is running using the command ‘sudo
systemctl status apache2’.
Step 43: Copy the public IP of the EC2 instance and paste it on a
browser of the host OS.

Step 44: The Apache2 Ubuntu Default Page appears.


Step 45: cd into /var/www/html and open the index.html file in
the nano editor.

Step 46: This is the default code. We need to replace this code
with our own code for the sample webpage.
Step 47: The default code is replaced with our own code in
index.html. Save and exit the editor.

Step 48: We can verify that we are able to access the hosted
webpage using ‘curl’ inside the instance.
Step 49: The hosted web page can also be accessed from the
host OS as shown.

Step 50: The hosted web page can also be accessed by another
system (a smartphone – iOS 14.7.1) as shown.
How to convert a PEM file to PPK format
.pem (Privacy-Enhanced Mail) and . ppk (PuTTY Private Key)

When you create an Amazon EC2 instance, AWS generates the access
keys in a Privacy Enhanced Mail (PEM) format.

For those who want to PuTTY to their EC2 instance, there’s a complication:
PuTTY does not natively support the PEM file format. You must convert the
PEM to a PPK file in order to PuTTY into the instance.
PEM to PPK conversion steps

Here are the steps to quickly convert a PEM to a PPK file with PuTTYGen:

1. Download PuTTYGen from puttygen.com

2. Open PuTTYGen and click the Load button

3. Set the filetype to *.* so the AWS PEM file is visible

4. Select your PEM file and PuTTYGen will import it

5. Click Save Private Key and PuTTYGen will convert the PEM to a PPK file

Step 3 often causes problems, because the file selector is set to PPK so the PEM
will not be visible even if you select the correct folder. Just change the filetype to
*.* and every filetype will be visible.

Upon import, PuTTYgen explains how to convert an SSH PEM to a PPK format for use with PuTTY.
Connect to EC2 from PuTTY

Once the PEM to PPK conversion is complete, simply register the PPK file in
PuTTY’s SSH authentication tab.

To SSH into an EC2 instance, find the SSH URL in the SSH client tab of the EC2
instances’ connection tab. Paste this in as PuTTY’s hostname and click Open.

How to PuTTY into Amazon EC2 instances with a PPK key.


PuTTY, EC2 and PEM files

Once the connection is made, you can issue commands and configure your EC2
instance at will.

And that’s how easy it is to convert a PEM file to PPK format and PuTTY into
remote servers such as Amazon EC2 instances.

What is the Secure Shell (SSH)


protocol?
The Secure Shell (SSH) protocol is a method for securely sending
commands to a computer over an unsecured network. SSH uses
cryptography to authenticate and encrypt connections between devices.
SSH also allows for tunneling, or port forwarding, which is when
data packets are able to cross networks that they would not otherwise be
able to cross. SSH is often used for controlling servers remotely, for
managing infrastructure, and for transferring files.
How to Install Apache 2 on AWS EC2 Instance Ubuntu 20.04

Table of Contents
 Steps to Install Apache 2 on AWS EC2 Instance Ubuntu 20.04
 Step 1: Launch an EC2 Instance(Ubuntu 20.04)
 Step 2: Connect to your EC2 instance
o To SSH from browser using Instance connect, follow below steps.
 Step 3: Install Apache Web Server
o Explanation of commands
o Update latest package available on the system
o Install Apache Web Server
o Verify Apache Installation
o Check Apache Server Status
 Step 4: Change Security Group of instance to allow port 80 and 443
 Step 5: Verify the Installation by Accessing the Server
 Step 6: Customize the web page
 Step 7: View the customized web page
o Other important commands:

Steps to Install Apache 2 on AWS EC2


Instance Ubuntu 20.04
1. Launch an EC2 Instance(Ubuntu 20.04)
2. Connect to your Ubuntu Instance
3. Install Apache 2 on the Instance
4. Configure Security group of instance to allow port 80
5. Verify the Installation by Accessing the Server
6. Customize the web page
7. View Customized Web page

Let’s get started …


Step 1: Launch an EC2 Instance(Ubuntu
20.04)
Before we install apache web server on the EC2 instance, we will
need an EC2 instance(ubuntu) up and running. You can refer my
previous tutorial to launch an instance in AWS.

Link to tutorial: How to Launch EC2 Instance Step by Step in


AWS

Important Note: Please note that, above tutorial uses Linux 2 AMI.
However, to create Ubuntu instance you need to
select Ubuntu while choosing AMI for your instance. Rest of the
process for creating instance remains same.

While you are in Choose AMI screen, search for ubuntu keyword
and select ubuntu server 20.04(free tier eligible) as shown below.
Here is how it looks while choosing AMI.
Step 2: Connect to your EC2 instance
Once your instance is up and running, you need to connect to your
instance. I will be using EC2 instance connect feature for this. It
allows you to SSH into instance from browser itself.

Lazy me 😛

However, if you want to usual SSH client, navigate to the directory


where keypair file is there on your local system and then run
command like below from your terminal.

ssh -i "DemoKeyPair.pem" ubuntu@instance-public-ip

To SSH from browser using Instance


connect, follow below steps.
1. Select your instance and click connect as highlighted
below

2. Once, you click Connect, you will see a screen with default
username for ubuntu instance
Verify that ubuntu is showing in username field and click Connect.

A new browser window will open and you will be connected into
your instance like below.
Amazing !!!

Now, you are ready to run commands on your EC2 instance. Let’s
move to the installation part in next step.

Step 3: Install Apache Web Server


Now, we need to run below set of commands one by one, to install
apache web server on our instance. Please note that we will
use sudo(root privilege ) to run all these command. The reason is,
whenever you try to install, remove or change any software, you
must have root privilege to do such tasks.

sudo apt update

sudo apt install apache2 -y

apache2 -version

sudo systemctl status apache2

Explanation of commands
Update latest package available on the
system
It’s a best practice to update all the packages to latest before
installing anything new.

sudo apt update

Install Apache Web Server


In this step, we are installing apache 2 on the EC2 instance. We are
providing -y option here to run this command silently. If you don’t
provide -y option, the CLI will ask you Y/n option and you have to
choose Y to install it. We want it install anyway so we are using -y
option.

sudo apt install apache2 -y

Verify Apache Installation


Let’s check if apache2 has been installed successfully. You can use
below command-

apache2 -version

As you can see in above screenshot, apache 2.4.41 has been


installed successfully.

Check Apache Server Status


By default, Apache is configured to start automatically when the
server boots. Lets check the status of server by using below
command.
sudo systemctl status apache2

At this moment, your apache web server is already installed and


started in your Ubuntu 20.04 instance. But, you can’t access it right
now. if you try to hit the public IP you will get error like This site
can’t be reached.

Why?

Because, our EC2 instance doesn’t allow web traffic yet.

On the security group, only SSH is allowed as of now.

Let’s change that.

Step 4: Change Security Group of


instance to allow port 80 and 443
Let’s allow web traffic on port 80 and 443(Internet traffic for http and
https).

Click on your instance id to see the instance details. Scroll down


and click on Security Tab and you should see security group.

Click on Security Group id link -> Click on Edit Inbound Rule


Use Add rule button to add more rule one by one.

Specify rules for HTTP and HTTPS Web traffic from anywhere like
above.

Step 5: Verify the Installation by


Accessing the Server
We have installed apache web server and it is running on our
instance. We have allowed web traffic as well in previous step.

Therefore, our instance allows web traffic now, it’s time to grab the
public IP or public dns of the instance.
Click on open address and you should be able to see the default
page like below.
Step 6: Customize the web page
We have seen the default apache page served by apache. Lets
modify that.

We will create our own index.html in the Document Root folder


which is /var/www/html in our case.

DocumentRoot: Document Root is the directory from which


apache looks for and serves web files on your request. So we will
create an index.html in /var/www/html folder
You can use various command to create and put content into
index.html inside folder. You can check this tutorial

For example- one of the simplest option is to use:

sudo echo “Hello World from $(hostname -f)” >


/var/www/html/index.html

But using ‘>’ creates a problem and the the above command ends
up running as ubuntu instead of root resulting in permission denied
like below.

-bash: /var/www/html/index.html: Permission denied

Let’s switch our user to root here by using sudo su. As you can see
in below screenshot, after using this command, root becomes the
current user.

Although it is advisable to stick to sudo when performing tasks that


require root privileges. By doing so, the current user is only granted
privileged for the specified command. On the other
hand, su switches to the root user completely and every command
runs as root which is not secured.

After switching the user try that command again but this time
without sudo as we already are running it as sudo

echo "Hello World from $(hostname -f)" >


/var/www/html/index.html
Once you hit enter, the file content gets replaced by hello world
things. Let’s verify that.

Step 7: View the customized web page


This time when you enter public ip or dns into the browser, you
will see your customized page like below.

You might also like