How to Install Apache HTTP Server (httpd) on Amazon EC2 with User Data Script

How to Install Apache HTTP Server (httpd) on Amazon EC2 with User Data Script

Table of contents

Introduction: The Apache HTTP Server, commonly referred to as httpd, is a robust and widely used web server software that powers millions of websites worldwide. In this tutorial, we'll walk you through the process of setting up the Apache web server on an Amazon EC2 instance using a user data script. This script automates the installation and configuration of httpd, allowing you to quickly host your website or web application.

Architecture

Here's a step-by-step guide on how to install the Apache HTTP Server (httpd) on an Amazon EC2 instance using a user data script

Prerequisites:

  • An AWS account with access to EC2.

  • Basic knowledge of AWS services and EC2 instances.

  • An Amazon EC2 instance with appropriate permissions to run user data scripts.

Step 1: Launch an Amazon EC2 Instance:

  1. Log in to your AWS Management Console.

  2. Navigate to the EC2 dashboard.

  3. Click "Launch Instance" to create a new EC2 instance.

Step 2: Choose an Amazon Machine Image (AMI):

  1. Select an Amazon Linux AMI or your preferred Linux distribution.

Added text

  1. Choose an instance type based on your requirements. I have selected for t2.micro

    1. Create a key pair with .pem if you want to log into the EC2 instance with SSH or create a key pair with .ppk if you want to log in to the EC2 instance with putty

  1. Configure instance details (e.g., VPC, subnet, security group).

  • Use Default VPC and Subnet

  • Enable Auto-assign Public IP

  • Allow SSH traffic to login to the EC2 instance from your local and allow HTTP traffic from the internet to access the webpage from the local

Step 3: Add User Data Script:

  1. In the "Configure Instance Details" section, scroll down to "Advanced Details."

  2. In the "User data" field, enter the following script:

#!/bin/bash
yum update -y
yum install -y httpd
service httpd start
service httpd status
chkconfig httpd on
cd /var/www/html
echo "Welcome to EC2 instance with User Data Example" > /var/www/html/index.html

This script does the following:

  1. Updates the package manager (yum).

  2. Installs the Apache HTTP Server (httpd).

  3. Starts the httpd service.

  4. Checks the status of the httpd service.

  5. Configures httpd to start on boot.

  6. Navigates to the web server's default directory (/var/www/html).

  7. Creates an index.html file with a welcome message.

This script updates the package repository, installs the Apache HTTP Server (httpd), starts the httpd service, and configures it to start automatically on boot.

Step 5: Launch the EC2 Instance:

  1. Review your instance settings and click "Launch."

Step 6: Access the Default Web Page:

  1. Wait for the instance to launch.

  2. Note the public IP address or DNS name of the instance.

  1. Open a web browser and enter the public IP or DNS name to access the web page.

Conclusion: In this tutorial, I have demonstrated how to set up the Apache HTTP Server (httpd) on an Amazon EC2 instance using a user data script. This streamlined process allows you to quickly deploy a web server on AWS.