π 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
- Go to AWS Console β EC2 β Launch Instance
- Choose Amazon Linux 2023 AMI
- Select instance type:
t2.micro(Free tier) - Create a imagekey pair:
- Name:
my-ec2-key - Type: RSA
- Format:
.pem
- Name:
- Network settings:
- Auto-assign Public IP: Enabled
- Create a Security Group with these inbound rules:
| Type | Port | Source |
|---|---|---|
| SSH | 22 | My IP |
| HTTP | 80 | 0.0.0.0/0 |
- Click Launch Instance
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
Click on the Connect instance button using connection setup , just copy the SSH URL in example
sudo ssh -i ~/.ssh/my-ec2-key.pem ec2-user@<PUBLIC_IP>
After login you will see the the screen like this way
π Step 3: Update the System
sudo yum update -y
This ensures your system packages are up to date.
π³ Step 4: Install Docker
sudo yum install docker -y
Docker will allow us to run NGINX without installing it directly on EC2.
βΆοΈ Step 5: Start Docker Service
sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker
π€ Step 6: Allow Non-Root Docker Access
sudo usermod -aG docker ec2-user
exit
π Log in again for the changes to apply:
ssh -i your_pem_file_name ec2-user@<PUBLIC_IP>
β Step 7: Verify Docker Installation
docker --version
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
This command:
- Runs NGINX in detached mode
- Exposes port 80 publicly
π Step 9: Verify Running Container
docker ps
You should see something like:
0.0.0.0:80->80/tcp
π Step 10: Access NGINX from Browser
Open your browser and navigate to:
http://<PUBLIC_IP>
π You should now see:
Welcome to nginx!
This confirms your EC2 instance is publicly accessible and serving content.










Top comments (0)