STEP 1: ADD REQUIRED REPO FROM OFFICIAL NODEJS WEBSITE.
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -STEP 2: INSTALL NODEJS.
sudo yum install -y nodejsSTEP 3: VERIFY INSTALLED VERSION.
node -v
npm -vSTEP 4: CREATE A server.js FILE AND RUN IT.
Create a file named server.js with the provided the below content.
const http = require('http');
const fs = require('fs');
const path = require('path');
const server = http.createServer((req, res) => {
const filePath = path.join(__dirname, 'index.html');
const stat = fs.statSync(filePath);
res.writeHead(200, {
'Content-Type': 'text/html',
'Content-Length': stat.size,
});
const readStream = fs.createReadStream(filePath);
readStream.pipe(res);
});
const PORT = 80;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
this script creates a basic web server that listens on port 80, reads the content of an index.html file, and sends it as the response when a request is made to the server. The server logs a message indicating that it is running on http://localhost:80.
STEP 5: INSTALL REQUIRED PACKAGES / INSTALL DEPENDENCIES.
npm installSTEP 6: NODE.JS WILL INTERPRET AND EXECUTE THE CODE IN THE server.js FILE. THIS IS A COMMON PATTERN FOR RUNNING SERVER-SIDE JAVASCRIPT APPLICATIONS.
node server.jsAfter following these steps, your Node.js server should be up and running.
Make sure your ec2 instance security group io opened with required port i.e; 80/3000/any toher port
========================================================================================================================
PM2 useful Commands
Command to install pm2
npm install -g pm2list all running processes/apps.
pm2 list (or) pm lsStart app/services with name
pm2 start server.js --name "my-app1"Command to list the logs
pm2 logs id/nameex: pm2
Command to Monitor the resources
pm2 monitTo make service as startup
pm2 startupCommand to delete service/app
pm2 delete name/id