Intro

Ok, so we use lots of nodejs and angularjs framework for website development. So, In this time some use yarn to run and use npm. So what happend is it's run in live front of you, but when you close the terminal it's stop. So, we need to run that in background. for that we use PM2. It's a nodejs service to run nodejs or angularjs process in background. But using pm2 running yarn is bit difficult. So here is the solution comes in.

Let's Started

Imagine you have a nodejs project you want to run in a new server so let's get in setup.

I am assuming my project nodejs version is nodejs 10. So, I am going to install that version and using Ubuntu 18 for OS.

  • Installing CURL
sudo apt install curl -y
  • After Install done. Let's download the installer using curl
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
  • Now Let's install nodejs.
sudo apt install nodejs

Ok So we have install the nodejs 10 in our system. and It's come with npm too.

  • Now Let's install yarn
npm install -g [email protected]
  • Now install pm2
npm install pm2 -g

So now we are ready to go with process. We have upload our project and we have install all other important module for the project.

Now our project will be start with this command

yarn run:server

But it's run on terminal if I close the terminal it's close the server. So, I need to run that in background. So we are going to use pm2 to run that in the background. But pm2 to need bash file to run any command in the background and we need to run that in any host. Sometime angularjs or nodejs check the host and block on run in any domain so we are going write the command in that way.

so we are going to write the program in this way.

  • We are going to make a bash file. I am giving name is server.sh
  • Now I am going to write this command using my nano editor.
yarn run:server --host 0.0.0.0 --disable-host-check true
  • Now let's save and let's run it with pm2. Now I am going to enter a command for run with pm2 in background
pm2 start server.sh

Done. Now my project is running in background and with any host with host check disable. Thank you for be with me.

*Note: PM2 can run any kind of .sh file or any kind of app.js file run in the background. for more information about command and running check the link https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/