Pages

Wednesday, May 15, 2024

Step-by-Step Tutorial: Launching an EC2 Instance and Configuring Apache on AWS

Step-by-Step Tutorial: Launching an EC2 Instance and Configuring Apache on AWS


To launch an EC2 instance on the AWS Management Console, configure it to run Apache, and ensure it is accessible only from your PC, follow these detailed steps:

Step 1: Log In to AWS Management Console

Step 2: Launch an EC2 Instance

  1. Select EC2 Service:

    • From the AWS Management Console, select "Services" from the top menu, then choose "EC2".
  2. Launch Instance:

    • Click on "Instances" from the left sidebar, then click "Launch Instances".
  3. Choose an Amazon Machine Image (AMI):

    • Select an Amazon Machine Image (AMI). Use the "Amazon Linux 2 AMI" or another that is marked as "Free tier eligible".
  4. Choose an Instance Type:

    • Choose t2.micro (eligible for the AWS free tier). Click "Next: Configure Instance Details".
  5. Configure Instance:

    • Configure the instance details as needed, or leave the defaults. Click "Next: Add Storage".
  6. Add Storage:

    • Adjust the storage settings as needed or leave them at default values. Click "Next: Add Tags".
  7. Add Tags:

    • Optionally add tags by clicking "Add Tag". Input key-value pairs (e.g., "Name" and "Web Server"). Click "Next: Configure Security Group".
  8. Configure Security Group:

    • Select "Create a new security group".
    • Name it (e.g., "WebServerSG").
    • Set rules to allow traffic:
      • Click "Add Rule" to open port 22 (SSH) and type your public IP followed by /32 in the "Source" field for secure SSH access.
      • Click "Add Rule" to open port 80 (HTTP) and type your public IP followed by /32 in the "Source" field to view the web page.
    • Click "Review and Launch".
  9. Review and Launch:

    • Review your instance settings. Click "Launch".
  10. Select a Key Pair:

    • Select "Create a new key pair", name it, and download it. You will need this key to SSH into your instance.
    • Acknowledge that you have the key pair by checking the box, and click "Launch Instances".

Step 3: Install Apache on Your EC2 Instance

  1. Access Your Instance:

    • Once the instance state is "running", select it in the EC2 dashboard.
    • Find the "IPv4 Public IP" under the "Description" tab.
  2. Connect to Your Instance:

    • Use an SSH client with the downloaded key pair. Example command:
ssh -i /path/to/your-key.pem ec2-user@your-public-ip

Replace /path/to/your-key.pem with your key path and your-public-ip with your instance's IP address.

3. Install Apache:

sudo yum update -y sudo yum install httpd -y sudo systemctl start httpd sudo systemctl enable httpd

4. Create a Simple Web Page:

echo "<html><body><h1>Hello from MQM Apache on AWS EC2</h1></body></html>" | sudo tee /var/www/html/index.html

Step 4: Test the Website

  • Open a browser and navigate to http://your-public-ip. You should see your "Hello from Apache on AWS EC2" message.

These steps will set up a basic web server that is accessible only from your specified IP, ensuring both security and functionality.

To Troubleshoot on AWS EC2 Instance:


Step 1: Verify Security Group Settings

The first step is to ensure that your EC2 instance's security groups are configured to allow inbound traffic on the necessary ports:

  • HTTP and HTTPS: Ensure ports 80 (HTTP) and 443 (HTTPS) are open.
  • SSH: Port 22 should be open to allow SSH access for troubleshooting.

Step 2: Check Instance Status and Network Configuration

  • Instance Health: Ensure that your EC2 instance is running. You can check this in the EC2 dashboard under the "Instances" section.
  • Network ACLs: Make sure the associated Network ACLs allow both inbound and outbound traffic for HTTP, HTTPS, and SSH.

Step 3: Confirm Apache is Running

SSH into your instance using:

ssh -i /path/to/your-key.pem ec2-user@your-instance-public-ip

Check Apache’s status:

sudo systemctl status httpd

If Apache isn't running, start it with:

sudo systemctl start httpd

Step 4: Ensure Correct Public IP Address

If your EC2 instance was stopped and restarted, its public IP might have changed unless it's associated with an Elastic IP. Always check that you are using the current public IP.

Step 5: Review VPC and Subnet Settings

Your instance should be in a VPC with a correctly configured Internet Gateway. Ensure that the subnet’s route table directs traffic to the Internet Gateway.

Step 6: Firewall Configuration

Internal firewalls like iptables or firewalld on your EC2 instance can block incoming traffic. Verify that these settings are not preventing access to Apache.

Step 7: Test with a Simple HTML Page

To rule out application-specific issues, test by serving a simple HTML page. Sometimes, complex applications can respond slowly or timeout.

Step 8: Analyze Apache Logs

Checking the Apache error logs can provide insights into what might be causing the issue:

sudo tail -f /var/log/httpd/error_log


No comments:

Post a Comment