易于部署和更新的 Node.js 设置 [英] Node.js setup for easy deployment and updating

查看:19
本文介绍了易于部署和更新的 Node.js 设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在为客户开发一个网站(Apache 下的 TYPO3),该网站由 node.js/socket.io 应用程序支持,该应用程序为 CMS 提供的内容提供实时更新.

We're currently developing a website (TYPO3 under Apache) for a customer that is supported by a node.js/socket.io application that provides realtime updates to the content served from the CMS.

由于这是我们的第一个 node.js 项目,我没有任何关于完美设置"的最佳实践,所以我花了一些时间研究部署技术.

As this is our first node.js project I don't have any best practices to go by when it comes to 'the perfect setup' so I've spent some time researching deployment techniques.

要实现良好的设置,我还有几个问题:

A couple of questions remain for me to achieve a good setup which:

  1. 客户易于部署.这非常重要,因为我们的网站将集成到他们的实时"TYPO3 安装中,该安装为大量网站提供服务,并在不由客户管理的服务器上运行,而是另一个(集中式)组织,该组织可拨打支持电话和更改服务器过程缓慢.

  1. Is easy for the customer to deploy. This is very important because our website will be integrated in their 'live' TYPO3 installation which serves an abundance of websites and is running on servers which aren't managed by the customer but another (centralized) organization which makes support calls and server changes a slow process.

应该很容易更新. 如前所述,请求重新启动和进行服务器更改是一个缓慢的过程,因此理想情况下,节点安装应该在收到推送到服务器上的更改时重新启动/更新使用 git 实时安装.

Should be easy to update. As mentioned requesting restarts and making server changes is a slow process, so idealy the node installation should restart / update when it receives changes that are pushed onto the live installion using git.

部署

普遍共识似乎是永远使用 在部署节点应用程序以保持它们运行时.我已经测试了 forever,当通过 npm install ever-g(全局)安装时它似乎工作正常.不过,这需要外部协助才能在实时环境中全局安装,所以我更愿意让它从应用程序的 node_modules 目录中运行,但我无法创建一个可靠的包装器来做所以.

The general consensus seems to be to use forever when it comes to deploying node applications to keep them running. I've tested forever, and it seems to work fine when installed by npm install forever -g (global). This would require external assistance to globally install on the live environment though, so I'd prefer to have it running from the application's node_modules directory, but I haven't been able to create a solid wrapper to do so.

此外,forever 工作正常,但必须手动启动.确保它在服务器启动时启动并继续运行的最佳方法是什么?

Additionally, forever works fine, but it has to be started manually. What would be the best approach to ensure that it gets started on server boot and keeps running?

  • 一个简单的 init.d 脚本?
  • 编写看门狗包装器?
  • 检查 forever 状态的 TYPO3 调度程序任务?
  • A simple init.d script?
  • Writing a watchdog wrapper?
  • A TYPO3 scheduler task that checks forever status?

快速开发/更新后重启

我们目前仍处于项目的开发阶段,每次我对 node.js 应用程序进行更改时,我都会手动重新启动 nodeforever.这有效,但远非理想.有几个较小的 npm 模块可以检查文件修改并在检测到更改时重新启动 node,例如:

We're currently still in the development stage of the project and every time I make changes to the node.js application I manually restart node or forever. This works, but is far from ideal. There are several smaller npm modules that check for file modifications and restart node upon detected changes, like:

  • Nodemon
  • Node.js Supervisor
  • Bounce
  • Nodules (which doesn't require restarting node, so might be easier to combine with forever)
  • Up

有人有这方面的经验吗?

Does anyone have experience with any of these?

更新:为什么不直接使用集群?

集群模块通过reload 机制,但 不适用于 Node 0.5+.替换它的 核心集群模块(Node 0.6+) 没有所有这些功能,但是只提供聚类.反过来,与 socket.io 不兼容.至少不能不使用Redis(这对我们来说是个问题,因为我们可以't 强制向客户提供另一项先决服务).

The Cluster module provides similar functionality through the reload mechanism, but doesn't work with Node 0.5+. The core Cluster module (Node 0.6+) that replaced it doesn't have all these features but only provides clustering. Which in turn doesn't play well with socket.io. At least not without using Redis (which is a problem for us, because we can't force another prereq service to the customer).

--

显然,在将项目移交给客户之前,我正在尝试找到最稳定的解决方案,将更新重新启动器与 forever 结合起来,我真的希望任何人都制作出经过验证的组合技术.

Obviously I'm trying to find the most stable solution that combines an update-restarter with forever before handing over the project to the customer and I'm really hoping anyone has produced a proven combination of techniques.

推荐答案

结合所有收集到的知识(非常感谢 Julian Knight 的想法)和过去一周测试的方法,我决定采用下面描述的部署解决方案(我想我很乐意分享以帮助其他人解决类似问题):

Combining all knowledge gathered (Big thanks to Julian Knight for the ideas) and methods tested in the past week, I've decided to settle for the deployment solution described below (I thought I'd be nice to share to help others with comparable questions):

