如何在 AWS EC2 部署網頁應用程式,免受 DevOps 煩惱(2026)
Deploy a Web App on AWS EC2 Without DevOps Headaches (2026)
指南説明如何在 AWS EC2 部署 Node.js 或 Next.js 應用程式,設定 Elastic IP、PM2、Nginx 和 HTTPS,並處理安全組、記憶體不足等常見問題。ShipOwn 是適用於 Windows、macOS 和 Linux 的桌面應用程式,可透過 SSH 管理部署、回滾及伺服器監察。部署計劃須經用戶批准,AI 終端提出的命令也須逐項確認。
AWS EC2 gives you a virtual server in a huge cloud. That's the good news. The bad news: before your app serves a single request, you're dealing with key pairs, security groups, IP addresses that change on stop and start, and a default user that isn't root.
This guide cuts through it. We'll launch an EC2 instance, deploy a Node.js or Next.js app with PM2 and Nginx, add HTTPS, and then show how ShipOwn turns repeat deploys into a short desktop flow.
Step 1: Launch the instance
In the EC2 console, Launch instance:
-
AMI: Ubuntu Server LTS (the default user is
ubuntu). - Instance type: a small general-purpose type is fine for most side projects. Small instances can run out of memory during builds; more on that below.
-
Key pair: create or choose one and download the
.pemfile. You won't get it again. - Network settings / Security group: allow SSH (22) from your IP only, and HTTP (80) and HTTPS (443) from anywhere.
-
Storage: give yourself enough disk for
node_modules, builds and logs.
Step 2: Give it a stable IP
A normal public IP changes when the instance is stopped and started. Allocate an Elastic IP and associate it with the instance, then point your domain's A record at it.
Step 3: Connect
chmod 400 my-key.pem
ssh -i my-key.pem ubuntu@ELASTIC_IP
sudo apt update && sudo apt upgrade -y
Step 4: Install the runtime
sudo apt install -y git nginx
# Node.js LTS via NodeSource or nvm
sudo npm i -g pm2
Add swap on small instances. next build and large TypeScript builds can be killed for lack of RAM:
sudo fallocate -l 2G /swapfile && sudo chmod 600 /swapfile
sudo mkswap /swapfile && sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Step 5: Deploy the app
git clone https://github.com/you/app.git && cd app
cp .env.example .env
npm ci && npm run build
pm2 start npm --name app -- start
pm2 save && pm2 startup
Step 6: Nginx and HTTPS
server {
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
sudo nginx -t && sudo systemctl reload nginx
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d app.example.com
If Certbot can't validate, the usual cause on AWS is the security group: port 80 must be open to the internet for the HTTP challenge.
The AWS-specific gotchas
| Problem | Cause | Fix |
|---|---|---|
| SSH times out | Security group doesn't allow 22 from your current IP | Update the inbound rule |
| Site works, then dies after a stop/start | Public IP changed | Use an Elastic IP |
Permission denied (publickey) |
Wrong user or key | Use ubuntu@ for Ubuntu AMIs and the right .pem
|
| Build killed | Not enough RAM | Add swap or build elsewhere |
| 502 Bad Gateway | App not on the expected port |
pm2 logs, check the port |
Repeat deploys with ShipOwn
Launching the instance is a one-time job. Deploying, updating, adding domains and rolling back happen again and again. That's where ShipOwn helps.
ShipOwn is a desktop app for Windows, macOS and Linux. It connects to your EC2 instance over plain SSH, using the same .pem key and ubuntu user. No ShipOwn deployment agent is installed on the instance.
The flow:
-
Connect a server: Elastic IP, port 22, user
ubuntu, your key. ShipOwn tests the connection before saving it. - Pick a source: GitHub repo and branch, or a local folder.
- Review the plan: detected framework, commands, port, domain and target. Every value is editable, and nothing runs until you approve.
-
Watch it build: choose a server build, or a PC build that builds on your computer and uploads the verified output. That's handy on a small instance that struggles with
next build. - Operate it: live URL, deployed commit, how far behind its branch it is, health, logs, redeploy and rollback.
What ShipOwn takes care of:
- Node apps under PM2: Next.js, Nuxt, Remix, SvelteKit, NestJS, Fastify, Express.
- Static sites on Nginx: Vite (React/Vue), Astro, plain HTML.
- Docker apps from a Dockerfile or Compose file. It can also install Docker on a server that doesn't have it.
- Nginx config tested before reload, and restored if rejected.
- Certbot HTTPS, with certificate expiry shown in the app.
- Release directories and rollback: the live pointer only moves after the build and health check pass.
Monitoring without CloudWatch setup
For a single instance, you might not want to set up a full metrics stack. ShipOwn's server view shows CPU, RAM, disk, load and PM2 processes, read directly from /proc and pm2 jlist, next to an embedded terminal. Heartbeats can run from the system tray while the app is running and your PC is awake.
When something breaks
Describe the problem to ShipOwn's AI terminal ("the site returns 502 since the last deploy"). Using your own Anthropic, OpenAI or DeepSeek key, it inspects the server and proposes one command at a time with its reason. You approve each one. Destructive commands (recursive deletes, reboots, firewall flushes, stopping SSH) are always held for confirmation.
FAQ
Does ShipOwn manage AWS resources like security groups? No. You create the instance in AWS. ShipOwn works over SSH once the server exists.
Can I use Amazon Linux? ShipOwn handles Ubuntu, Debian, the RHEL family and Alpine. See the supported environments page before choosing.
Auto-deploy on push? Not in ShipOwn. It shows waiting commits, and you click deploy.
Conclusion
EC2 is powerful but has a few traps: security groups, changing IPs, small-instance memory. Get those right once, then use ShipOwn for the deploy-and-rollback loop. Try ShipOwn free for 30 days, no card required.
Written by Vishvajeet Shukla, full stack developer and creator of ShipOwn.
來源:DEV Community · dev.to