top | item 10835688

How to deploy your node app on Linux, 2016 edition

134 points| nailer | 10 years ago |certsimple.com | reply

46 comments

order
[+] xorcist|10 years ago|reply
Please note that this describes something you would do for a toy project. If you do this for work you probably want to:

* Install NPM in a way that you can upgrade easily. Your package manager was designed to do this, so use it. Never ever put untracked files in the system directories.

* Don't git clone into production. You need to know which version was deployed when and where. At the very least set a tag. Better yet, roll a package from that tag (see above) which you then can sign and store. It's very easy and there are tools to help.

* Schedule when and how you upgrade your operating system, NPM, and your application. To do this you need a way to take an application out of production, which brings us to...

* You generally want a load balancer or some sort of web server between the world and your application. This could be as simple as an Apache or nginx. Don't muck about with port forwards!

* And most importantly, document this down to every command in the internal wiki! Even better, write a Puppet/Salt/Ansible file and put it under version control.

In short: Tools exist for a reason. Use them. Don't hack files manually until you master the tools and know why they exist.

[+] nailer|10 years ago|reply
> * Install NPM in a way that you can upgrade easily. Your package manager was designed to do this, so use it. Never ever put untracked files in the system directories.

The articles uses the package manager - specifically, the article directs people to install node using nodesource's RPM and dpkg node packages (npm is included with node), and only mentions tarballs if these don't exist for your OS.

> * Schedule when and how you upgrade your operating system, NPM, and your application. To do this you need a way to take an application out of production, which brings us to... > You generally want a load balancer or some sort of web server between the world and your application.

Hence covering that and load balancing with HAProxy in the article. This is mentioned in the introduction.

> * And most importantly, document this down to every command in the internal wiki! Even better, write a Puppet/Salt/Ansible file and put it under version control.

Hence mentioning exactly that in the opening few paragraphs.

It's very clear you didn't read the article before commenting.

[+] pavlov|10 years ago|reply
Tools exist for a reason. Use them. Don't hack files manually until you master the tools and know why they exist.

I feel it's often the other way around: don't hack around with big powerful tools until you know what the underlying problem is and what steps are actually needed to solve it.

[+] joshmn|10 years ago|reply
Lock down your damn firewall too.
[+] edelans|10 years ago|reply
For hassle-free node apps deployment, I use and recommend PM2 : a [free and open source tool](https://github.com/Unitech/pm2)

As its doc states: PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

It also provides a very nice integration with keymetrics.io, which is the paid service which finance PM2 development (they do provide a free tier).

PS: Apart from using it and loving it, I am not affiliated with this product's team.

[+] joshmn|10 years ago|reply
Is there a ruby alternative?
[+] jimmcslim|10 years ago|reply
Surprised that this is missing a recommendation to run 'npm shrinkwrap' and include npm-shrinkwrap.json in your deployment package so that the dependencies you have been testing against (and more importantly your dependencies' dependencies) don't shift unintentionally under your feet. Although maybe this isn't a problem with npm v3?

I'd also recommend including your dependencies in your deployment package anyway to avoid a deployment being held up by npmjs.org downtime.

[+] nailer|10 years ago|reply
Author here! You're totally right, I've just added a shrinkwrap section.

PS. npm v3 still needs it, in fact it makes it better - `install --save` updates the shrinkwrap file.

Re: checking in modules, I used to do this during 2013 when npm was up and down every few days, but stopped a couple of years ago after npm Inc stabilised everything. It's been solid so far and the smaller repo sizes (and faster deploys) have been worth it.

[+] tangue|10 years ago|reply
As someone who never deployed a node app I was a bit surprised to see it doesn't require a server like Apache or Nginx as a reverse proxy ? Is it a commmon practice to deploy node apps like this ?
[+] wanda|10 years ago|reply
It's not necessary but I do indeed prefer to reverse proxy with nginx to leverage nginx's superlative performance/caching features + http2 support.
[+] nailer|10 years ago|reply
For most cases you want load balancing, hence HAProxy. But if you don't, node has event driven IO like nginx does, so it's quite capable of things like static file serving and https (like nginx, node uses openssl for all the hard work).
[+] tomcam|10 years ago|reply
Boy, this is golden. Just the ticket for a reluctant devops noobster like me. Bonus for explaining wheel in *nix.
[+] hardwaresofton|10 years ago|reply
Huge thanks for taking the time to write and document this process. This is bound to be a great resource for numerous devs in the future.
[+] emilsedgh|10 years ago|reply
Does it make sense to reverse proxy a node app behind nginx if the node app only provides a REST API?

As far as I know, using nginx in front helps with serving static files, which is a moot point on a REST API.

[+] icebraining|10 years ago|reply
As far as I know, using nginx in front helps with serving static files, which is a moot point on a REST API.

Well, that depends on the API; for example, a product inventory API might need to serve many product images, a document management API might have to serve PDFs and such, etc.

[+] illyism|10 years ago|reply
It makes sense if you want to load balance or port forward or anything similar.
[+] daleharvey|10 years ago|reply
After a few years of deploying apps in a reasonably similiar way to this, I switched to using dokku for deploying node applications recently, the experience so far has been extremely nice.

I get `git push dokku master` deployment for free, for any application, I dont have to worry about conflicting node versions between applications and it took a lot less setup for all of my applications than this process describes for one.

[+] nailer|10 years ago|reply
Sure - as discussed in the article, the steps of deploying a server should only be performed once, and customised to your needs.

From then on you can - and should - deploy everything with Ansible playbooks, AWS AMIs / Digital Ocean images, Dockerfiles, or whatever else. You've already been doing this for years and have the experience - this is written for developers who haven't done a lot of Linux before and want to take control of the process.

[+] Killswitch|10 years ago|reply
Dokku is great if you want to host all apps on a single server. I had used it for a while.

Switched to using github to push to, which fires off a build on a ci server, which fires off a push to docker registry and my server pulls that and rolls the versions.

This setup allows me to have everything on a single server until an app starts needing it's own, then I deploy a new CoreOS server and transfer the fire system to that one.

[+] CalmStorm|10 years ago|reply
If the node app uses express js and has multiple processes, then a redis server may be required to handle non-stikcy session. It would be nice if the article describes the redis server setup.
[+] emilsedgh|10 years ago|reply
If you are deploying to a single server, consider using `pm2 deploy` which makes life much easier for rolling new releases and deploying them on server.
[+] cdnsteve|10 years ago|reply
Why is Docker not being used here?
[+] nailer|10 years ago|reply
Mainly because I didn't want it to be a Docker/kubernetes tutorial. At some point I'll publish an Ansible playbook you can modify for your environment and call from a Dockerfile.