DEV Community

Ved Dandotia
Ved Dandotia

Posted on Edited on

Deploying Your First Web Server on AWS EC2 (Step-by-Step with User Data)

πŸš€ Launch Your First Amazon EC2 Instance to Host a Web Server

If you're starting your cloud journey with AWS, Amazon EC2 (Elastic Compute Cloud) is one of the first services you'll work with. It allows you to launch virtual machines in the cloud and host applications with complete control over the operating system, networking, and storage.

In this tutorial, you'll learn how to launch an EC2 instance, configure its networking, automatically install an Apache web server using EC2 User Data, and verify that your website is accessible from anywhere on the internet.

By the end of this guide, you'll have your own web server running on AWS. πŸš€


πŸ“š What You'll Learn

  • Launch an Amazon EC2 instance
  • Configure a Security Group
  • Use EC2 User Data to automate server setup
  • Deploy an Apache web server
  • Verify the deployment
  • Clean up AWS resources

πŸ—οΈ Architecture

Deployment Flow

Internet
    β”‚
    β–Ό
Security Group
(SSH :22 | HTTP :80)
    β”‚
    β–Ό
Amazon EC2 Instance
(Apache Web Server)
    β”‚
    β–Ό
Static HTML Page
Enter fullscreen mode Exit fullscreen mode

βœ… Prerequisites

Before starting, make sure you have:

  • An AWS Account (Free Tier eligible)
  • Basic knowledge of the AWS Console
  • An SSH client

    • Windows: PowerShell, Windows Terminal, or PuTTY
    • macOS/Linux: Terminal

Step 1 β€” Launch an EC2 Instance

  1. Open the AWS Management Console.
  2. Navigate to EC2.
  3. Click Launch Instance.
  4. Enter an instance name (for example, demoEC2).
  5. Choose the Amazon Linux 2 AMI.
  6. Select t3.micro as the instance type.
  7. Create or select an existing Key Pair.
  8. Under Network Settings, configure the Security Group.

Configure Inbound Rules

Type Port Source
SSH 22 My IP (Recommended)
HTTP 80 0.0.0.0/0

AWS automatically creates a Security Group (for example, launch-wizard-2).

πŸ“· Add a screenshot of the Launch Instance page here.


Step 2 β€” Configure User Data

Instead of manually connecting to the server and installing software, we'll use User Data. The script below runs automatically the first time the EC2 instance boots.

Navigate to:

Advanced Details β†’ User Data

Paste the following script.

#!/bin/bash

# Update packages
yum update -y

# Install Apache
yum install -y httpd

# Start Apache
systemctl start httpd

# Enable Apache after reboot
systemctl enable httpd

# Create sample webpage
echo "<h1>Hello World from EC2 User Data!</h1>" > /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

What does this script do?

  • Updates installed packages
  • Installs Apache HTTP Server
  • Starts the Apache service
  • Enables Apache to start automatically after every reboot
  • Creates a sample HTML page

Once the instance finishes booting, the web server is ready without any manual setup.

πŸ“· Add a screenshot of the User Data section here.


Step 3 β€” Launch the Instance

Click Launch Instance.

Within a minute, the instance state should become Running.

Take note of:

  • Public IPv4 Address
  • Public DNS

Example:

ec2-xx-xx-xx-xx.ap-south-1.compute.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

πŸ“· Add a screenshot of the EC2 Instance Summary here.


Step 4 β€” Verify the Security Group

Open your instance and navigate to:

Security β†’ Security Groups β†’ Inbound Rules

Verify that the following rules exist.

Type Port Source
SSH 22 Your Public IP
HTTP 80 0.0.0.0/0

The default outbound rule (Allow All Traffic) is sufficient for this tutorial.

⚠️ Security Best Practice

Avoid exposing SSH (Port 22) to the entire internet in production. Restrict it to your public IP whenever possible.

πŸ“· Add a screenshot of the Security Group rules here.


Step 5 β€” Test the Web Server

Open your browser and visit either:

http://<your-public-ip>
Enter fullscreen mode Exit fullscreen mode

or

http://<your-public-dns>
Enter fullscreen mode Exit fullscreen mode

If everything is configured correctly, you'll see:

Hello World from EC2 User Data!
Enter fullscreen mode Exit fullscreen mode

πŸ“· Add a screenshot showing the webpage in your browser.


Step 6 β€” (Optional) Connect Using SSH

Connect to your EC2 instance:

ssh -i your-key.pem ec2-user@<your-public-ip>
Enter fullscreen mode Exit fullscreen mode

Verify Apache is running.

sudo systemctl status httpd
Enter fullscreen mode Exit fullscreen mode

Expected output:

Active: active (running)
Enter fullscreen mode Exit fullscreen mode

🧹 Cleanup

To avoid unwanted AWS charges:

  1. Open the EC2 Console.
  2. Select your instance.
  3. Click Instance State.
  4. Choose Terminate Instance.

🎯 Conclusion

Congratulations! πŸŽ‰

You successfully:

  • βœ… Launched an Amazon EC2 instance
  • βœ… Configured a Security Group
  • βœ… Automated server setup using EC2 User Data
  • βœ… Installed and configured Apache
  • βœ… Hosted your first web page on AWS
  • βœ… Accessed it using a public IP address

Learning EC2 is a major milestone in your AWS journey. From here, you can explore more advanced services such as:

  • Elastic Load Balancer (ELB)
  • Auto Scaling
  • Amazon RDS
  • Route 53
  • Elastic Beanstalk
  • Amazon ECS
  • AWS CloudFormation

πŸ’¬ Final Thoughts

This is one of the simplest yet most important AWS projects for beginners. It introduces you to compute, networking, security, and automationβ€”all fundamental cloud concepts.

If you found this tutorial helpful, leave a ❀️, share it with others learning AWS, and let me know what AWS service you'd like to explore next.

Happy Cloud Learning! β˜οΈπŸš€


Tags: #aws #cloud #ec2 #devops #beginners

Top comments (0)