1. Introduction & use cases
Amazon EC2 (Elastic Compute Cloud) gives you virtual servers (instances) in the AWS cloud. You choose the operating system (an AMI such as Ubuntu 24.04), the size (instance type, e.g. t3.small or ARM-based t4g.small), storage (EBS volumes) and the firewall (security group).
You get full root access – install anything, just like a VPS – and can resize, snapshot or replace the server at any time. EC2 is a good fit when you want control over the server or run software that is not serverless.
Where it is used
- Hosting Node.js, Python, PHP, Java or Go applications
- Self-hosted tools (Jenkins, GitLab runners, databases for testing)
- Background workers and scheduled jobs
- Lift-and-shift of existing servers to the cloud
2. Prerequisites
| You need | Version / notes |
|---|---|
| AWS account | With MFA on the root user and a non-root admin identity |
| SSH client | Built into macOS/Linux/Windows 10+ (ssh) |
| Domain (optional) | For HTTPS on your own name |
| An app to deploy | e.g. from the Node.js guide |
3. Local setup
1Launch an instance
EC2 console → Launch instance: name it, choose Ubuntu Server 24.04 LTS, instance type
t3.micro/t3.small(ort4gfor ARM), and storage 20–30 GB gp3.2Create a key pair
In the launch wizard create a key pair (ED25519,
.pem) and download it – AWS shows it only once. Protect the file.BASHmkdir -p ~/.ssh && mv ~/Downloads/my-ec2-key.pem ~/.ssh/ chmod 400 ~/.ssh/my-ec2-key.pem3Configure the security group
Allow SSH (22) from My IP only, and HTTP (80) + HTTPS (443) from anywhere. Do not open app ports like 3000.
OUTPUTType Port Source SSH 22 My IP (e.g. 49.36.x.x/32) HTTP 80 0.0.0.0/0, ::/0 HTTPS 443 0.0.0.0/0, ::/04Connect with SSH
Use the instance’s public IP or DNS. The default user on Ubuntu AMIs is
ubuntu.BASHssh -i ~/.ssh/my-ec2-key.pem ubuntu@<PUBLIC_IP> sudo apt update && sudo apt upgrade -y
4. Create & run a project
1Allocate an Elastic IP
A public IP changes when you stop/start the instance. EC2 → Elastic IPs → Allocate → Associate with your instance gives a fixed address for DNS.
2Install Node.js and run a sample app
Same as any Ubuntu server.
BASHcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash source ~/.bashrc && nvm install --lts mkdir ~/app && cd ~/app && npm init -y cat > server.js <<'EOF' require('http').createServer((q, s) => s.end('Hello from EC2')).listen(3000); EOF node server.js & curl localhost:30003Use an IAM role instead of keys
If the app needs AWS services (S3, SES …), attach an IAM role to the instance (Actions → Security → Modify IAM role). The SDKs pick up credentials automatically.
✅ When it works you should see output like Figure 1.
5. Development configuration
Environment variables
Store app settings in an env file on the instance (readable only by the app user) or in SSM Parameter Store / Secrets Manager, fetched at startup via the instance role.
NODE_ENV=production
PORT=3000
AWS_REGION=ap-south-1
# no AWS keys here – the instance role provides credentialsKey dependencies
| Package / tool | Used for |
|---|---|
AMI | Ubuntu 24.04 LTS (or Amazon Linux 2023) |
Instance type | t3/t4g for small apps; m/c families for steady load |
EBS gp3 | Root disk; snapshot for backups |
Security group | Instance firewall |
Elastic IP | Fixed public IP |
SSM Session Manager (optional) | Shell access without opening port 22 |
Folder structure
/home/ubuntu/
└── app/
├── server.js
├── package.json
└── .env # chmod 600
/etc/nginx/sites-available/example.comEssential commands
| Command | What it does |
|---|---|
ssh -i key.pem ubuntu@IP | Connect |
scp -i key.pem file ubuntu@IP:~ | Copy files |
aws ec2 describe-instances --output table | List instances |
aws ec2 create-snapshot --volume-id vol-… | Back up a volume |
sudo systemctl status nginx | Check services on the instance |
6. Production deployment
Server & hosting requirements
| Requirement | Recommendation |
|---|---|
| Instance | t3.small (2 GB RAM) for a typical Node/Python app |
| Networking | Elastic IP, security group 80/443 open, 22 from your IP |
| Process manager | PM2 or systemd |
| Web server | Nginx + Let’s Encrypt |
| Backups | Scheduled EBS snapshots (Data Lifecycle Manager) |
| Monitoring | CloudWatch metrics + alarms |
Deployment steps
1Install Node.js on the server
Install the current LTS release with nvm (or the NodeSource packages). Use the same major version as on your computer.
BASHcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash source ~/.bashrc nvm install --lts node -v && npm -v2Get the code and build it
Clone the repository on the server (or upload a build artifact from CI). Install exact dependency versions with
npm ciand build.BASHgit clone https://github.com/you/your-app.git /home/ubuntu/app cd /home/ubuntu/app npm ci --omit=dev3Keep the app running with PM2
PM2 restarts the app if it crashes and starts it again after a server reboot.
pm2 startupprints one command – run it with sudo, thenpm2 save.BASHsudo npm install -g pm2 pm2 start server.js --name app pm2 startup # run the command it prints pm2 save pm2 logs app # live logs pm2 restart app # after each deploy4Install Nginx as a reverse proxy
Nginx receives requests on ports 80/443 and forwards them to your app on
127.0.0.1:3000. Replaceexample.comwith your domain./etc/nginx/sites-available/example.comserver { listen 80; server_name example.com www.example.com; location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; 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; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } }5Enable the site and reload Nginx
Link the config into
sites-enabled, test it and reload.nginx -tmust say syntax is ok before you reload.BASHsudo apt install -y nginx sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx6Point the domain to the Elastic IP
Add an A record for
example.com(andwww) to the Elastic IP in Route 53 or your registrar.OUTPUT# A example.com 13.233.x.x (Elastic IP) dig +short example.com7Add free HTTPS with Let’s Encrypt
Certbot gets a free SSL certificate, adds it to the Nginx config and redirects HTTP to HTTPS. Certificates renew automatically (a systemd timer runs twice a day); test renewal once with
--dry-run.BASHsudo apt install -y certbot python3-certbot-nginx sudo certbot --nginx -d example.com -d www.example.com sudo certbot renew --dry-run8Automate backups and alarms
Create a Data Lifecycle Manager policy for daily EBS snapshots, and a CloudWatch alarm on
CPUUtilizationandStatusCheckFailedwith an email (SNS) notification.
🗺 The whole path from your code to the live site is drawn in the deployment diagram.
7. Images & screenshots
Illustrations drawn for this guide from the commands above. Product dashboards change often, so for screens such as cloud consoles follow the step text and the official documentation.
8. Common errors & solutions
WARNING: UNPROTECTED PRIVATE KEY FILE! / permission denied
The .pem file is too open. Run chmod 400 key.pem (Windows: remove inherited permissions so only your user can read it).
SSH times out
Security group does not allow port 22 from your current IP (it changes on mobile/home networks), or you used the wrong IP after a restart. Update the rule or use an Elastic IP.
Permission denied (publickey)
Wrong username (ubuntu for Ubuntu, ec2-user for Amazon Linux) or wrong key file.
Site not reachable on port 80/443
Security group lacks HTTP/HTTPS rules, Nginx is not running, or UFW blocks it (sudo ufw allow 'Nginx Full').
Instance slows down over time (t3)
Burstable instances spend CPU credits. Check CPUCreditBalance; move to a larger or non-burstable type, or enable unlimited mode.
Best practices
- Restrict SSH to your IP or use Session Manager instead of port 22.
- Attach IAM roles; never store AWS keys on instances.
- Snapshot volumes automatically and test restores.
- Keep the OS patched (
unattended-upgrades). - For more than one server, add a load balancer and auto scaling group.