DEV Community

Cover image for πŸ’» Step-by-Step: Hosting a Static Website on AWS EC2 🌟

πŸ’» Step-by-Step: Hosting a Static Website on AWS EC2 🌟

πŸ‘‹ Hey there! I’m Sarvar, a Cloud Architect passionate about cutting-edge technologies. With years of experience in Cloud Operations (Azure and AWS), Data Operations, Data Analytics, DevOps, and GenAI I've had the privilege of working with clients around the globe, delivering top-notch results. I’m always exploring the latest tech trends and love sharing what I learn along the way. Let’s dive into the world of cloud and tech together! πŸš€

In this tutorial, we will walk through the process of hosting a static website on AWS using an EC2 instance. This solution is ideal if you want to control the server hosting your website and are looking to use AWS for scalability and flexibility.


Prerequisites

Before we get started, ensure you have the following:

  • An active AWS account
  • AWS CLI installed (optional)
  • A basic understanding of EC2, security groups, and static websites

GitHub Repository for Required Files: All the necessary files for this tutorial can be found in the following GitHub repository:

Static Website on EC2 Repository


Step 1: Launch an EC2 Instance

1. Sign in to the AWS Management Console and navigate to EC2 under the Compute section.

Image description

2. Click Launch Instance to start a new EC2 instance.

Image description

3. Choose an AMI: For this example, we’ll use the Amazon Linux 2 AMI. This is a lightweight, secure, and stable Linux server that can serve static content.

Image description

4. Select Instance Type: Choose t2.micro (free tier eligible) for basic hosting.

Image description

5. Create New Key: Click on "Create New Key Pair": In the EC2 dashboard, navigate to the Key Pairs section and click on Create Key Pair to begin the process of generating a new SSH key pair for your EC2 instance.

Image description

Click "Create": Once you've chosen a name and confirmed the .pem extension, hit the Create button. This will generate your private key and automatically download the .pem file to your computer.

Image description

6. Configure Instance: Leave the default settings, or configure them based on your needs. Ensure your instance has a public IP so you can access it from the internet.

Image description

7. Add Storage: You can stick with the default 8GB storage or increase it if necessary.

Image description

8. Configure Security Group: Create a new security group or use an existing one. Allow inbound HTTP (port 80) and SSH (port 22) access.

Image description

9. Configure Security Group: Click on Lunch Instance

Image description


Step 2: Connect to Your EC2 Instance

Once your instance is running, you can SSH into it.

  1. From the EC2 dashboard, select your instance and click Connect.
  2. From EC2 Instance Connect option click on connect to SSH into the instance using your private key.

Image description

Image description


Step 3: Install Apache HTTP Server

We’ll use Apache HTTP Server to serve the static website.

1. Update your EC2 instance:

   sudo yum update -y
Enter fullscreen mode Exit fullscreen mode

Image description

2. Install Apache:

   sudo yum install -y httpd
Enter fullscreen mode Exit fullscreen mode

Image description

3. Start Apache:

   sudo systemctl start httpd
Enter fullscreen mode Exit fullscreen mode

4. Enable Apache to start on boot:

   sudo systemctl enable httpd
Enter fullscreen mode Exit fullscreen mode

Image description


Step 4: Configure Static Website

Once you’ve successfully logged into the EC2 instance, follow these steps to install the necessary files from GitHub and set up your server:

1. Download the Static Website Files from GitHub: Use the git command to clone your repository directly into the instance:

   yum install git -y
   git clone https://github.com/simplynadaf/static-website-on-ec2.git
Enter fullscreen mode Exit fullscreen mode

Image description

Image description

2. Move the Files to the Web Root: Copy the contents of the repository to the web root directory:

   cp -r static-website-on-ec2/* /var/www/html
Enter fullscreen mode Exit fullscreen mode

Image description

3. Clean Up: Remove the cloned repository folder if it's no longer needed:

   rm -rf static-website-on-ec2
Enter fullscreen mode Exit fullscreen mode

Step 5: Test Your Website

Now that everything is set up, open a web browser and visit your EC2 instance’s public IP (e.g., http://your-public-ip). If everything is configured correctly, your static website should appear.

Image description


Important Note 🚨

Always remember to use http://: By default, browsers will attempt to connect using HTTPS, which may result in a "failed to connect" error if HTTPS is not configured on your server.

To access your website, manually type the address in the format:

http://your-public-ip/
Enter fullscreen mode Exit fullscreen mode

Conclusion: In this tutorial, we’ve covered how to host a simple static website using an EC2 instance on AWS. This method gives you flexibility and control over your server, making it a great option for small-to-medium static websites. With the ability to configure a custom domain and SSL certificate, you can further enhance your website's availability and security.

β€” β€” β€” β€” β€” β€” β€” β€”
Here is the End!

✨ Thank you for reading! ✨ I hope this article helped simplify the process and gave you valuable insights. As I continue to explore the ever-evolving world of technology, I’m excited to share more guides, tips, and updates with you. πŸš€ Stay tuned for more content that breaks down complex concepts and makes them easier to grasp. Let’s keep learning and growing together! πŸ’‘

Top comments (10)

Collapse
 
valerij_timofeev_ed059a72 profile image
Valerij Timofeev

I'm wondering, what is the benefit of using EC2 for hosting a static website in comparison for example to CloudFront?

Collapse
 
sarvar_04 profile image
Sarvar Nadaf

Hi Valeriji,
I highly appreciate your response but this article written for the beginners. Will write article on cloudfront.

Collapse
 
reynaldi profile image
reynaldi

thanks for sharing Sarvar, and what a nice cover image for your article

Collapse
 
sarvar_04 profile image
Sarvar Nadaf

Thank you so much reynaldi 😎

Collapse
 
reynaldi profile image
reynaldi

if you don't mind sharing, what did you use to generate it? i particularly like how the text is still legible

Thread Thread
 
sarvar_04 profile image
Sarvar Nadaf

I've used Amazon Nova Canvas Foundation Module in Amazon bedrock playground.

Collapse
 
techie_1414 profile image
Techie

Excellent πŸ‘

Collapse
 
sarvar_04 profile image
Sarvar Nadaf

Thanks Man

Collapse
 
8907234 profile image
8907234 • Edited

If it's static, why would you use EC2 instead of S3? This site needs a downvote button.

Attention beginners. DO NOT DO THIS. It's a massive waste of time (and money if you're not on AWS free tier).

Collapse
 
sarvar_04 profile image
Sarvar Nadaf

Hey It's true I'm working on some GenAI use case series that why I've created written this article.