脚本错误时自动重启脚本更改时自动重新加载由 forever,因为它还包括脚本监视,只要 Forever 是从 node.js 脚本中生成的.

Auto-restarting on script errors and automatic reloading on script changes is handled by forever, as it also includes a script watch, as long as Forever is spawned from within a node.js script.

为此,我添加了一个 server.js 来启动我们实际想要运行的 app.js 脚本:

To do so, I've added a server.js to launch the app.js script we actually want to run:

server.js

var forever = require('forever'),
    child = new(forever.Monitor)('app.js', {
        'silent': false,
        'pidFile': 'pids/app.pid',
        'watch': true,
        'watchDirectory': '.',      // Top-level directory to watch from.
        'watchIgnoreDotFiles': true, // whether to ignore dot files
        'watchIgnorePatterns': [], // array of glob patterns to ignore, merged with contents of watchDirectory + '/.foreverignore' file
        'logFile': 'logs/forever.log', // Path to log output from forever process (when daemonized)
        'outFile': 'logs/forever.out', // Path to log output from child stdout
        'errFile': 'logs/forever.err'
    });
child.start();
forever.startServer(child);

这会监视应用程序目录中的所有文件的更改,并在发生更改时立即重新启动在 forever 中运行的脚本.由于日志和 pidfile 位于应用程序的子目录中,因此必须从文件监视中忽略它们,否则脚本将循环重新启动:

This watches all files in the application directory for changes and restarts the script running in foreveras soon as one changes. Because the logs and pidfile are in subdirectories of the application, those have to be ignored from the file watch, or the script will loop restarts:

.foreverignore

pids/**
logs/**

为了让这一切在系统启动时开始,并使我们能够使用 start node-appstop node-app 轻松控制服务,我们使用 Ubuntu 的新贵.我结合了两个例子(thisthis 一个)成为一个很好的工作:

To make this all start on system boot and enabling us to easily control the service using start node-appand stop node-app we use Ubuntu's Upstart. I've combined two examples (this and this one) into one that does the job quite well:

/etc/init/node-app.conf

# This is an upstart (http://upstart.ubuntu.com/) script
# to run the node.js server on system boot and make it
# manageable with commands such as
# 'start node-app' and 'stop node-app'
#
# This script is to be placed in /etc/init to work with upstart.
#
# Internally the 'initctl' command is used to manage:
# initctl help
# initctl status node-app
# initctl reload node-app
# initctl start node-app

description "node.js forever server for node-app"
author      "Remco Overdijk <remco@maxserv.nl>"
version "1.0"

expect fork

# used to be: start on startup
# until we found some mounts weren't ready yet while booting:

start on started mountall
stop on shutdown

# Automatically Respawn:
respawn
respawn limit 99 5

env HOME=/home/user/node-app-dir

script
    # Not sure why $HOME is needed, but we found that it is:
    export HOME=$HOME
    chdir $HOME
    exec /usr/local/bin/node server.js > logs/node.log &
end script

#post-start script
#   # Optionally put a script here that will notifiy you node has (re)started
#   # /root/bin/hoptoad.sh "node.js has started!"
#end script

正如 Kevin 在他的文章中明智地提到,以 root 身份运行 node 是不明智的,因此我们将在下周迁移到新服务器时将其更改为 exec sudo -u www-data/usr/local/bin/node.

As Kevin wisely mentions in his article it's unwise to run node as root, so we'll change that to exec sudo -u www-data /usr/local/bin/node when we move to new servers next week.

因此,foreverupstart 启动的 node server.js 自动启动,并监控崩溃和文件更改,保持整个设置只要我们想要就可以运行.

So, forever gets started automatically by node server.js which gets launched by upstart, and monitors for crashes and file changes, keeping the entire setup running as long as we want.

我希望这对任何人都有帮助.

I hope this helps anyone.

这篇关于易于部署和更新的 Node.js 设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