5 ms·
How to deploy your node app on Linux, 2016 edition
- kybernetikos 11y agoIf you want to run multiple node apps on a single box, you might find bouncy useful too: https://github.com/substack/bouncy https://github.com/substack/bouncy
- deleted 11y ago[deleted]
- tomcam 11y agoBoy, this is golden. Just the ticket for a reluctant devops noobster like me. Bonus for explaining wheel in *nix.
- jimmcslim 11y agoSurprised 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 11y agoAuthor 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.
- hardwaresofton 11y agoHuge 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.
- tangue 11y agoAs 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 11y agoIt's not necessary but I do indeed prefer to reverse proxy with nginx to leverage nginx's superlative performance/caching features + http2 support.
- nailer 11y agoFor 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).
- nomailing 11y agoDon't you think nginx is better for serving static files? I first served directly croon node with express and did a test with chrome with a simulated modem connection speed. A simultaneous fast client was blocked because the number of connection nodejs/express was exhausted by the slow modem connection getting a lot of js files... I think I also read many other blogs suggesting serving static files with nginx in front of nodejs. Are you sure that this does not make sense?
- nailer 11y agoIt's not so much a matter of 'better' vs 'worse' as a balance between complexity and speed. node has very fast IO - that's what it's known for - and unlike Python or Ruby it can handle static files at speed: http://i.stack.imgur.com/amngX.png http://i.stack.imgur.com/amngX.png (from the second post at http://stackoverflow.com/questions/9967887/node-js-itself-or-nginx-frontend-for-serving-static-files http://stackoverflow.com/questions/9967887/node-js-itself-or... which provides some excellent discussion). nginx is faster, but small projects that don't need load balancing may benefit from having less software to install. As the article mentions, you'll probably want a load balancer for high availability. Whether you use haproxy or nginx for that is a whole different discussion.
- daleharvey 11y agoAfter 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 11y agoSure - 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 11y agoDokku 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.
- edelans 11y agoFor hassle-free node apps deployment, I use and recommend PM2 : a [free and open source tool](https://github.com/Unitech/pm2 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.
- jorge-d 11y agoPM2 also provides a tool [1] to deploy your application directly into your server, rollback & co. You can checkout this blogpost [2]. [1] http://pm2.keymetrics.io/docs/usage/deployment/ http://pm2.keymetrics.io/docs/usage/deployment/ [2] https://keymetrics.io/2014/06/25/ecosystem-json-deploy-and-iterate-faster/ https://keymetrics.io/2014/06/25/ecosystem-json-deploy-and-i...
- humbleMouse 11y agoPM2 is amazing!
- joshmn 11y agoIs there a ruby alternative?
- xorcist 11y agoPlease 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 11y ago> * 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.
- kiallmacinnes 11y ago>> * 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. Sure, but it also advises users that it's generally OK to extract tarballs into /usr/local. While this clearly is OK in some circumstances, the target audience for this article clearly aren't in a position to make that decision, or understand the consequences of doing this. The article could have suggested "/opt/node" for example and avoided the issue at the expense of explaining how PATH variables work.
- emilsedgh 11y agoDoes 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.
- illyism 11y agoIt makes sense if you want to load balance or port forward or anything similar.
- icebraining 11y agoAs 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.
- emilsedgh 11y agoIf 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 11y agoWhy is Docker not being used here?
- nailer 11y agoMainly 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.
- ehartsuyker 11y agoCan I shamelessly plug[0] and say maybe do all this and stick it in a Debian package if you're running Debian, or like most of the cloud world, Ubuntu. [0] https://github.com/ehartsuyker/node-deb https://github.com/ehartsuyker/node-deb
- CalmStorm 11y agoIf 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.