如何将 Node.js 应用程序作为自己的进程运行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4681067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:48:55  来源:igfitidea点击:

How do I run a Node.js application as its own process?

node.jsservicedeploymentdaemon

提问by respectTheCode

What is the best way to deploy Node.js?

部署 Node.js 的最佳方式是什么?

I have a Dreamhost VPS (that's what they call a VM), and I have been able to install Node.js and set up a proxy. This works great as long as I keep the SSH connection that I started node with open.

我有一个 Dreamhost VPS(这就是他们所说的VM),而且我已经能够安装 Node.js 并设置代理。只要我保持打开节点时的 SSH 连接,这就会很好用。

采纳答案by mikemaccana

2016 answer: nearly every Linux distribution comes with systemd, which means forever, monit, PM2, etc. are no longer necessary - your OS already handles these tasks.

2016 年回答:几乎每个 Linux 发行版都带有 systemd,这意味着永远不再需要 monit、PM2 等 - 您的操作系统已经处理了这些任务

Make a myapp.servicefile (replacing 'myapp' with your app's name, obviously):

制作一个myapp.service文件(显然,用您的应用程序名称替换“myapp”):

[Unit]
Description=My app

[Service]
ExecStart=/var/www/myapp/app.js
Restart=always
User=nobody
# Note Debian/Ubuntu uses 'nogroup', RHEL/Fedora uses 'nobody'
Group=nobody
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/myapp

[Install]
WantedBy=multi-user.target

Note if you're new to Unix:/var/www/myapp/app.jsshould have #!/usr/bin/env nodeon the very first line.

请注意,如果您是 Unix 新手:/var/www/myapp/app.js应该#!/usr/bin/env node在第一行。

Copy your service file into the /etc/systemd/systemfolder.

将您的服务文件复制到该/etc/systemd/system文件夹中。

Tell systemd about the new service with systemctl daemon-reload.

告诉 systemd 新服务systemctl daemon-reload

Start it with systemctl start myapp.

systemctl start myapp.

Enable it to run on boot with systemctl enable myapp.

启用它以在启动时运行systemctl enable myapp

See logs with journalctl -u myapp

查看日志 journalctl -u myapp

This is taken from How we deploy node apps on Linux, 2018 edition, which also includes commands to generate an AWS/DigitalOcean/Azure CloudConfig to build Linux/node servers (including the .servicefile).

这取自我们如何在 Linux 上部署节点应用程序,2018 版,其中还包括生成 AWS/DigitalOcean/Azure CloudConfig 以构建 Linux/节点服务器(包括.service文件)的命令。

回答by David Tang

Use Forever. It runs Node.js programs in separate processes and restarts them if any dies.

永远使用。它在单独的进程中运行 Node.js 程序,并在任何进程终止时重新启动它们。

Usage:

用法:

  • forever start example.jsto start a process.
  • forever listto see list of all processes started by forever
  • forever stop example.jsto stop the process, or forever stop 0to stop the process with index 0 (as shown by forever list).
  • forever start example.js开始一个过程。
  • forever list查看永久启动的所有进程的列表
  • forever stop example.js停止进程,或forever stop 0停止索引为 0 的进程(如 所示forever list)。

回答by Ben

I've written about my deployment method here: Deploying node.js apps

我在这里写了我的部署方法:部署 node.js 应用程序

In short:

简而言之:

  • Use git post-receive hook
  • Jakefor the build tool
  • Upstart as a service wrapper for node
  • Monit to monitor and restart applications it they go down
  • nginxto route requests to different applications on the same server
  • 使用 git post-receive 钩子
  • 构建工具的Jake
  • 新贵作为节点的服务包装器
  • Monit 监视和重新启动应用程序,如果它们出现故障
  • nginx将请求路由到同一服务器上的不同应用程序

回答by nickleefly

pm2does the tricks.

pm2有技巧。

Features are: Monitoring, hot code reload, built-in load balancer, automatic startup script, and resurrect/dump processes.

功能有:监控、热代码重载、内置负载均衡器、自动启动脚本和复活/转储进程。

回答by nponeccop

You can use monit, forever, upstartor systemdto start your server.

您可以使用monitforeverupstartsystemd来启动服务器。

You can use Varnish or HAProxy instead of Nginx (Nginx is known not to work with websockets).

您可以使用 Varnish 或 HAProxy 代替 Nginx(已知 Nginx 不能与 websockets 一起使用)。

As a quick and dirty solution you can use nohup node your_app.js &to prevent your app terminating with your server, but forever, monitand other proposed solutions are better.

作为一种快速而肮脏的解决方案,您可以使用它nohup node your_app.js &来防止您的应用程序终止与您的服务器,但是forevermonit和其他建议的解决方案更好。

回答by Capy

I made an Upstart script currently used for my apps:

我制作了一个当前用于我的应用程序的 Upstart 脚本:

description "YOUR APP NAME"
author "Capy - http://ecapy.com"

env LOG_FILE=/var/log/node/miapp.log
env APP_DIR=/var/node/miapp
env APP=app.js
env PID_NAME=miapp.pid
env USER=www-data
env GROUP=www-data
env POST_START_MESSAGE_TO_LOG="miapp HAS BEEN STARTED."
env NODE_BIN=/usr/local/bin/node
env PID_PATH=/var/opt/node/run
env SERVER_ENV="production"

######################################################

start on runlevel [2345]
stop on runlevel [016]

respawn
respawn limit 99 5

pre-start script
    mkdir -p $PID_PATH
    mkdir -p /var/log/node
end script

script
    export NODE_ENV=$SERVER_ENV
    exec start-stop-daemon --start --chuid $USER:$GROUP --make-pidfile --pidfile $PID_PATH/$PID_NAME --chdir $APP_DIR --exec $NODE_BIN -- $APP >> $LOG_FILE 2>&1
end script

post-start script
    echo $POST_START_MESSAGE_TO_LOG >> $LOG_FILE
end script

Customize all before #########, create a file in /etc/init/your-service.conf and paste it there.

在######### 之前自定义所有内容,在 /etc/init/your-service.conf 中创建一个文件并将其粘贴到那里。

Then you can:

然后你可以:

start your-service
stop your-service
restart your-service
status your-service

回答by Rich Jones

I've written a pretty comprehensive guide to deploying Node.js, with example files:

我写了一个非常全面的部署 Node.js 的指南,包含示例文件:

Tutorial: How to Deploy Node.js Applications, With Examples

教程:如何使用示例部署 Node.js 应用程序

It covers things like http-proxy, SSLand Socket.IO.

它涵盖了诸如 http-proxy、SSLSocket.IO 之类的内容

回答by Seldaek

If you have root access you would better set up a daemon so that it runs safe and sound in the background. You can read how to do just that for Debianand Ubuntuin blog post Run Node.js as a Service on Ubuntu.

如果您有 root 访问权限,您最好设置一个守护进程,以便它在后台安全运行。您可以在博客文章Run Node.js as a Service on Ubuntu 中阅读如何为DebianUbuntu执行此操作。

回答by Ruben Vermeersch

Here's a longer article on solving this problem with systemd: http://savanne.be/articles/deploying-node-js-with-systemd/

这是一篇关于用 systemd 解决这个问题的较长文章:http: //savanne.be/articles/deploying-node-js-with-systemd/

Some things to keep in mind:

要记住的一些事情:

  • Who will start your process monitoring? Forever is a great tool, but it needs a monitoring tool to keep itself running. That's a bit silly, why not just use your init system?
  • Can you adequately monitor your processes?
  • Are you running multiple backends? If so, do you have provisions in place to prevent any of them from bringing down the others in terms of resource usage?
  • Will the service be needed all the time? If not, consider socket activation (see the article).
  • 谁将开始您的过程监控?Forever 是一个很棒的工具,但它需要一个监控工具来保持自己的运行。这有点傻,为什么不直接使用你的 init 系统呢?
  • 您能否充分监控您的流程?
  • 您是否运行多个后端?如果是这样,您是否有适当的规定来防止其中任何一个在资源使用方面降低其他人?
  • 服务会一直需要吗?如果没有,请考虑套接字激活(请参阅文章)。

All of these things are easily done with systemd.

所有这些事情都可以通过 systemd 轻松完成。

回答by Marak

Foreverwill do the trick.

永远都会成功。

@Kevin: You should be able to kill processes fine. I would double check the documentation a bit. If you can reproduce the error it would be great to post it as an issue on GitHub.

@Kevin:您应该能够很好地终止进程。我会仔细检查一下文档。如果您可以重现该错误,最好将其作为问题发布到 GitHub 上。