Hosting a website on an AWS EC2 Instance

Manya khedeManya khede
3 min read

Consider EC2 as your server in the cloud. It provides the power, flexibility, and control you desire, without the need to manage physical hardware.

Deploying a website on an EC2 instance serves as an excellent practical project for gaining experience in cloud computing. In this guide, I will guide you through launching an EC2 instance, setting up a web server, and hosting a basic HTML site. Let us begin.


Prerequisites

  • AWS Account: Make sure you have an AWS account. You can create one here.

  • Basic Terminal Knowledge: Familiarity with SSH and command-line interfaces.

  • HTML File (Optional): If you already have a website, have your HTML/CSS files ready.


Step 1: Launch an EC2 Instance

The first step to hosting your website is to launch an EC2 instance, which will act as your virtual server.

Instructions:

  1. Log in to the AWS Management Console and go to the EC2 Dashboard.

  2. Click "Launch Instance."

  3. Choose an Amazon Machine Image (AMI): Select an Ubuntu AMI for a simple setup (e.g., Ubuntu Server 20.04 LTS).

  4. Choose Instance Type: Select t2.micro for free tier eligibility.

  5. Configure Security Group: Allow SSH (port 22) and HTTP (port 80).

  6. Launch and Download Key Pair: Download your .pem key file to use when connecting to the instance

    Example Command to SSH into Your Instance:

Once your instance is running, note the Public IP from the EC2 dashboard.

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

Replace your-key.pem with the name of your key file and your-ec2-public-ip with your instance’s public IP address.

Step 2: Connect to Your EC2 Instance

Now, connect to your EC2 instance using SSH from your local machine.

Example Command:

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

You should see a welcome message from Ubuntu, which means you’re in!

Step 3: Install Apache Web Server

Apache is a reliable web server software that we’ll use to serve our website.

Commands:

  1. Update the package index:

     sudo apt update
    
  2. Install Apache:

     sudo apt install apache2 -y
    
  3. Verify Apache Installation:

    Check if Apache is running by entering:

     sudo systemctl status apache2
    
  4. Start Apache (if needed):

     bashCopy codesudo systemctl start apache2
    

Step 4: Configure Firewall

To ensure your website's accessibility, configure the security group to allow traffic on port 80 (HTTP) and, optionally, port 443 (HTTPS).

  1. Edit Inbound Rules in AWS Security Groups:

    • Go to Security Groups in the AWS console.

    • Add an inbound rule for HTTP (port 80) from Anywhere (or your IP if it’s restricted).

  2. Enable UFW Firewall on EC2 (Optional):

     sudo ufw allow 'Apache Full'
    

Step 5: Deploy Your Website

Now, let’s upload or create an HTML file to be served by Apache.

Commands:

  1. Navigate to Apache’s root directory:

     cd /var/www/html
    
  2. Remove Default Apache HTML File (Optional):

     sudo rm index.html
    
  3. Create or Upload Your HTML File:

    • You can create a new HTML file directly on the server:
    echo "<!DOCTYPE html><html><body><h1>My First EC2 Website</h1></body></html>" | sudo tee index.html
  • Or, use scp to upload your local files:
    scp -i "your-key.pem" path/to/your/file.html ubuntu@your-ec2-public-ip:/var/www/html/index.html

Step 6: Verify Your Setup

Now, it’s time to test your website!

  1. Go to Your Browser: Open your instance’s public IP address in a browser.

     http://your-ec2-public-ip
    
  2. See your Site: You should see your!


  • Image of running an instance

website is live!

I successfully hosted my website on an EC2 Instance.


Conclusion

Congratulations! You’ve successfully hosted a website on an AWS EC2 instance. With this foundational setup, you can customize your website, add a custom domain, or install a CMS like WordPress.


0
Subscribe to my newsletter

Read articles from Manya khede directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Manya khede
Manya khede