How to use EC2 User Data Script to Install Apache Web Server

How to use EC2 User Data Script to Install Apache Web Server
Sharing is Caring:

How to use EC2 User Data Script to Install Apache Web Server

Dear Reader, I hope you are doing good. In one of my previous post, I explained how you can install apache web server in a EC2 instance.

For learning purpose that’s good. However, usually you would like your apache web server to be automatically installed as soon as your instance starts up. This is called bootstrapping your instance with required software and configurations.

You can use EC2 user data scripts to bootstrap your EC2 instance at launch. In this post, I will help you install the same apache web server but using EC2 user data script.

If you are a beginner and don’t know about ec2 user data script, here is a small section on the same.

What is EC2 User Data ?

In a very simple terms if I say, user data is user data/commands that you can specify at the time of launching your instance. These data/command executes after your EC2 instance starts.

You don’t need to SSH into your EC2 instance and run those command one by one. Rather all you need is to specify the whole script in the user data section and they get executed once your instance boots up.

Now, that’s cool !!!

Let’s understand by an example-

You want to create a file log.txt in dev folder as soon as your instance starts. You can specify below user data to achieve that.

#!/bin/bash
touch /dev/log.txt

Note: User data scripts run as root user so you don’t need to specify sudo with your commands

Steps to create EC2 User data

  1. Specify user data while launching EC2 Instance
  2. Allow traffic on port 80 and 443 on Security Group
  3. Verify User data Execution/Apache Installation

Let’s do all these steps one by one.

1. Specify User Data while launching EC2 Instance

Launch an EC2 instance with Linux 2 AMI. If you need help, you can use my previous tutorial to launch EC2 instance.

Link : How to Launch EC2 Instance In AWS Step by Step

Complete Step 1 to 4 as specified in the tutorial

When you are in step 5 and configuring instance details like VPC, subnet etc. , in this screen only scroll down to Advanced Details section and provide user data script to install apache web server

EC2 User Data Script to Install Apache Web Server: Provide EC2 User data

EC2 User data script to install Apache Web Server on Linux 2

#!/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

Paste this user data as test as specified in above screenshot and move to next step.

2. Allow traffic on port 80 and 443 on Security Group

Finish step 6 and 7

When you are in step 8 to configure security group, instead of adding one single rule to allow SSH on port 22, we will allow web traffic for apache from port 80(HTTP) and 443(HTTPS).

You can use Add Rule button to add HTTP and HTTPS

EC2 User Data Script to Install Apache Web Server: Configure Security Group

Click Review and Launch

Review the details that you have configured and click Launch

3. Verify EC2 User Data Execution/ Apache Installation

Pease note that, it takes some time for the user data script to execute. So give it some time and then verify the installation.

From the instance details screen, grab the public IP or pubic DNS of your instance

paste it in a new browser window and hit enter

or

Simply click on open address as shown below

EC2 User Data Script to Install Apache Web Server - Instance Detais

Once you click open address, you should be able to see that user data script has executed. Apache is installed and customized with our hello world code.

EC2 User Data Script to Install Apache Web Server: Verify Installation

If you are getting This site can’t be reached error, try below things-

  • Reload after some time because user data scripts might not have executed by the time you tried
  • Check your security group configuration to cross check if you allowed port 80 and 443
  • Try hitting the IP or DNS with http to see if HTTPS version is causing issue for example –http://ec2-13-233-223-241.ap-south-1.compute.amazonaws.com/

Conclusion

In this quick tutorial, we learnt about EC2 user data and how it can help us in bootstrapping our EC2 instance with required software and configurations.

Then we learnt how to use EC2 User Data Script to Install Apache Web server in your Linux 2. Finally we verified EC2 user data execution/Apache installation.

I hope you enjoyed this tutorial. If you have a question, feel free to drop in comment. I will be more then happy to answer your query.

Meanwhile you can also –

Suggested Read:

Sharing is Caring:

Leave a Reply

Your email address will not be published. Required fields are marked *