How to install Apache Web Server on EC2 Instance using User data script

Hello Friends,

In this tutorial, we will see how we can install Apache Web Server on EC2 instance using user data script.


In one of my previous tutorials, I have already explained how we can launch an EC2 instance using AWS console. If you have not already gone through that, I will recommend  to please go through that
first ,as that is prerequisite for this tutorial. Here is the link to that tutorial How to Launch an EC2 Instance in AWS.

- When you are in Step 6 of the above tutorial and you scroll down the "Configure Instance               Details", you will see following "Advanced Details" section :


- In the User Data section ,paste the following shell script to install Apache Web Server

#!/bin/bash
yum update -y
yum install -y httpd.x86_64
systemctl start httpd.service
systemctl enable httpd.service
echo "Hello World from $(hostname -f)" > /var/www/html/index.html



- Click on "Add Storage"  and proceed with Steps mentioned in How to Launch an EC2 Instance in AWS

- One more thing that you need to do is to edit the Security Group and add rule to allow Http traffic on port 80.

For that, as can be seen in Step 13, you need to look into the Description tab and below "iPV4 public IP" ,you will find "Security groups". Click on security group link and it will take you to "Security Groups" page, where you can click on "Security Group ID" and then click on "Edit inbound rules" and add rule to allow http traffic on port 80 from anywhere on internet.


- Now copy the public IP of your EC2 instance from "Description" .

Note : As the referred tutorial(How to Launch an EC2 Instance in AWS) was created some time back, so below screenshot has different instance Id and IP than the one mentioned in previous tutorial.



-Hit the IP  in the browser. You will be able to see "Hello World from ip-172-31-31-194.eu-west-2.compute.internal" in your browser, which is served by Apache server on EC2 instance.

Summary

In this tutorial, we learnt that 
- How we can install Apache Web Server on EC2 instance.
- Make sure that security group has inbound and outbound rule to allow Http traffic on port 80.

Thanks for reading. Please subscribe the blog, if you liked the post to get new post directly in your inbox.