Hosting a static website can be a straightforward and cost-effective way to get your content online. Amazon Web Services (AWS) offers a reliable and scalable platform. In this guide, we’ll walk you through hosting a static website on AWS EC2, from setting up your instance to verifying your website’s accessibility.

What is AWS EC2, and why is it used for hosting?

Amazon Elastic Compute Cloud (EC2) is a web service that provides resizable compute capacity in the cloud. It allows you to run virtual servers, known as instances, to host your applications. Here’s why you should consider using EC2 for hosting your static website:

  • Scalability: EC2 allows you to scale your infrastructure up or down based on your website’s traffic needs.
  • Flexibility: You have complete control over the server environment, including the operating system, web server, and security settings.
  • Cost-Effectiveness: With EC2’s pay-as-you-go pricing model, you only pay for the computing capacity you use.

Setting Up Your EC2 Instance: A Step-by-Step Walkthrough

  1. Create an AWS Account: If you don’t already have an AWS account, sign up at aws.amazon.com.
  2. Launch an EC2 Instance:
    • Navigate to the EC2 Dashboard from the AWS Management Console.
    • Click on “Launch Instance” and choose an Amazon Machine Image (AMI). For simplicity, select the “Amazon Linux 2 AMI”.
    • Choose an instance type. The t2.micro instance is free-tier eligible and suitable for a low-traffic static website.
    • Configure the instance details and storage, and add any necessary tags.
    • Configure security group settings to allow HTTP (port 80) and SSH (port 22) traffic.
    • Review and launch your instance. Download the key pair (.pem file) to connect to your instance securely.
  3. Connect to Your Instance:
    • Open a terminal on your local machine.

Use the following SSH command to connect to your instance:

ssh -i /path/to/your-key-pair.pem ec2-user@your-ec2-public-dns

Installing and Configuring Your Web Server

  1. Update Your Instance:

Once connected, update your instance’s package lists:

sudo yum update -y

  1. Install Apache Web Server:

Install Apache using the following command:

sudo yum install httpd -y

  1. Start and Enable Apache:

Start the Apache service:

sudo systemctl start httpd

Enable Apache to start on boot:

sudo systemctl enable httpd

Uploading and Displaying Your Static Website

  1. Create a Simple HTML File:

Create an HTML file in the default web directory:

sudo nano /var/www/html/index.html

Add your HTML content, for example:

<!DOCTYPE html>

<html>

<head>

    <title>Welcome to My Static Website</title>

</head>

<body>

    <h1>Hello, World!</h1>

    <p>This is my static website hosted on AWS EC2.</p>

</body>

</html>

  • Save and exit the editor.
  1. Set Permissions:

Ensure the Apache user has the necessary permissions:

sudo chown -R apache:apache /var/www/html

Verify Website Access and Next Steps

  1. Verify Access:
    • Open a web browser and enter your instance’s public DNS or IP address. Your static website should be displayed.
  2. Next Steps:
    • Domain Name: Consider registering and configuring a domain name to point to your EC2 instance using Route 53.
    • SSL/TLS: Implement HTTPS for secure communication using AWS Certificate Manager and configure it with your web server.
    • Backup and Scaling: Explore AWS backup solutions and auto-scaling options to ensure your website’s reliability and performance.

References

Host a Static Website

Configuring a static website on Amazon S3