How to Deploy a Static Website on AWS EC2 Using Apache
augusthottie
Posted on August 2, 2024
Deploying a static website on an AWS EC2 instance using Apache is a straightforward process that you can follow to get your site live. This guide will walk you through each step using an example where the static website includes an HTML file hosted on GitHub. The example website mentions the HNG Internship which I took part in, and contains a link to https://hng.tech, but you can adapt these steps to deploy any static website.
Table of Contents
- Introduction
- Prerequisites
- Setup AWS EC2 Instance
- Install and Configure Apache
- Deploy the Static Website
- Access the Website
- Conclusion
Introduction
Deploying a static website on an AWS EC2 instance using Apache is a straightforward process. This guide will walk you through each step, ensuring your website is live and accessible. The static website includes an HTML file hosted on GitHub, mentioning the HNG Internship and linking to https://hng.tech.
Prerequisites
Before you begin, make sure you have:
- An AWS account
- Basic knowledge of AWS EC2 and SSH
- An SSH key pair for accessing the EC2 instance
- A static website (HTML, CSS, JavaScript files)
Setup AWS EC2 Instance
1. Launch an EC2 Instance
- Log in to your AWS Management Console.
- Navigate to the EC2 Dashboard.
- Click on "Launch Instance".
- Choose an Amazon Machine Image (AMI) (e.g., Amazon Linux 2 AMI).
- Select an Instance Type (e.g., t2.micro for free tier eligibility).
- Configure Instance Details (default settings are usually fine).
- Add Storage (default settings are fine).
- Add Tags (optional).
- Configure Security Group:
- Add a rule to allow HTTP traffic on port 80.
- Add a rule to allow SSH traffic on port 22.
- Review and Launch the instance.
- Select your SSH key pair for the instance.
2. Connect to the EC2 Instance
- Open a terminal (or use PuTTY if on Windows).
- Connect to your instance using SSH:
ssh -i /path/to/your-key-pair.pem ec2-user@your-instance-public-ip
Install and Configure Apache
1. Update Packages
First, update your package list to ensure you have the latest versions:
sudo yum update -y
2. Install Apache
Install the Apache web server:
sudo yum install httpd -y
3. Start Apache
Start the Apache service:
sudo systemctl start httpd
4. Enable Apache to Start on Boot
Enable Apache to start on boot to ensure it runs automatically when the instance is restarted:
sudo systemctl enable httpd
Deploy the Static Website
1. Navigate to the Apache Directory
Change the directory to where Apache serves files:
cd /var/www/html
2. Download the index.html
File Using wget
Download your HTML file from GitHub:
sudo wget https://raw.githubusercontent.com/AugustHottie/devops-task0/master/index.html
3. Set the Correct Permissions
Set the appropriate permissions for the index.html
file:
sudo chown apache:apache /var/www/html/index.html
sudo chmod 644 /var/www/html/index.html
Access the Website
Open a Web Browser
Navigate to the public IP address of your EC2 instance. Your static website should be displayed. In our example, it mentions the HNG Internship and links to https://hng.tech.
Conclusion
By following the steps outlined in this guide, you can successfully deploy any static website on an AWS EC2 instance using Apache. Your website should now be accessible via the public IP address of your EC2 instance. This guide uses an example from an HNG Internship task, but the steps are adaptable to any static site!
If you have any questions or need further assistance, feel free to reach out. Happy deploying!🚀
Posted on August 2, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.