简体   繁体   English

Node.js Express应用程序生产部署

[英]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?). 假设我有一个nodejs + express + mongodb Web应用程序,现在我想使用运行Linux发行版的AWS EC2计算机上线(我正在使用ubuntu服务器14.04,您会使用其他功能吗?)。

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,..) ? 您会使用http服务器(nginx,apache,..)吗? What would be your firewall configuration? 您的防火墙配置是什么? What else would you consider before going public (nodejs&express configurations,etc..)? 在公开之前,您还会考虑什么(nodejs&express配置等)? What tools would you use (eg 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. 这个问题的重点不是性能,不要考虑高级别的机器负载和应用程序扩展。

I have implemented production environments for several commercial applications on AWS, most recently using Node/Express/Mongo. 我已经在AWS上为多个商业应用程序实现了生产环境,最近一次使用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 AWS服务

  • EC2 Security Group : primary firewall EC2安全组 :主防火墙
  • ELB/Autoscaler (optional) : load balancing; ELB / Autoscaler(可选) :负载平衡; add/remove instances based on load; 根据负载添加/删除实例; enable SSL 启用S​​SL
  • Cloudwatch : monitor instance availability Cloudwatch :监视实例可用性
  • SNS : specify notification methods for Cloudwatch alarms (e-mail/sms) SNS :指定Cloudwatch警报的通知方法(电子邮件/短信)

OS/Utilities OS /公用事业

  • Ubuntu : the most recent LTS version, currently 14.04; Ubuntu :最新的LTS版本,当前为14.04; configured to do weekly automatic updates using cron/apt-get 配置为使用cron / apt-get每周执行一次自动更新
  • nginx : reverse proxy to node process; nginx :节点进程的反向代理; 50x error handling; 50x错误处理; serve /public files; 提供/ public文件; enable SSL if not using ELB 如果不使用ELB,则启用SSL
  • Upstart : start/stop app for maintenance; Upstart :启动/停止应用程序进行维护; respawn app if it crashes (ala forever); 重新崩溃(如果永久崩溃)的应用程序; start app on reboot 重新启动时启动应用
  • Monit : monitor node process for extended outages (handles upstart respawn failure); Monit :监视节点进程是否出现长时间中断(处理新贵重生失败); monitor filesystem free space; 监视文件系统的可用空间; monitor mongo process 监控蒙哥进程

I use the standard apt-get versions of node, express, and npm with no special config settings. 我使用node,express和npm的标准apt-get版本,没有特殊的配置设置。 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. 为了部署应用程序更新,我编写了一个cron脚本来检查github分支,因此通过提交到该分支会自动触发发布新版本。

There are of course many more options and these are by no means the definitive ones.... in other words, YMMV. 当然,还有更多选择,而这些绝不是确定的选择。换句话说,就是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. 但是,以我的经验,这些配置非常容易,可以可靠地工作,保持运行平稳,并且在出现问题时(需要引起您的注意)迅速通知您。

I think Tom's answer was great. 我认为汤姆的答案很棒。 In addition to that, some things to consider. 除此之外,还需要考虑一些事项。

MongoDB on AWS AWS上的MongoDB

There are some things to further consider when you host your MongoDB instance. 托管MongoDB实例时,还有一些事情需要进一步考虑。 Firstly, you're likely using a pretty standard EBS volume, but for your production database its really important to reduce latency involve with EBS. 首先,您可能会使用相当标准的EBS卷,但是对于您的生产数据库,减少EBS的延迟非常重要。 Mongodb recommends getting EBS optimized instances, and using EBS volumes with provisioned IOPS. Mongodb建议获得EBS优化的实例,并使用带有预配置IOPS的EBS卷。

Provisioned IOPS EBS volumes are typically prefered over Ephemeral storage, because when you destroy an instance with Ephemeral storage you lose that data. 与使用临时存储相比,通常更喜欢使用预配置IOPS EBS卷,因为使用临时存储破坏实例时,该数据会丢失。 However EBS is separate from your instance, and provide various other features which can come in handy for managing your mongodb cluster. 但是,EBS与您的实例是分开的,并且提供了许多其他功能,这些功能可用于管理mongodb集群。 Though Ephemeral storage will likely be more performant, you should be aware of the potential for losing data if you delete that instance. 尽管临时存储的性能可能更高,但是您应该意识到删除该实例可能会丢失数据。

I had the great experience with working with the guys at MongoDB for a short period of time while working for a client of mine, and you should absolutely use MMS to manage/monitor your mongoDB instances. 在为我的客户工作时,我与MongoDB的工作人员有短暂的合作经验,并且您绝对应该使用MMS来管理/监视mongoDB实例。 It's free, unless you want backups, so theres no reason not to. 它是免费的,除非您需要备份,所以没有理由不这样做。 Monit may monitor your systems but it will not monitor or report the health of your mongoDB cluster. Monit可能会监视您的系统,但不会监视或报告mongoDB集群的运行状况。

Process Management 流程管理

Upstart is a great way to maintain availability of your node. Upstart是维护节点可用性的好方法。 However, not everyone is good at writing upstart scripts. 但是,并非所有人都擅长编写新贵的脚本。 Many people write really minimal upstart scripts. 很多人写的暴发户脚本很少。 As others recommend, PM2 is a great process manager. 正如其他人所建议的那样,PM2是出色的流程经理。 PM2 will allow hot-reloading your app on deployment, providing zero downtime deployments. PM2将允许在部署时热重新加载您的应用程序,从而提供零停机时间部署。 But back to the subject, PM2 can start itself as a daemon, and PM2 persists your deployment therefore being able to give you the benefits provided by upstart/init.d and you don't have to worry about writing and testing your upstart script. 但是回到主题,PM2可以作为守护程序启动,而PM2可以保留您的部署,因此能够为您提供upstart / init.d提供的好处,而您不必担心编写和测试您的upstart脚本。

Some extra sugar is that PM2 provides its own REST api(optionally) and web interface for managing your instances(reload, restart, stop, load/mem/cpu, etc). PM2提供了一些额外的好处,即它提供了自己的REST api(可选)和用于管理实例的Web界面(重新加载,重新启动,停止,加载/内存/ cpu等)。

Edit: PM2 can only hot reload if you are clustering. 编辑:如果正在群集,PM2仅可以热重载。 You'll need at least 2 nodes on a host to use clustering, and you'll want a CPU for each node. 主机上至少需要2个节点才能使用群集,并且每个节点都需要一个CPU。 This may not be in your favor, however you can still use PM2 to upstart your app. 这可能不利于您,但是您仍然可以使用PM2来启动您的应用程序。

Content Distribution and Static Assets 内容分配和静态资产

You may already know its best not to server static assets with node . 您可能已经知道最好不要使用node服务器静态资产 This is typically where Nginx comes to the rescue. 这通常是Nginx进行救援的地方。 Nginx provides other benefits, as Tom stated in his answer(50x errors, SSL). 正如Tom在回答中所说的那样,Nginx还提供了其他好处(50x错误,SSL)。 Its worth noting that AWS offers CloudFront a Content Distribution Network. 值得注意的是,AWS为CloudFront提供了内容分发网络。 While you may not be able to get all of your static assets on to cloudfront, its worth storing larger and non-compressible files like images on cloudfront especially if you expect to have a large number of mobile users. 尽管您可能无法将所有静态资产都放到Cloudfront上,但是值得在Cloudfront上存储较大且不可压缩的文件(例如图像),尤其是在您希望拥有大量移动用户的情况下。 This will just bring your content closer to your end user. 这只会使您的内容更接近最终用户。

References: 参考文献:

I've recently created/installed a nodejs+express+mongodb webapplication using OpenShift and it's hosted at AWS. 我最近使用OpenShift创建/安装了nodejs + express + mongodb Web应用程序,该应用程序托管在AWS上。 It's free and I'm only using two gears at the moment (Mongo + web). 它是免费的,目前我只使用两个齿轮(Mongo + Web)。 Haven't had to configure any virtual firewalls/zones like I would have if I'd spun them up directly at AWS EC2. 无需像直接在AWS EC2上启动虚拟防火墙/区域那样配置任何虚拟防火墙/区域。

Seems to be working out for me so far and a lot less hassles than my work earlier mucking about in the AWS console. 到目前为止,似乎对我来说正在努力,并且比我先前在AWS控制台中苦苦挣扎的工作少了很多麻烦。

You should use git deployment procedure as described in http://nvie.com/files/Git-branching-model.pdf 您应按照http://nvie.com/files/Git-branching-model.pdf中所述使用git部署过程

For production you should also have well formatted and organised config files to make deployment easier. 对于生产环境,您还应该具有格式正确且组织良好的配置文件,以使部署更加容易。

Use Pm2 module to cluster ,load balance and forever run different nodes. 使用Pm2模块进行群集,负载平衡并永久运行不同的节点。

Apache is a great server you can use that. Apache是​​一个很棒的服务器,您可以使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM