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

发布于 2024-10-12 04:18:14 字数 199 浏览 5 评论 0原文

部署 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

-黛色若梦 2024-10-19 04:18:14

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

创建一个 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=nogroup
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/myapp

[Install]
WantedBy=multi-user.target

如果您是 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):

[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=nogroup
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.js should have #!/usr/bin/env node on the very first line and have the executable mode turned on chmod +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).

尘世孤行 2024-10-19 04:18:14

使用永远。它在单独的进程中运行 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 forever
  • forever stop example.js to stop the process, or forever stop 0 to stop the process with index 0 (as shown by forever list).
最单纯的乌龟 2024-10-19 04:18:14

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

简而言之:

  • 使用 git post-receive hook
  • Jake 作为构建工具
  • Upstart 作为服务包装器让节点
  • Monit 监视并重新启动应用程序,它们会向下
  • nginx 将请求路由到不同的应用程序同一服务器

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

In short:

  • Use git post-receive hook
  • Jake for the build tool
  • Upstart as a service wrapper for node
  • Monit to monitor and restart applications it they go down
  • nginx to route requests to different applications on the same server
她说她爱他 2024-10-19 04:18:14

pm2 可以解决这些问题。

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

pm2 does the tricks.

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

少女情怀诗 2024-10-19 04:18:14

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

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

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

You can use monit, forever, upstart or systemd 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, but forever, monit and other proposed solutions are better.

瀞厅☆埖开 2024-10-19 04:18:14

我制作了一个当前用于我的应用程序的 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

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

然后你可以:

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

I made an Upstart script currently used for my apps:

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.

Then you can:

start your-service
stop your-service
restart your-service
status your-service
分開簡單 2024-10-19 04:18:14

我写了一份相当全面的 Node.js 部署指南,其中包含示例文件:

教程:如何部署 Node.js 应用程序,包含示例

它涵盖了 http-proxy、SSLSocket.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.

征棹 2024-10-19 04:18:14

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

需要记住的一些事情:

  • 谁将开始您的进程监控? Forever 是一个很棒的工具,但它需要一个监控工具来保持自身运行。这有点傻,为什么不直接使用你的 init 系统呢?
  • 您能充分监控您的流程吗?
  • 您正在运行多个后端吗?如果是这样,您是否有适当的规定来防止其中任何一个在资源使用方面降低其他的性能?
  • 是否一直需要该服务?如果没有,请考虑套接字激活(请参阅文章)。

所有这些事情都可以使用 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:

  • 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).

All of these things are easily done with systemd.

黯然#的苍凉 2024-10-19 04:18:14

如果您具有 root 访问权限,您最好设置一个守护程序,以便它在后台安全可靠地运行。您可以阅读如何针对 DebianUbuntu 在博客文章在 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.

享受孤独 2024-10-19 04:18:14

永远就可以了。

@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.

2024-10-19 04:18:14

试试这个: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

妥活 2024-10-19 04:18:14

正如 Box9 所说,Forever 是生产代码的不错选择。但即使 SSH 连接从客户端关闭,也可以保持进程继续运行。

虽然对于生产来说不一定是一个好主意,但在长时间的调试会话中,或者跟踪长时间进程的控制台输出,或者每当需要断开 SSH 连接但使终端在服务器中保持活动状态时,这非常方便稍后重新连接(例如在家中启动 Node.js 应用程序并在稍后工作时重新连接到控制台以检查情况如何)。

假设您的服务器是 *nix 盒子,您可以使用 shell 中的 screen 命令来保持即使客户端 SSH 关闭,进程仍在运行。如果尚未安装,您可以从网络下载/安装 screen(如果是 Linux,请查找适合您的发行版的软件包,或使用 MacPorts(如果操作系统为 X)。

它的工作原理如下:

  1. 当您第一次打开 SSH 连接时,输入“screen” - 这将启动您的 screen 会话。
  2. 开始正常工作(即启动 Node.js 应用程序)
  3. 完成后,关闭终端。您的服务器进程将继续运行。
  4. 要重新连接到控制台,请通过 ssh 返回服务器,登录,然后输入“screen -r”以重新连接。您的旧控制台上下文将弹出,可供您继续使用它。
  5. 要退出屏幕,请在连接到服务器时,在控制台提示符上键入“exit” - 这将使您进入常规 shell。

如果需要,您可以像这样同时运行多个屏幕会话,并且可以从任何客户端连接到其中任何一个。在线阅读文档了解所有选项。

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:

  1. When you first open the SSH connection, type 'screen' - this will start your screen session.
  2. Start working as normal (i.e. start your Node.js application)
  3. When you are done, close your terminal. Your server process(es) will continue running.
  4. To reconnect to your console, ssh back to the server, login, and enter 'screen -r' to reconnect. Your old console context will pop back ready for you to resume using it.
  5. To exit screen, while connected to the server, type 'exit' on the console prompt - that will drop you onto the regular shell.

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.

凡尘雨 2024-10-19 04:18:14

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

一人独醉 2024-10-19 04:18:14

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.

萌吟 2024-10-19 04:18:14

根据您的情况,您可以使用 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.

瑾兮 2024-10-19 04:18:14

尝试 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.

梦里兽 2024-10-19 04:18:14

我在 Dreamhost 上使用 Express,并在后台 pm2 start app.js 上运行 pm2。方便的。可以查看日志,甚至保存日志文件pm2日志; mylogs.txt

pm2 monit - monitoring metrics of app

pm2 list - table view of processes

您可以配置 PM2 在超过某个内存使用阈值时自动重新启动进程。这有助于防止内存泄漏或内存过度使用。

pm2 start app.js --name=myapp --max-memory-restart 200M

I am using express on Dreamhost and got pm2 running on background pm2 start app.js. Convenient. Can view logs and even save log files pm2 logs <PM2_ID> mylogs.txt,

pm2 monit - monitoring metrics of app

pm2 list - table view of processes

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.

pm2 start app.js --name=myapp --max-memory-restart 200M
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文