DEV Community

Christina Sharon S
Christina Sharon S

Posted on

Creating a simple EC2 instance,running a webserver and accessing it from outside.

What is EC2?

Amazon Web Services provides a service called EC2 (Elastic Compute Cloud), which allows you to create virtual servers in the cloud.Instead of buying a physical computer and maintaining it,you can simply launch a server online in minutes. These virtual servers are called instances.

Why Do We Need EC2?

  • You don’t need to buy or maintain hardware
  • You can scale resources up or down anytime
  • Applications are accessible from anywhere
  • You only pay for what you use
  • Built in security and reliability

Basically, EC2 lets us to deploy real world applications without worrying about infrastructure.

Steps I Followed to Create an EC2 Instance

1.Logging into AWS Console

I logged into my AWS account and searched for EC2 in the services.

2.Launching an Instance

I clicked on Launch Instance and configured:

  • Name: MyWebServer2
  • AMI: Amazon Linux
  • Instance Type: t2.micro (Free Tier)
  • Key Pair: Created a new key pair for SSH access

3.Configuring Security Group

I allowed:

  • Port 22 (SSH) (To connect to my instance)
  • Port 80 (HTTP) (To access my website)

4.Launching the Instance

After reviewing everything, I launched the instance and waited for it to start.

Connecting to My Instance

Once the instance was running, I connected using SSH:

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

Installing a Web Server

Inside the instance, I installed Apache (These lines will be provided by AWS when you create a instance itself)

sudo yum update -y
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
Enter fullscreen mode Exit fullscreen mode

Then I created a simple web page:

echo "<h1>Hello from my EC2 server!</h1>" | sudo tee /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

Accessing My Website

I copied the Public IP address of my EC2 instance and opened it in my browser:

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

And just like that my web server was live and accessible from anywhere.

Top comments (0)