Search code examples
node.jsexpressamazon-ec2production-environment

Node.js express application production deployment


Let's say I have a nodejs+express+mongodb web application and now I want to go live using an AWS EC2 machine running with a linux distribution (I'm using ubuntu server 14.04, would you use something different?).

What would be the basic steps to have a decent production environment into a single tier instead of we usually use on a dev environment?

Would you use a http server (nginx,apache,..) ? What would be your firewall configuration? What else would you consider before going public (nodejs&express configurations,etc..)? What tools would you use (e.g. forever)? What other system configurations would you consider?

The focus of this question is not on performance, do not consider high levels of machine load and application scaling.


Solution

  • I have implemented production environments for several commercial applications on AWS, most recently using Node/Express/Mongo. Through lots of investigation, trial and error, and a bit of scripting I've settled on a configuration that works for me.

    Below is a summary of the services/utilities I use. By design, everything is widely available and well documented so you should have no problem finding discussions on how to configure each to your specific requirements.

    AWS Services

    • EC2 Security Group: primary firewall
    • ELB/Autoscaler (optional): load balancing; add/remove instances based on load; enable SSL
    • Cloudwatch: monitor instance availability
    • SNS: specify notification methods for Cloudwatch alarms (e-mail/sms)

    OS/Utilities

    • Ubuntu: the most recent LTS version, currently 14.04; configured to do weekly automatic updates using cron/apt-get
    • nginx: reverse proxy to node process; 50x error handling; serve /public files; enable SSL if not using ELB
    • Upstart: start/stop app for maintenance; respawn app if it crashes (ala forever); start app on reboot
    • Monit: monitor node process for extended outages (handles upstart respawn failure); monitor filesystem free space; monitor mongo process

    I use the standard apt-get versions of node, express, and npm with no special config settings. For deploying app updates, I have written a cron script that checks a github branch, so releasing a new version is triggered automatically by committing to that branch.

    There are of course many more options and these are by no means the definitive ones.... in other words, YMMV. However, in my experience these are fairly easy to configure, work reliably, keep things running smoothly, and notify you quickly if/when problems come up that require your attention.