How to Setup Node.js Persistent Applications on Ubuntu 18.04

Based on the Chrome V8 Engine, Node.js is a popular language used to build highly scalable applications. He is already involved in many projects. Node.js applications are popular for their scalability. Running multiple concurrent processes on multiple servers reduces latency and increases uptime. This article will guide you through how you can set up saving an application. The guide targets Ubuntu 18.04 and also 20.04 servers.

Install Node.js.

In order to install Node.js you need to enter the following:

apt-get update
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
apt-get install -y nodejs

Install Yarn

Yarn is Facebook’s very easy-to-use package manager for Node.js packages. Recommended for installation! This can be done as follows:

curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
apt-get update && apt-get install yarn

Install Node.js app

If your application is hosted in a git repository, you need to clone it and then use the command:

yarn install

Then you install Process Manager 2. This is a special package that automatically launches applications. It is a very reliable tool; proof of this is that this tool is used by PayPal, Microsoft. You can install it using the following command:

yarn global add pm2

The next step is to add the application as your personal process. It will run in background mode. To do this, you can use the following command:

pm2 start [path to your entry.js] -i max

Automatically Launch the App at Boot Mode

After starting the application, save the application configuration. Then run the startup command to automatically start Process Manager 2 in the background when you boot Ubuntu. For Ubuntu 16.04 Process Manager 2 will bind to systemd

pm2 save
pm2 startup 

After successfully entering these commands, the Node.js application will crash due to an error, it will restart then immediately automatically. However, if Ubuntu crashes for any reason, the process will automatically restart from Ubuntu

Was this article helpful?

Related Articles