如何将 Node.js 应用程序作为自己的进程运行?
部署 Node.js 的最佳方式是什么?
我有一个 Dreamhost VPS(他们称之为 VM),并且我已经能够安装Node.js 并设置代理。只要我保持启动节点的 SSH 连接处于打开状态,这种方法就非常有效。
What is the best way to deploy 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
2016 年答案:几乎每个 Linux 发行版都附带了 systemd,这意味着forever、monit、PM2 等不再需要 - 您的操作系统已经处理这些任务。
创建一个
myapp.service
文件(显然,将“myapp”替换为您的应用程序名称):如果您是 Unix 新手,请注意:
/var/www/ myapp/app.js
应该在第一行有#!/usr/bin/env 节点
并打开可执行模式chmod +x myapp.js.
将服务文件复制到
/etc/systemd/system
文件夹中。使用 systemctl daemon-reload 告诉 systemd 有关新服务的信息。
使用
systemctl start myapp
启动它。使用
systemctl enable myapp
使其能够在启动时运行。使用
journalctl -u myapp
查看日志这取自我们如何在 Linux 2018 版上部署节点应用程序,其中还包括生成 AWS/DigitalOcean/Azure CloudConfig 来构建 Linux/节点服务器的命令(包括
.service
文件)。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.
Make a
myapp.service
file (replacing 'myapp' with your app's name, obviously):Note if you're new to Unix:
/var/www/myapp/app.js
should have#!/usr/bin/env node
on the very first line and have the executable mode turned onchmod +x myapp.js
.Copy your service file into the
/etc/systemd/system
folder.Tell systemd about the new service with
systemctl daemon-reload
.Start it with
systemctl start myapp
.Enable it to run on boot with
systemctl enable myapp
.See logs with
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
.service
file).使用永远。它在单独的进程中运行 Node.js 程序,并在任何进程终止时重新启动它们。
用法:
forever start example.js
启动一个进程。forever list
查看由 permanent 启动的所有进程的列表forever stop example.js
停止进程,或forever stop 0
停止进程索引为 0(如永久列表
所示)。Use Forever. It runs Node.js programs in separate processes and restarts them if any dies.
Usage:
forever start example.js
to start a process.forever list
to see list of all processes started by foreverforever stop example.js
to stop the process, orforever stop 0
to stop the process with index 0 (as shown byforever list
).我在这里写了关于我的部署方法:部署node.js应用程序
简而言之:
I've written about my deployment method here: Deploying node.js apps
In short:
pm2 可以解决这些问题。
功能包括:监控、热代码重载、内置负载均衡器、自动启动脚本和复活/转储进程。
pm2 does the tricks.
Features are: Monitoring, hot code reload, built-in load balancer, automatic startup script, and resurrect/dump processes.
您可以使用
monit
、forever
、upstart
或systemd
来启动服务器。您可以使用 Varnish 或 HAProxy 代替 Nginx(已知 Nginx 不能与 websocket 一起使用)。
作为一种快速而肮脏的解决方案,您可以使用
nohup node your_app.js &
来防止您的应用程序随服务器终止,但永远
、monit
和其他建议的解决方案更好。You can use
monit
,forever
,upstart
orsystemd
to start your server.You can use Varnish or HAProxy instead of Nginx (Nginx is known not to work with websockets).
As a quick and dirty solution you can use
nohup node your_app.js &
to prevent your app terminating with your server, butforever
,monit
and other proposed solutions are better.我制作了一个当前用于我的应用程序的 Upstart 脚本:
在 ######### 之前自定义所有内容,在 /etc/init/your-service.conf 中创建一个文件并将其粘贴到那里。
然后你可以:
I made an Upstart script currently used for my apps:
Customize all before #########, create a file in /etc/init/your-service.conf and paste it there.
Then you can:
我写了一份相当全面的 Node.js 部署指南,其中包含示例文件:
教程:如何部署 Node.js 应用程序,包含示例
它涵盖了 http-proxy、SSL 和 Socket.IO< /a>.
I've written a pretty comprehensive guide to deploying Node.js, with example files:
Tutorial: How to Deploy Node.js Applications, With Examples
It covers things like http-proxy, SSL and Socket.IO.
这是一篇关于使用 systemd 解决此问题的较长文章: http://savanne.be /articles/deploying-node-js-with-systemd/
需要记住的一些事情:
所有这些事情都可以使用 systemd 轻松完成。
Here's a longer article on solving this problem with systemd: http://savanne.be/articles/deploying-node-js-with-systemd/
Some things to keep in mind:
All of these things are easily done with systemd.
如果您具有 root 访问权限,您最好设置一个守护程序,以便它在后台安全可靠地运行。您可以阅读如何针对 Debian 和 Ubuntu 在博客文章在 Ubuntu 上将 Node.js 作为服务运行。
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 Debian and Ubuntu in blog post Run Node.js as a Service on Ubuntu.
永远就可以了。
@Kevin:你应该能够很好地终止进程。我会仔细检查一下文档。如果您可以重现该错误,最好将其作为问题发布在 GitHub 上。
Forever will 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.
试试这个:http://www.technology-ebay.de/the-teams/mobile-de/blog/deploying-node-applications-with-capistrano-github-nginx-and-upstart.html
使用 Capistrano、Upstart 和 Nginx 部署 Node.js 应用程序的精彩而详细的指南
Try this: http://www.technology-ebay.de/the-teams/mobile-de/blog/deploying-node-applications-with-capistrano-github-nginx-and-upstart.html
A great and detailed guide for deploying Node.js apps with Capistrano, Upstart and Nginx
正如 Box9 所说,Forever 是生产代码的不错选择。但即使 SSH 连接从客户端关闭,也可以保持进程继续运行。
虽然对于生产来说不一定是一个好主意,但在长时间的调试会话中,或者跟踪长时间进程的控制台输出,或者每当需要断开 SSH 连接但使终端在服务器中保持活动状态时,这非常方便稍后重新连接(例如在家中启动 Node.js 应用程序并在稍后工作时重新连接到控制台以检查情况如何)。
假设您的服务器是 *nix 盒子,您可以使用 shell 中的 screen 命令来保持即使客户端 SSH 关闭,进程仍在运行。如果尚未安装,您可以从网络下载/安装 screen(如果是 Linux,请查找适合您的发行版的软件包,或使用 MacPorts(如果操作系统为 X)。
它的工作原理如下:
如果需要,您可以像这样同时运行多个屏幕会话,并且可以从任何客户端连接到其中任何一个。在线阅读文档了解所有选项。
As Box9 said, Forever is a good choice for production code. But it is also possible to keep a process going even if the SSH connection is closed from the client.
While not necessarily a good idea for production, this is very handy when in the middle of long debug sessions, or to follow the console output of lengthy processes, or whenever is useful to disconnect your SSH connection, but keep the terminal alive in the server to reconnect later (like starting the Node.js application at home and reconnecting to the console later at work to check how things are going).
Assuming that your server is a *nix box, you can use the screen command from the shell to do keep the process running even if the client SSH is closed. You can download/install screen from the web if not already installed (look for a package for your distribution if Linux, or use MacPorts if OS X).
It works as following:
You can have multiple screen sessions running concurrently like this if you need, and you can connect to any of it from any client. Read the documentation online for all the options.
Forever 是保持应用程序运行的一个不错的选择(并且它可以作为模块安装 npm,这很好)。
但对于更严肃的“部署”——比如部署的远程管理、重新启动、运行命令等——我会使用带有节点扩展的 capistrano。
https://github.com/loopj/capistrano-node-deploy
Forever is a good option for keeping apps running (and it's npm installable as a module which is nice).
But for more serious 'deployment' -- things like remote management of deploying, restarting, running commands etc -- I would use capistrano with the node extension.
https://github.com/loopj/capistrano-node-deploy
https://paastor.com 是一项相对较新的服务,可以为您部署到 VPS 或其他服务器。有一个 CLI 可以推送代码。 Paastor 有一个免费套餐,至少在发布本文时是这样。
https://paastor.com is a relatively new service that does the deploy for you, to a VPS or other server. There is a CLI to push code. Paastor has a free tier, at least it did at the time of posting this.
根据您的情况,您可以使用 upstart 守护进程。对于完整的部署解决方案,我可能建议 capistrano。两个有用的指南是如何设置 Node.js env 和 如何通过 capistrano + upstart 进行部署< /em>。
In your case you may use the upstart daemon. For a complete deployment solution, I may suggest capistrano. Two useful guides are How to setup Node.js env and How to deploy via capistrano + upstart.
尝试 node-deploy-server。它是一个复杂的工具集,用于将应用程序部署到您的私有服务器上。它是用 Node.js 编写的,并使用 npm 进行安装。
Try node-deploy-server. It is a complex toolset for deploying an application onto your private servers. It is written in Node.js and uses npm for installation.
我在 Dreamhost 上使用 Express,并在后台
pm2 start app.js
上运行 pm2。方便的。可以查看日志,甚至保存日志文件pm2日志; mylogs.txt
,您可以配置 PM2 在超过某个内存使用阈值时自动重新启动进程。这有助于防止内存泄漏或内存过度使用。
I am using express on Dreamhost and got pm2 running on background
pm2 start app.js
. Convenient. Can view logs and even save log filespm2 logs <PM2_ID> mylogs.txt
,You can configure PM2 to automatically restart a process if it exceeds a certain memory usage threshold. This helps prevent memory leaks or excessive memory usage.