Thursday, 20 July 2017

thumbnail

How run nodejs app with pm2 in Ubuntu-14.04 Aws instance

How run nodejs app with pm2 in Ubuntu-14.04 Aws instance


In this tutorial we will run nodejs app in ubuntu server with the help of pm2 package.

  • Your application will restart if it crashes, and it will keep a log  of unhandled exceptions.
  • Your application will restart when your server starts for .e.g it will run as a service.
1. Install the git in your Ubuntu-14.04 instance  :- 

sudo apt-get install git -y

2. Install nodejs with the below command :-

sudo apt-get install python-software-properties
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash - 
sudo apt-get install nodejs -y 


Verify if with the following command 

node -v 
npm -v

3. Now we have install pm2 package with help of npm :-

NPM is a package manager that you will use to install frameworks and libraries to use with your Node.js applications. NPM was installed with Node.js. PM2 is a sweet little tool that is going to solve two problems for you:


  • It is going to keep your site up by restarting the application if it crashes. These crashes should NOT happen, but it is good know that PM2 has your back. (Some people may be aware of Forever.js, another tool that is used to keep node based sites running - I think you will find that PM2 has a lot to offer.)

  • It is going to help you by restarting your node application as a service every time you restart the server. Some of use know of other ways to do this, but pm2 makes it easier, and it has some added flexibility.

sudo npm install pm2 -g



4. Create a simple node js app for testing purpose :-

Create a file called server.js and paste the following code  in it.

vim server.js

var http = require('http');
var server = http.createServer(function (request, response) {
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.end("Hello World\n");
});
server.listen(80);
console.log("Server running at http://127.0.0.1:80/");

save and exit the file 

In this scenario we are running server on port 80 if you want to configure it on different port you can change the port number.

To this application you can run node command but this will interrupted when you exit the terminal.

 
As you can see in the above image server is running on port 80 but if you interrupt the terminal may be it will stop the server. So to do it in better way we have to configure with pm2 package. 

PM2 will take care of node server and it will automatically restart the service when it is stopped.

sudo pm2 start server.js


You can also put it on startup with the following command :-

sudo pm2 startup

If you have multiple app running on node then you can list with the following command :-

sudo pm2 list

Lastly you can test in the browser with your public ip and domain name :-


 That's it 
Enjoy using it 
Please do Comment, Likes and Share. 

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

Follow by Email