如何将 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
How do I run a Node.js application as its own process?
提问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 foreverforever stop example.jsto stop the process, orforever stop 0to stop the process with index 0 (as shown byforever 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:
简而言之:
回答by nickleefly
回答by nponeccop
You can use monit, forever, upstartor systemdto start your server.
您可以使用monit,forever,upstart或systemd来启动服务器。
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 &来防止您的应用程序终止与您的服务器,但是forever,monit和其他建议的解决方案更好。
回答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
It covers things like http-proxy, SSLand Socket.IO.
它涵盖了诸如 http-proxy、SSL和Socket.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 中阅读如何为Debian和Ubuntu执行此操作。
回答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 轻松完成。

