mskw
mskw

Reputation: 10328

Best practice to run a Node.js server on background on Cloud VM's

I'm currently running a node.js server on Google Compute Engine, however it always shuts down when I log out.

Is there a best practice for keeping the server on the background? I don't want to just run try first thing I find and use it, as I don't know the consequences for reliability etc.

I've seen people use nohup, and forever. But Im not sure how professional servers does it, say twitter/facebook servers.

Upvotes: 1

Views: 686

Answers (2)

Misha Brukman
Misha Brukman

Reputation: 13424

Node Process Manager

You can use pm2, a process manager for Node.js, to run applications in the background for production deployment.

As the project's README says:

$ npm install pm2 -g
$ pm2 start app.js

Your app is now put in background, monitored and kept alive forever.

You can see more info, features, screenshots, etc. on the project's home page.

Managed VMs

As an alternative, since you're using Google Cloud Platform, take a look at Google Managed VMs which can run and manage your Node.js apps (in addition to other languages)—see the Getting started guide for Node.js for more info.

Note: As of this writing (3 Dec 2015), Managed VMs are in beta, which means:

This is a Beta release of Managed VMs. This feature is not covered by any SLA or deprecation policy and may be subject to backward-incompatible changes

Upvotes: 2

Paul
Paul

Reputation: 36319

So, this would be the same in most server environments, though details vary a bit based on what operating system you're using.

The general components you're looking at are going to be your web server (e.g. apache, nginx), your process manager (e.g. pm2 or init.d), and your application.

The web server is optional, but recommended in production environments as it can handle things like caching and static file serving with better efficiency in most cases than doing it in your node process.

The process manager is what will handle keeping your process running when you log off (and to restart it if the server reboots or the process crashes); generally they work by running the app as a no-logon service account (which is ideal anyway since you can neck down permissions that way).

There's a step-by-step example for CentOS here: https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-centos-7 if you would like the command-line info on how to do it.

Upvotes: 3

Related Questions