≡ Menu

How to Deploy Node.js Script on Linux Using Upstart and Monit

Node.js LogoNode.js is a server-side scripting and Javascript framework.

This runs on top of Chrome V8 Engine.

What makes it different is that the application developed using node can be seen as a server responding to your request. When scripting using node.js, we are creating a http server which keeps on responding to your request.

In other words, when you are running a nodejs application you are running an instance of a http server.

Its quite different from the scenario where you install a separate web server like Apache or Nginx with appropriate language compiler packages and then deploying your code under the DocumentRoot of that webserver.

When something goes wrong, due to high load or some unhandled exception in the script, not only the application crashes, in a way your instance of web server itself is crashing. So you need to keep monitoring your application and may need to re-start it if any thing goes wrong.

If you are new to Node.js: How to Install Node.js and NPM on Linux from Binaries or NodeSource

You should use some kind of mechanism that can respawn the node process when needed. This can be done by using forever, nodemon or crontab until and unless your host system itself is not getting restarted.

In such scenarios we need something more effective.

The following are the two high-level steps to get this done:

  1. Set your node application to start with system service managers or daemons.
  2. Use some tools which watches the linux daemon and use that to control your Node.js app

Step 1: Setup a Process manager (Upstart) for your Node.js App

We can use the upstart to make the node process to be managed by daemons or say run in background. You will not need to keep the terminal open always. Also you will be able to get easily configurable start/restart/stop scripts.

Certain version of RHEL based distros, such as Red Hat, Ubuntu, CentOS and Fedora already comes with Upstart.

You can install Upstart on Ubuntu and Debain based distros using the following command:

sudo apt-get install upstart

Below is the example of the configuration file kept in “/etc/init/” folder.

#/etc/init/myNodeApp.conf

description "Some Node App"
author "TheGeekStuff"

start on startup  #You can also start on various run-levels e.g. "start on runlevel[23]"
stop on shutdown

respawn

script
  cd /home/project
  exec sudo -u thegeekstuff NODE_ENV=production /usr/bin/node /home/project/app.js >> /home/project/log/app.log 2>&1
end script

In the above configuration file on line no 4 we are telling to run the script on system startup. Here we can also explicitly mention the various unix run levels on which the app is to be started. The line “respawn” tells to restart the process if it crashes.

The configuration inside the “script” scope tells to get inside your project folder and initiate your app with a log on file and console as output.

Refer to: How to Use Node.js with MySQL using Example for Connection Pool

Step 2: Monitor Your Node.js App Using Monit (The watcher)

We can use monit as its name suggests, to monitor the daemon process, filesystems, directories, localhost and also the various process characteristics like memory and CPU cycles associated with a process.
So it can be used to automate the actions to be taken, for a wide range of scenarios which are associated with not only an application crash but also like high resource utilization, security breach and file changes.

Using it we can configure tests that will be evaluated at certain intervals.

You can install the monit on Ubuntu and Debain based system using the following command:

sudo apt-get install monit

Following is an example Monit configuration done in “/etc/monit/monitrc ”

check host myNodeApp with address 127.0.0.1
  start "/sbin/start myNodeApp"
  stop "/sbin/stop myNodeApp"
  if failed port 2001 protocol HTTP
  	request /
  	with timeout 5 seconds
  	then restart
  if 5 restarts within 5 cycles then timeout

The above scripts keeps checking the local host with an http request and if request fails it restarts the node app.

You should now configure upstart to start monit when your system restarts. This way most of your Node.js App failure scenarios can be handled.

Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book