What you’ll build
You will run a small Express app on an EC2 virtual server. PM2 keeps the app running, and Nginx serves it on port 80. The steps use Amazon Linux 2023, the default operating system image in the EC2 console.
While the instance runs, you pay for the instance itself, its EBS storage and its public IPv4 address. The clean-up steps at the end explain how to stop those charges when you are done.
Before you start
- An AWS account, with a regular IAM user for daily work rather than the root user.
- Node.js (current LTS) and a project folder on your computer. Run
npm installlocally so the project has apackage-lock.jsonfile. - An SSH client. macOS, Linux and recent versions of Windows include OpenSSH.
Here is a minimal app to deploy. Save it as app.js:
const express = require("express");
const app = express();
const port = process.env.PORT || 3000;
app.get("/", (req, res) => {
res.send("Hello from EC2");
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
Install Express with npm install express, then commit package.json and package-lock.json with the code.
Step 1: Launch an EC2 instance
In the EC2 console, choose Launch instance and set the following:
- Name: a name you will recognize later.
- AMI: Amazon Linux 2023. Its default user is
ec2-user. - Instance type: a small type such as
t3.micro. Check whether your account and Region include it in the free tier. At3.microhas 1 GiB of memory, which can be tight during installs (see Troubleshooting). - Key pair: create a new RSA key pair and download the
.pemfile. Keep it somewhere safe. - Network settings: use the default VPC and a public subnet, with Auto-assign public IP turned on.
- Security group: allow SSH (port 22) from My IP only. If the site will be public, also allow HTTP (port 80) and HTTPS (port 443) from anywhere. Do not open port 3000 to the internet.
Choose Launch instance and wait until the instance state shows Running.
Step 2: Connect over SSH
Find the instance’s public DNS name in the console, then run:
chmod 400 my-key.pem
ssh -i my-key.pem ec2-user@<public-dns-name>
The chmod command makes the key readable only by you. SSH refuses keys that other users can read.
Step 3: Install Node.js
The default Node.js package on Amazon Linux may not be the version you want. A version manager gives you more control. Install nvm by following the install instructions in the nvm project’s README, then run:
nvm install --lts
node -v
Step 4: Get your code onto the instance
You can clone the code from a Git repository:
git clone <your-repository-url> my-app
cd my-app
Or copy the folder from your computer with scp. Run this on your computer:
scp -i my-key.pem -r ./my-app ec2-user@<public-dns-name>:~/
Then, on the instance, install only the production dependencies:
cd ~/my-app
npm ci --omit=dev
npm ci needs the package-lock.json file, and it installs exactly the versions recorded there.
Step 5: Run the app and test it
PORT=3000 node app.js
Open a second SSH session and run curl http://localhost:3000. You should see Hello from EC2.
This works, but the app stops when you close the SSH session. The next step fixes that.
Step 6: Keep the app running with PM2
PM2 is a process manager. It restarts the app if it crashes and can start it again after a reboot.
npm install -g pm2
pm2 start app.js --name my-app
pm2 startup
pm2 save
pm2 startup prints a command that you must run once, usually with sudo. Copy it exactly as printed. Then pm2 save stores the current process list so it returns after a reboot.
If you prefer, you can run the app as a systemd service instead. PM2 is shorter to set up for a first deployment.
Step 7: Put Nginx in front on port 80
Ports below 1024 need root privileges, so Node should listen on 3000 and Nginx should accept public traffic on 80 and forward it to the app.
sudo dnf install -y nginx
sudo systemctl enable --now nginx
Create /etc/nginx/conf.d/my-app.conf:
server {
listen 80;
server_name _;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
}
}
Test the configuration and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
Open http://<public-dns-name> in your browser. You should see the message from your app.
Step 8: Add HTTPS (optional, needs a domain)
HTTPS needs a certificate for a domain name. A common approach is:
- Point a DNS record for your domain at the instance’s public address. Use an Elastic IP (see below) so the address stays the same.
- Install Certbot, which obtains free Let’s Encrypt certificates, and run it for Nginx.
You can also put an Application Load Balancer in front of the instance and attach an AWS Certificate Manager (ACM) certificate. That is more setup, but it is common in production.
Give the instance a stable address
A public IP address changes when you stop and start an instance. To keep one address, allocate an Elastic IP in the EC2 console and associate it with your instance. Since February 2024, AWS charges about $0.005 per hour for every public IPv4 address, including Elastic IPs. That is roughly $3.65 a month if it stays allocated all month.
Keep secrets out of your code
- Read settings from environment variables, for example
process.env.PORT. - Do not commit
.envfiles or passwords to Git. - For sensitive values, use AWS Systems Manager Parameter Store or AWS Secrets Manager.
Troubleshooting
-
The site does not load in the browser. Check that the security group allows port 80 from your address, that Nginx is running (
sudo systemctl status nginx) and that the app is running (pm2 status). -
You see the Nginx welcome page. The default server block in
/etc/nginx/nginx.confis answering requests. Comment out thatserverblock, then runsudo systemctl reload nginx. -
npm ciornpm installstops partway or is killed. A 1 GiB instance can run out of memory during installs. Add a swap file and try again:sudo fallocate -l 1G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile -
The app stops after you log out. Start it with PM2 (step 6), not with
node app.jsalone. -
Port 80 needs root for Node. Keep Node on port 3000 and let Nginx listen on 80. Do not run the Node process as root.
Clean up
Stopping an instance does not stop every charge. EBS volumes and Elastic IPs keep billing. To stop all compute charges, terminate the instance. Then check the EC2 console for leftover volumes, snapshots and Elastic IPs, and delete any you no longer need.
Next steps
- If your app needs a database, read the guide to setting up an Amazon RDS database. Connect to it from the app with settings from environment variables.
- Prefer containers? Compare this with deploying a Docker container on AWS ECS.
- New to the platform? The AWS fundamentals overview explains the services used here.
- Before you leave the instance running, read the guide to avoiding unexpected AWS charges.