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.
sudo -sand type in your password. Root access is required to modify the startup file so that the process manager starts on boot.
node -vto check the version installed. If Node.js is not on the correct version, the following commands will install it.
$ sudo apt-get update $ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash - $ sudo apt-get install -y nodejs
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.
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.
If your application is hosted in a git repository, clone it and use
yarn install. Your application should have a server entry file, usually
index.js, which will be used in the next steps.
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
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.
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
$ 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
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
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.
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
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.