Deploying NodeJS enterprise applications

Using Linux native tools to manage application deployment and proper running

Sharing is caring!

by Ezequiel Parada

06/29/2015

When we started to develop NodeJS applications, our main concern was how to deploy, distribute, manage and maintain the applications in a production environment.

In a local environment, you have full control over all the aspects of the OS, including user permissions, applications installed, etc. However, when working in an enterprise environment, you have to deal with several constraints.

The problem:

We didn’t have a proper way to deploy the applications. At first, we just copied the files, installed all the required NPM modules (and any other required library) and just run the program. Using different NodeJS versions for different applications in the same server was almost impossible. We couldn’t use any tool to handle several NodeJS versions installed at the same time (like NVM). The application files (other than the JS code itself) were scattered all over the host. The configuration and log files were stored inside the application folder, instead of being in a more common location.

Because of these problems, the whole thing was a mess, we needed to organize the whole deployment process, focusing on the following aspects:

  • Standardizing folder structure and file location.

  • Running the application as a service.

  • Simplifying maintenance tasks, log rotation, version upgrading.

  • Complying with Linux applications standards.

The Solution:

First of all, we decided to use Linux native tools to manage the application deployment and proper running.

We defined a proper file location:

  • Application Files: /var/lib/<app-name>

  • Configuration: /etc/<app-name>/conf.json

  • Logs: /var/log/<app-name>/app.log

  • Upstart settings: /etc/init/<app-name>.conf

  • Logrotate settings: /etc/logrotate.d/<app-name>

  • NodeJS installations:/var/lib/node/<nodejs-version>

  • NPM Cache: /var/lib/node/.npm

Logrotate

With Logrotate, we define the log rotation and what to do with the old log files.

Logrotate config file example:

Debian software package

We used Debian Software Package to accomplish the following:

  • Enforcing the file location.

  • Checking and Installing NodeJS versions.

  • Creating an application exclusive user.

  • Installing NPM dependencies having full control over the files created.

This was done through two of the files store inside a DEB file, preinst and postinst.

Preinst:

With this file we run a script to check if the NodeJS version needed is already installed. If that’s not the case, we access the archives of nodejs.org, download the installation package and extract it in a predefined path. We also check/create a user (with the application name as username).

Postinst:

In this config file we configure the NPM cache folder location, to centralize all the modules in case we install more than one NodeJS app in the same server. After that, we run NPM to install every dependency. Once the NPM process is done, we configure the ownership of all the files involved in the running (application, configuration, log files and home folder). To conclude we stop the service and restart with the new changes.

Example of a shell script to generate the .DEB Package:

Upstart

With Upstart, we handle the way the application is being executed. Setting it as a Linux service, and making it executable when the server is rebooted. In the config file we also define: The NodeJS version used to execute. The user that is running the application (this will always be the already created user). The log file that will store the information written by the application in the standard out and standard error.

Upstart script example:

Hope this is helpful! What other techniques or tools do you use or know?

More articles to read

Previous blog post

Web Technologies

08/10/2015

Build responsive websites more easily

Read the complete article

Next blog post

Web Technologies

05/04/2015

Using ElasticSearch with Grails

Read the complete article