Setup and Run Apache on EC2 – 3 different ways

In this tutorial, I will show you how to create an EC2 instance and run apache server on it. We will also setup Security Group for the instance, and open up necessary ports to be able to access the web pages served from Apache over the web.

There are 3 different ways, we will set this up:

  1. In the first part, Using AWS Console, to create the EC2 instance, setup Security Group with ports 80/443 open. Once setup, we will SSH into the instance and run commands to setup apache server.
  2. In the second part, we will use UserData to run the scripts at the instance creation time. UserData is used to bootstrap and run any initialization scripts when the instance is first launched.
  3. The third part, is where we automate the entire process using Cloudformation template. This is the demo of Infrastructure as code, where we will create the EC2 instance, setup the security group with appropriate ports open, attach the security group to the instance and use UserData to install apache server on the instance, all via one cloudformation template and command line.

We will run the following steps. Video recoding is provided at the end of this post.

PART 1

1) Let’s create a EC2 Key Pair. This public/private key pair is needed for SSH into the instance. The private key will be downloaded on your machine. Make sure to change the permission on the file, by running chmod 0400 <file>.pem

2): Create a t2.micro instance, open port 80 and 443 on the security group, associate the key-pair we created in Step 1. Once the instance is up and running, you can use the browser based terminal or the following command to SSH into the instance.

ssh -i <path to the pem file>.pem ec2-user@<instance's public ip address>
# Run the following commands to install Apache and create index.html file
sudo su 
yum update -y
yum install httpd.x86_64 -y
systemctl start httpd.service
systemctl enable httpd.service
echo "Hello from apache server! Welcome to <b>mycloudtutorials.com</b>" > /var/www/html/index.html 

3) you can check the status of apache server, by using ps -ef | grep httpd command.

4) From the AWS console, grab the public dns name of the EC2 instance and access the webpage using http://<ec2-public-dns-name>/index.html

5) You can terminate the instance from the AWS console at this point.

PART 2

1) You can use the EC2 Key pair created in Part 1 or create a new key.

2): Create a t2.micro instance. In the Advance Section, under UserData, enter the following commands.

#!/bin/sh
yum update -y
yum install httpd.x86_64 -y
systemctl start httpd.service
systemctl enable httpd.service
echo "Hello from apache server! Created this page using the <b>UserData</b>. Welcome to <b>mycloudtutorials.com</b>" > /var/www/html/index.html 

3) Open port 80 and 443 on the security group, associate the key-pair we created in Step 1.

4) Once the instance is running, grab the public dns name of the EC2 instance and access the webpage using http://<ec2-public-dns-name>/index.html

5) You can terminate the instance from the AWS console at this point.

PART 3

1) You can use the EC2 Key pair created in Part 1 or create a new key.

2): In this step, we will use cloudformation to create the EC2 instance, install apache web server, create security group with appropriate port settings and attach security group to the EC2 instance.

Open a text editor and save the following in a file named, apache-ec2.yaml. Note I am using the AMI Id ami-0518bb0e75d3619ca, which is a valid Amazon Linux2 AMI id in Oregon (us-west-2) region. Please change this based on the region where you are running this Cloudformation script.

AWSTemplateFormatVersion: 2010-09-09

Resources:
  EC2ApacheInstance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-0518bb0e75d3619ca
      InstanceType: t2.micro
      Tags:
        - Key: Env
          Value: Dev
        - Key: Name
          Value: Apache Web Server
      UserData: 
        Fn::Base64:
          !Sub |
            #!/bin/sh
            yum update -y
            yum install httpd.x86_64 -y
            systemctl start httpd.service
            systemctl enable httpd.service
            echo "hello from mycloudtutorials.com - $(curl http://169.254.169.254/latest/meta-data/public-ipv4)" > /var/www/html/index.html
      SecurityGroups:
        - !Ref ApacheSecurityGroup
      KeyName: "mycloudtutorials"

  ApacheSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: "Apache Security Group"
      GroupDescription: Apacche security Group
      SecurityGroupIngress:
        - IpProtocol: "tcp"
          CidrIp: "0.0.0.0/0"
          FromPort: 22
          ToPort: 22
        - IpProtocol: "tcp"
          CidrIp: "0.0.0.0/0"
          FromPort: 80
          ToPort: 80
        - IpProtocol: "tcp"
          CidrIp: "0.0.0.0/0"
          FromPort: 443
          ToPort: 443

3) Run the following command (if you have AWS CLI installed), or upload the Cloudformation template in AWS Console’s Cloudformation section.

aws cloudformation create-stack --stack-name "My-Apache" --template-body file://./apache-ec2.yaml

Check the AWS Console’s Cloudformation section for the script to finish executing.

4) Once the instance is running, grab the public dns name of the EC2 instance and access the webpage using http://<ec2-public-dns-name>/index.html

5) You can delete the cloudformation stack either from the AWS console OR by running the following command (if you have AWS CLI installed)

aws cloudformation delete-stack --stack-name "My-Apache"

The CloudFormation Template is uploaded to Github:

https://github.com/mycloudtutorials/Cloudformation/blob/master/apache-ec2.yaml

Youtube Video:

This should conclude this exercise, where we created an EC2 instance, installed apache server on it in 3 different ways. UserData is a very powerful feature, which can be used for running one time scripts, clone repository, install some components etc. Cloud-formation is at the center of Infrastructure-As-Code for setting up servers, load balancers, and many other AWS services.