DEV Community

Cover image for How to Deploy a Static Website on AWS EC2 Using Apache
augusthottie
augusthottie

Posted on

How to Deploy a Static Website on AWS EC2 Using Apache

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

  1. Introduction
  2. Prerequisites
  3. Setup AWS EC2 Instance
  4. Install and Configure Apache
  5. Deploy the Static Website
  6. Access the Website
  7. 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

  1. Log in to your AWS Management Console.
  2. Navigate to the EC2 Dashboard.
  3. Click on "Launch Instance".
  4. Choose an Amazon Machine Image (AMI) (e.g., Amazon Linux 2 AMI).
  5. Select an Instance Type (e.g., t2.micro for free tier eligibility).
  6. Configure Instance Details (default settings are usually fine).
  7. Add Storage (default settings are fine).
  8. Add Tags (optional).
  9. Configure Security Group:
    • Add a rule to allow HTTP traffic on port 80.
    • Add a rule to allow SSH traffic on port 22.
  10. Review and Launch the instance.
  11. Select your SSH key pair for the instance.

2. Connect to the EC2 Instance

  1. Open a terminal (or use PuTTY if on Windows).
  2. Connect to your instance using SSH:
   ssh -i /path/to/your-key-pair.pem ec2-user@your-instance-public-ip
Enter fullscreen mode Exit fullscreen mode

Install and Configure Apache

1. Update Packages

First, update your package list to ensure you have the latest versions:

sudo yum update -y
Enter fullscreen mode Exit fullscreen mode

2. Install Apache

Install the Apache web server:

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

3. Start Apache

Start the Apache service:

sudo systemctl start httpd
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Deploy the Static Website

1. Navigate to the Apache Directory

Change the directory to where Apache serves files:

cd /var/www/html
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!πŸš€

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.