Hosting an apache webserver using Ec2 rhel8 instance, S3, and cloud front services through AWS CLI

Aishwarya Birla
5 min readNov 1, 2020

Task Description📄

🔰 Create High Availability Architecture with AWS CLI 🔰

🔅The architecture includes-

- Webserver configured on EC2 Instance

- Document Root(/var/www/html) made persistent by mounting on EBS Block Device.

- Static objects used in code such as pictures stored in S3

- Setting up Content Delivery Network using CloudFront and using the origin domain as S3 bucket.

- Finally, place the Cloud Front URL on the web app code for security and low latency.

In order to perform these tasks we need to first understand some core topics relating to it.

A server is a program that provides the services to the clients(who requests it). Clients can request only those services, which a server decides to provide. We can provide web services, where clients can request web pages or web app via https protocol. Here, the web pages or the web app are present on the serves storage, and the client requests for these pages via https protocol. HTTP is preferred since it is a way to share the data without giving more control of the device, to the client (like that in SSH protocol). To implement this kind of protocol we will need a program or software. Apache httpd is a software which can be used to host the web pages or web app so that anyone can access through the internet.

A device has a main or block storage where it stores the operating system files. In case of corruption, all other files in the partition or the drive can be corrupted along with the OS. A simple solution for this problem can be to store the important data in another hard disk so that we can still access the data by plugging it into another device.

S3 services of AWS are regional services and anyone requesting the data, which is stored in a data center located in another part of the globe, would take time. Although AWS has its own private global infrastructure, yet there is little latency for data transfer between data centers far away. To avoid this kind of latency, a cache can be created to store the data in the data center nearest to the user. This cache can be useful for the client and other users in nearby locations can access this data frequently without much latency. This cache service is provided by the AWS cloud front. They create caches in data centers or edge locations (similar to mini datacenters). By default, the time to live for this cache is of 1 day. This service is easy to use as it can be implemented by simply replacing the S3 domain name url with the cloud front domain name url.

Now, its time to dive in our task-

In the last blog, I explained how to configure AWS CLI and use it to create instances and an additional EBS block storage and mount it to the instance. Now using the public IP of the instance and the key pair attached to it, remotely login the instance through SSH protocol. You can use putty or solar putty application for windows users or use juice ssh for mobile users or simple ssh command in your Linux or Mac users.

Firstly, after creating a session in putty, we need to first download and install httpd software. We can use yum or dnf to get and install the software.

Type y to install the software and it will print “complete!” as a message after installation is complete.

Yum will automatically detect and install all the dependencies needed to install httpd.

All the pages which have to be hosted by the server are stored in a folder “/var/www/html”

Before creating the html page we will first mount the EBS block volume, which we have previously attached to the instance, so that our data is safe even in case of corruption. Steps to mount the disk-

  1. Know your disk name >lsblk
  2. Format the block in the preferred format >mkfs.ext4 /dev/<disk_name>
  3. Now we can mount the disk in the desired folder (/var/www/html) >mount /dev/<disk_name> /var/www/html

After mounting the EBS block, we will first create a demo html file in this folder. We will provide a locally stored image link to the page. (For file transfer from windows to Linux we can use SCP protocol or Winscp software). After file configuration we need to start the httpd services using a command

> systemctl start httpd

After every reboot, we will have to restart the services. To permanently start the services we can use the command

>systemctl enable httpd

A client can simply request to access these pages through a browser by using a url of the format

<protocol>:<server_ip>/<page_name>

example

The next step is to use S3 services to store static data in it. We can configure s3 services through the AWS CLI.

In AWS CLI first, create a bucket for storing our data. For help, run the command-

>aws s3api create-bucket help

here, mywallpaperbyash is the name of the bucket, in ap-south-1 available for the public to read.

Note: S3 is a regional service, therefore we need to give a unique name to our bucket.

After creating the bucket, we have to upload the data. We can do this by put command. For help run-

>aws s3api put-object help

here, I uploaded a photograph using its path, and the name used to refer it is key(here its par)

Now we can use this information and enter a URL to the HTML file to go and fetch the data from s3 server.

Finally, we will use AWS CloudFront to decrease the latency, all over the world. We will use AWS CLI to configure the CloudFront services.

For help, run the command-

>aws cloudfront create-distribution help

>aws cloudfront create-distribution — origin-domain-name mywallpaperbyash.s3.amazonaws.com — default-root-object par

here, I used the S3 domain name ‘mywallpaperbyash’ and S3 object as ‘par’

Our Final step is to now replace the s3 domain name with CloudFront domain name, followed by the object name. (here

“http://mywallpaperbyash.s3.amazonaws.com/par" will be replaced by

“https://d103l0b4dz4enw.cloudfront.net/par")

Hence our task is successfully completed!!

#awscloud #awscli #aws #vimaldaga #righteducation #educationredefine #rightmentor #worldrecordholder #linuxworld #makingindiafutureready #righeudcation #awsbylw #arthbylw

--

--