Amazon EC2 (Elastic Compute Cloud) is one of the most popular ways to host applications in the cloud. It gives you a virtual server (an "instance") that you fully control and install whatever runtime, database, or web server you need, and run your app just like you would on a physical machine.
This guide walks through deploying a typical web application (Node.js, Python, or similar) on an EC2 instance, from launching the server to making it publicly accessible.
Prerequisites
- An AWS account
- Basic familiarity with the Linux command line
- Your application code (ideally in a Git repository)
- An SSH client (built into macOS/Linux terminals; use PuTTY or Windows Terminal on Windows)
Step 1: Launch an EC2 Instance
- Log in to the AWS Management Console and navigate to EC2.
- Click Launch Instance.
- Configure the basics:
- Name: something descriptive, e.g.,
my-app-server
- AMI (Amazon Machine Image): Choose an OS. Amazon Linux 2023 or Ubuntu 22.04/24.04 LTS are common, beginner-friendly choices.
- Instance type:
t2.microort3.microis free-tier eligible and fine for small apps/testing.
- Key pair: Create a new key pair (or use an existing one) and download the
.pemfile. You'll need this to SSH into the instance so you cannot download it again later, so keep it safe.
- Network settings: Click "Edit" and configure the security group (firewall) to allow:
- SSH (port 22) -> from "My IP" (safer) or "Anywhere" (less secure, fine for testing)
- HTTP (port 80) -> from Anywhere
- HTTPS (port 443) -> from Anywhere
- Any custom app port you use (e.g., 3000, 5000, 8080) if you're not putting a reverse proxy in front
- Storage: The default (usually 8 GB gp3) is fine for small apps.
- Click Launch Instance.
Step 2: Connect to Your Instance via SSH
Once the instance status shows "Running," grab its Public IPv4 address from the instance details page.
Set correct permissions on your key file (Mac/Linux):
chmod 400 my-key.pem
Connect:
ssh -i "my-key.pem" ubuntu@<your-public-ip>
Use
ec2-user instead of ubuntu if you launched an Amazon Linux AMI.Step 3: Install Dependencies
Update the system first:
sudo apt update && sudo apt upgrade -y # Ubuntu# orsudo yum update -y # Amazon Linux
Then install the runtime your app needs. A few examples:
Node.js (via NodeSource):
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -sudo apt install -y nodejsnode -v
Python:
sudo apt install -y python3 python3-pip python3-venv
Git (to pull your code):
sudo apt install -y git
Step 4: Get Your Code onto the Server
Clone your repository:
git clone https://github.com/your-username/your-repo.gitcd your-repo
Or upload files directly with
scp from your local machine:scp -i "my-key.pem" -r ./my-app ubuntu@<your-public-ip>:/home/ubuntu/
Install dependencies:
npm install # Node.js# orpip install -r requirements.txt # Python
Step 5: Run the Application
For a quick test, just run it directly:
npm start# orpython3 app.py
Visit
http://<your-public-ip>:<port> in your browser. If it doesn't load, double check your security group allows that port.Keep It Running After You Disconnect
Your app will stop the moment you close the SSH session unless you use a process manager.
PM2 (great for Node.js):
sudo npm install -g pm2pm2 start app.js --name my-apppm2 startup # ensures it restarts on server rebootpm2 save
systemd (works for any language) — create
/etc/systemd/system/myapp.service :[Unit]Description=My ApplicationAfter=network.target
[Service]WorkingDirectory=/home/ubuntu/your-repoExecStart=/usr/bin/node app.jsRestart=alwaysUser=ubuntu
[Install]WantedBy=multi-user.target
Then:
sudo systemctl enable myappsudo systemctl start myapp
Step 6: Put Nginx in Front (Recommended)
Running your app directly on port 80 or exposing custom ports isn't ideal. A reverse proxy like Nginx lets you serve on standard ports 80/443 and adds a layer for SSL, caching, and load balancing later.
sudo apt install -y nginx
Edit
/etc/nginx/sites-available/default and add a proxy block:server { listen 80; server_name your-domain-or-ip;
location / { proxy_pass http://localhost:3000; # your app's actual port proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }}
Restart Nginx:
sudo systemctl restart nginx
Now your app is reachable on
http://<your-public-ip> without specifying a port.Step 7: Add HTTPS (Optional but Recommended)
If you have a domain pointed at your instance's IP, use Certbot to get a free SSL certificate:
sudo apt install -y certbot python3-certbot-nginxsudo certbot --nginx -d yourdomain.com
Certbot automatically configures Nginx for HTTPS and sets up auto-renewal.
Step 8: (Optional) Attach an Elastic IP
By default, your instance's public IP changes if you stop and restart it. To keep a fixed IP:
- Go to EC2 → Elastic IPs → Allocate Elastic IP address.
- Associate it with your instance.
This is especially useful if you're pointing a domain's DNS at the server.
Common Gotchas
- Can't connect via SSH: Check the security group allows port 22 from your IP, and that you're using the right username for your AMI (
ubuntu,ec2-user, etc.).
- App unreachable from browser: Almost always a security group issue, the port your app runs on needs an inbound rule.
- App stops after closing terminal: You forgot to run it with PM2, systemd,
tmux, orscreen.
- Out of memory on
t2.micro: Small instances can struggle with build steps (e.g.,npm installon large projects). Consider building elsewhere and deploying only the built artifacts, or temporarily add swap space.
- Costs creeping up: Remember to stop or terminate instances you're not using EC2 bills by the hour/second while running.
Where to Go from Here
Once you're comfortable with this manual flow, consider leveling up with:
- Auto Scaling Groups + Load Balancers for handling traffic spikes and redundancy
- AWS CodeDeploy or a CI/CD pipeline (GitHub Actions, Jenkins) for automated deployments
- Docker to containerize your app for more consistent deployments
- Elastic Beanstalk if you want AWS to manage much of this infrastructure for you
Deploying to EC2 manually the first few times is a great way to understand what's actually happening under the hood before you reach for higher-level tools that automate it.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article