git clone https://github.com/atulkamble/aws-ec2-static-website.git
cd aws-ec2-static-website
Hosting a Website on AWS EC2
This project provides a comprehensive guide for configuring and hosting a static website on AWS EC2, including relevant code snippets for setup and deployment.
Objective:
Establish and deploy a static website on an AWS EC2 instance.
- AWS Account: Ensure you have an active AWS account.
- AWS CLI: Install and configure the AWS Command Line Interface (CLI). Download AWS CLI
- EC2 Key Pair: Generate an EC2 key pair for secure instance access.
- Git:
git config --global user.name "username" git config --global user.email "email@example.com"
-
Access AWS Management Console and navigate to the EC2 Dashboard.
-
Initiate Instance Launch:
- Select “Launch Instance”.
- Choose an Amazon Machine Image (AMI): Opt for the “Amazon Linux 2 AMI”.
- Select an Instance Type: Choose “t2.micro” (eligible for the free tier).
- Configure Instance: Accept the default configuration settings.
- Add Storage: Accept the default storage configuration.
- Add Tags: (Optional) Add a tag such as
Name: EC2WebServer
. - Configure Security Group: Create a new security group with rules allowing HTTP (port 80) and SSH (port 22) traffic.
- Review and Launch: Verify settings and click “Launch”. Select your key pair and proceed with launching the instance.
-
Open your terminal (Linux/Mac) or use PuTTY (Windows).
-
Establish SSH Connection:
ssh -i /path/to/your-key-pair.pem ec2-user@your-ec2-public-dns
-
Update the instance and install Apache HTTP Server:
sudo yum update -y sudo yum install -y httpd
-
Start Apache service and enable it to start on boot:
sudo systemctl start httpd sudo systemctl enable httpd sudo usermod -a -G apache ec2-user
-
Create a basic HTML file:
sudo tee /var/www/html/index.html <<EOF <html> <head> <title>My First AWS Website</title> </head> <body> <h1>Hello, World!</h1> <p>Welcome to my website hosted on AWS EC2!</p> </body> </html> EOF
-
Set file permissions (if required):
sudo chmod 644 /var/www/html/index.html
-
Verify deployment by accessing the instance’s public DNS in your browser:
http://your-ec2-public-dns
You should see the “Hello, World!” webpage.
-
Create a deployment script (
deploy_website.sh
):#!/bin/bash sudo yum update -y sudo yum install -y httpd sudo systemctl start httpd sudo systemctl enable httpd sudo usermod -a -G apache ec2-user sudo tee /var/www/html/index.html <<EOF <html> <head> <title>My First AWS Website</title> </head> <body> <h1>Hello, World!</h1> <p>Welcome to my website hosted on AWS EC2!</p> </body> </html> EOF sudo chmod 644 /var/www/html/index.html
-
Transfer and execute the script on your EC2 instance:
scp -i /path/to/your-key-pair.pem deploy_website.sh ec2-user@your-ec2-public-dns:/home/ec2-user/ ssh -i /path/to/your-key-pair.pem ec2-user@your-ec2-public-dns 'bash /home/ec2-user/deploy_website.sh'
You have successfully set up and hosted a static website on an AWS EC2 instance. This guide walks you through launching an EC2 instance, installing a web server, and deploying a basic HTML page. For more advanced setups, consider integrating additional AWS services or using infrastructure-as-code tools such as Terraform or Ansible.