DEV Community

Mayank Tamrkar
Mayank Tamrkar

Posted on

Launch an AWS EC2 Instance

πŸš€ Launch an AWS EC2 Instance and Serve NGINX Using Docker (Beginner Friendly)

If you are new to AWS and DevOps, setting up an EC2 instance and exposing it to the internet can feel confusing.

In this guide, we will go step by step β€” from launching an EC2 instance to seeing the NGINX welcome page in your browser using Docker.

No prior DevOps experience required πŸ‘


🧠 What We’ll Build

By the end of this tutorial, you will have:

  • A running AWS EC2 instance
  • SSH access configured
  • Docker installed
  • NGINX running inside a Docker container
  • Public access via browser

πŸ›  Prerequisites

  • AWS account
  • Basic terminal knowledge
  • SSH installed on your local machine
  • Internet connection

🟒 Step 1: Launch an EC2 Instance

ec2 setup

  1. Go to AWS Console β†’ EC2 β†’ Launch Instance
  2. Choose Amazon Linux 2023 AMI
  3. Select instance type: t2.micro (Free tier)
  4. Create a imagekey pair:
    • Name: my-ec2-key
    • Type: RSA
    • Format: .pem
  5. Network settings:
    • Auto-assign Public IP: Enabled
  6. Create a Security Group with these inbound rules:
Type Port Source
SSH 22 My IP
HTTP 80 0.0.0.0/0
  1. Click Launch Instance

create rsa key

Wait until the instance state becomes Running.


πŸ” Step 2: Connect to EC2 Using SSH

Open the folder where this file exists and fix file permissions

chmod 400 my-ec2-key.pem
Enter fullscreen mode Exit fullscreen mode

Click on the Connect instance button using connection setup , just copy the SSH URL in example

click instance button

connection setup

sudo ssh -i ~/.ssh/my-ec2-key.pem ec2-user@<PUBLIC_IP>
Enter fullscreen mode Exit fullscreen mode

rsa login image

After login you will see the the screen like this way

login screen


πŸ”„ Step 3: Update the System

sudo yum update -y
Enter fullscreen mode Exit fullscreen mode

This ensures your system packages are up to date.


🐳 Step 4: Install Docker

sudo yum install docker -y
Enter fullscreen mode Exit fullscreen mode

Docker will allow us to run NGINX without installing it directly on EC2.

ec2 screen


▢️ Step 5: Start Docker Service

sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
Enter fullscreen mode Exit fullscreen mode

running docker image


πŸ‘€ Step 6: Allow Non-Root Docker Access

sudo usermod -aG docker ec2-user
exit
Enter fullscreen mode Exit fullscreen mode

πŸ” Log in again for the changes to apply:

ssh -i your_pem_file_name ec2-user@<PUBLIC_IP>
Enter fullscreen mode Exit fullscreen mode

βœ… Step 7: Verify Docker Installation

docker --version
Enter fullscreen mode Exit fullscreen mode

If Docker is installed correctly, you’ll see the version output.


🌐 Step 8: Run NGINX Using Docker

docker run -d --name nginx-test -p 80:80 nginx
Enter fullscreen mode Exit fullscreen mode

This command:

  • Runs NGINX in detached mode
  • Exposes port 80 publicly

πŸ” Step 9: Verify Running Container

docker ps
Enter fullscreen mode Exit fullscreen mode

working nginx image

You should see something like:

0.0.0.0:80->80/tcp
Enter fullscreen mode Exit fullscreen mode

🌍 Step 10: Access NGINX from Browser

Open your browser and navigate to:

http://<PUBLIC_IP>
Enter fullscreen mode Exit fullscreen mode

πŸŽ‰ You should now see:

nginx image

Welcome to nginx!

This confirms your EC2 instance is publicly accessible and serving content.

Top comments (0)