Deploying a Static Website on AWS Ubuntu EC2 with Apache2

 

Deploying a Static Website on AWS Ubuntu EC2 with Apache2


Introduction

This guide provides a step-by-step process to deploy a static website on an AWS EC2 instance running Ubuntu with Apache2. It covers instance setup, Apache installation, file deployment, and basic security configurations.

Prerequisites

  • An AWS account

  • Basic knowledge of Linux commands

  • An existing static website (HTML, CSS, JS files)

Step 1: Launch an Ubuntu EC2 Instance

  1. Log in to AWS Management Console.

  2. Navigate to EC2 Dashboard > Instances.

  3. Click Launch Instance.

  4. Select Ubuntu as the AMI.

  5. Choose an instance type (e.g., t2.micro for free tier).

  6. Configure security group:

    • Allow SSH (port 22) for your IP.

    • Allow HTTP (port 80) and HTTPS (port 443) for anywhere.

  7. Launch the instance and download the key pair (.pem file).

Step 2: Connect to the EC2 Instance

  1. Open a terminal and navigate to the directory containing the .pem file.

  2. Run the following command to connect:

    ssh -i your-key.pem ubuntu@your-ec2-public-ip

Step 3: Update System Packages

sudo apt update && sudo apt upgrade -y

Step 4: Install Apache2

sudo apt install apache2 -y

Verify Apache is running:

sudo systemctl status apache2

Enable Apache to start on boot:

sudo systemctl enable apache2

Step 5: Deploy Static Website Files

  1. Navigate to the Apache web root directory:

    cd /var/www/html
  2. Remove the default Apache page:

    sudo rm index.html
  3. Upload your website files (via SCP or manual transfer):

    sudo cp -r /your/local/directory/* /var/www/html/
  4. Set proper permissions:

    sudo chmod -R 755 /var/www/html
    sudo chown -R www-data:www-data /var/www/html

Step 6: Configure Firewall and Security Groups

Enable UFW and Allow Apache:

sudo ufw allow 'Apache'
sudo ufw enable

Verify Firewall Status:

sudo ufw status

Ensure AWS Security Groups Allow HTTP/HTTPS

  1. Go to EC2 Dashboard > Security Groups.

  2. Edit Inbound Rules:

    • Allow HTTP (port 80) from anywhere.

    • Allow HTTPS (port 443) from anywhere.

Step 7: Access the Website

Open a browser and enter:

http://your-ec2-public-ip

Your static website should be live!

Troubleshooting

  • Permission Denied Issues: Use sudo before commands.

  • Apache Not Running: Restart with sudo systemctl restart apache2.

  • Site Not Loading: Check security group rules and firewall settings.

Next Steps

  • Secure your site with an SSL certificate (Let’s Encrypt).

  • Use a domain name with Route 53.

  • Deploy a dynamic website with databases.

Conclusion

By following this guide, you have successfully deployed a static website on an AWS Ubuntu EC2 instance using Apache2. This is a great foundation for future projects like dynamic applications or cloud-based solutions.



Connect me on :

LinkedIn : www.linkedin.com/in/thisararupasinghe

YouTube : https://youtube.com/@thisararupsinghe?si=ts0K6VDwdaCqe6P6

Medium : https://medium.com/@thisara.damith

Blogspot : https://zynora00.blogspot.com/






Comments

Popular posts from this blog