A simple Node.js web server app deployed on an AWS EC2 instance. This project demonstrates the fundamental process of deploying a Node.js app on a cloud server.
nodejs-deploy-example/
βββ index.js # Main Node.js server script
βββ README.md # Project documentation
- Node.js (v18+)
- npm (v10+)
- Git
- AWS EC2 Instance (Ubuntu)
- Nginx (optional for reverse proxy)
- PM2 (optional for process manager)
git clone https://github.com/GeigerJR/nodejs-deploy-example.git
cd nodejs-deploy-examplesudo apt update
sudo apt install -y nodejs npmnano index.jsPaste the code below:
const http = require('http');
const PORT = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.end('Hello from your Node.js server on EC2!');
});
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});Save and exit: CTRL + O, ENTER, then CTRL + X
node index.jsThen visit:
http://<your-ec2-public-ip>:3000
Install and use PM2 to keep the app running even after terminal logout:
sudo npm install -g pm2
pm2 start index.js
pm2 save
pm2 startupInstall Nginx:
sudo apt install nginxConfigure reverse proxy:
sudo nano /etc/nginx/sites-available/defaultReplace the location / block:
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}Restart Nginx:
sudo systemctl restart nginxVisit your EC2 Public IP in the browser (http://<your-ec2-ip>).
Create the README:
nano README.mdPaste in all content from this note, then:
git add README.md
git commit -m "Add full project documentation"
git push origin main