How to Setup Node.js Persistent Applications on Ubuntu 16.04

Published on: Fri, Apr 13, 2018 at 11:04 am EST
Linux Guides Node.js System Admin Ubuntu

Node.js applications are popular for their ability to scale. Running multiple concurrent processes on multiple servers yields lower latency and greater uptime. When managing multiple servers, it is easiest to have Node.js applications automatically restart if they crash. This tutorial will show you how to quickly set up application persistence from a blank Ubuntu 16.04 instance.

Prerequisites

  • Create a Vultr instance of any size with Ubuntu 16.04 LTS x64.
  • If you are not the root user, run sudo -s and type in your password. Root access is required to modify the startup file so that the process manager starts on boot.
  • Make sure Node.js is installed on the instance, and that the version of Node.js is 8.9.3 LTS or later, which is required for the persistence manager to run smoothly. Use node -v to check the version installed. If Node.js is not on the correct version, the following commands will install it.

Install Node.js.

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

Create swap partition

This step is optional, but recommended for the installation. Swap partitions are portions of the hard disk which are dedicated to helping supplement the RAM. On smaller Vultr instances, such as the 512MB RAM instance, creating a swap partition would be beneficial to performance. For an automated version of the swap tutorial, use the commands below.

$ git clone https://github.com/teamtofu/server-tools.git tools
$ bash ./tools/swap.sh

Generally speaking, using swap would not be necessary for instances with 4 GB of RAM or more when setting up persistent applications, although it may improve the applications’ performance. RAM is more responsive than swap, so instances with more RAM will tend yield better results regardless of swap.

Install Yarn

Yarn is an easy to use package manager from Facebook for Node.js packages. It is faster than NPM and also deterministic, which is especially useful when deploying multiple servers.

$ 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
$ sudo apt-get update && sudo apt-get install yarn

However, NPM can be used instead of Yarn, and it has comparable commands to all of those in this tutorial.

Install your Node.js application

If your application is hosted in a git repository, clone it and use yarn install. Your application should have a server entry file, usually server.js or index.js, which will be used in the next steps.

Install Process Manager 2

Process Manager 2 is the package which will auto-restart the application. Process Manager 2 is very popular, and it is used by Microsoft, PayPal, and Intuit, so it is secure and reliable.

$ yarn global add pm2

Add your application as a process

The application will be daemonized (run in the background) and auto-restarted with the start command.

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

If you want to launch the maximum number of processes automatically, pass the -i max argument. If the sole use of this Vultr instance is to serve the application, then this is recommended. For Vultr instances with more than one CPU, multiple processes will be launched, and Process Manager 2 will act as the load balancer for all of these individual processes.

Automatically start your application on boot

After starting the app, save the application’s configuration. Then, run the startup command to automatically run Process Manager 2 in the background when Ubuntu boots up. For Ubuntu 16.04, Process Manager 2 will bind to systemd.

$ pm2 save
$ pm2 startup

Now if your Node.js application crashes due to an error it will restart, and if Ubuntu crashes for any reason, the process will automatically restart with Ubuntu. The processes can be restarted with pm2 gracefulReload all and Process Manager 2 can be restarted with pm2 update. To stop Process Manager 2 from starting on boot, run pm2 unstartup.

Testing

Try typing reboot and checking to see if your application is served after Ubuntu boots up. If the application is for the web and served over port 80, then curl http://localhost/ will show the HTML for the starting page. In the case that it does not work, the command pm2 status will show the running processes, and the start command can be reissued.

Deploying new versions of the application

If the application is updated, such as by pulling a git repository, then a command must be issued to update the running application processes.

$ pm2 update

Example usage

Parse Server, a popular open-source platform as a service, serves as a backend for an application. The backend may crash due to errors in code or volume of requests, but the application will experience no downtime because the other servers (and/or processes) continue to run, and the Parse Server process is quickly restarted.

Want to contribute ?

You could earn up to $300 by adding new articles