Cloud Computing 7 min read

How to Launch an Apache Web Server on Amazon EC2 Using the AWS CLI

Learn step‑by‑step how to configure the AWS CLI, create a security group, generate a key pair, write a bootstrapping script, and launch an Amazon EC2 instance running an Apache web server, including opening ports and testing the deployment.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
How to Launch an Apache Web Server on Amazon EC2 Using the AWS CLI

In this tutorial you will learn how to use the Apache web server on an Amazon EC2 instance, driven entirely by the AWS Command Line Interface (AWS CLI).

Configure AWS CLI

Before running any commands, ensure the AWS CLI is installed and ready. After installation, configure your default credentials (access key ID and secret access key) and default region (e.g., us-east-1) with JSON output format.

aws configure

Create a Security Group

A security group acts as a virtual firewall for your EC2 instance. You must provide a name and description, and associate it with a VPC.

First, retrieve your VPC ID: aws ec2 describe-vpcs Then create the security group:

aws ec2 create-security-group --group-name <value> --description <value> --vpc-id <your vpc>

Copy the resulting GroupID; you will need it later.

Add Rules to the Security Group

Open ports 80 (HTTP) and 22 (SSH) to allow inbound traffic from any IPv4 address.

aws ec2 authorize-security-group-ingress --group-name your_group_name --protocol tcp --port 80 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-name your_group_name --protocol tcp --port 22 --cidr 0.0.0.0/0

Create a Key Pair

A key pair (public and private keys) is required to connect to your EC2 instance. aws ec2 create-key-pair --key-name KeyPairName Verify the key pair was created:

aws ec2 describe-key-pairs --key-name KeyPairName

Prepare the Apache Bootstrapping Script

The script updates packages, installs Apache, and starts the service. Create the script with Vim: vim apache_runscript.sh Insert the following content:

#!/bin/bash
yum update -y
yum install httpd -y
systemctl start httpd
systemctl enable httpd

Launch the EC2 Instance

Find an AMI ID in the AWS console (EC2 → Launch instances). Then run:

aws ec2 run-instances --image-id ami-xxxxxxxx --count 1 --instance-type t2.micro --key-name KeyPairName --security-group-ids sg-903004f8 --user-data file://apache_runscript.sh

Check the console to ensure the instance state changes to Running .

Test the Apache Web Server

Copy the public IPv4 address of the instance, paste it into a browser, and you should see the default Apache test page, confirming a successful deployment.

Source: https://aws.plainenglish.io/launching-an-amazon-ec2-instance-with-an-apache-web-server-from-the-aws-cli-cb989cca6e0b
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CLIAWSApacheEC2Security GroupKey Pair
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.