如何让 EC2 上的 Node.js 服务器永远运行? [英] How do I leave Node.js server on EC2 running forever?

查看:25
本文介绍了如何让 EC2 上的 Node.js 服务器永远运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我的问题可以看出,我是新手...

我建立了我的第一个网站,我设置了我的第一个 Node.js 服务器来为其提供服务,然后将所有内容推送到 EC2 上.

我测试了我的 EC2 IP 地址上的所有内容,似乎一切正常.

到现在为止,我一直在本地测试我的应用程序,因此每当我关闭终端时,app.js 都会停止运行,因此不会在 localhost 上提供任何服务.

现在我的服务器在 EC2 上,每当我关闭终端时都会发生同样的事情(显然"有人可以说..).

所以我的问题是如何让我的 Node.js 服务器在 EC2 上运行,就像......永远......这样我的网站保持活动......永远:)

我读了一些关于名为永远"的节点模块的内容,但我想知道(新的和所有..)为什么这个永远"功能不是 Node.js-EC2 系统的默认设置?

我的意思是,如果我错了,请纠正我,但是设置 Web 服务器并推送它以使其永远存在的全部意义不是吗?这不是服务器应该做的事情(无限地监听请求)吗?如果是这样,为什么我们需要额外的模块/设置来实现这一点?

感谢您的帮助.. 如您所知,我不仅在寻找解决方案,而且还在寻找解释,因为我真的很困惑.. :-)

编辑(您可能需要的一些细节) - 在 EC2 上安装我的应用程序后,这些是我在终端上遵循的步骤(该应用程序在 Amazon Linux 顺便说一句):

  • 我在终端

  • 登录亚马逊机器后,我会转到相关文件夹并执行 node app.js

  • 机器中有 3 个文件夹:nodenode_modules*我的应用程序名称*

  • app.js 驻留在 *我的应用程序名称*

  • 此后,该站点在我的 EC2 IP 上上线

  • 一旦我关闭终端,一切都会关闭

解决方案

在调用 Node.js 之前,运行以下命令:

屏幕

这将创建一个持久环境,让您的进程在断开连接后继续运行.

当您重新连接时,您可以使用此命令重新连接到该环境:

screen -r

这是一个随机链接,可以了解有关屏幕的更多信息:

http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/

但是,如果您的 EC2 实例重新启动,这将无济于事.有很多不同的方法可以做到这一点.将启动命令添加到/etc/rc.local 是一种方法.这是亚马逊指南的链接,其中包括向/etc/rc.local 添加内容.

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/building-shared-amis.html

希望这会有所帮助,

詹姆斯

As you can tell by my question, I'm new to this...

I built my first website, I set up my first Node.js server to serve it and then pushed everything live on EC2.

I tested everything on my EC2 IP address and everything seems to be working.

Now up until now, I've been testing my app locally so it makes sense that whenever I closed the terminal, app.js would stop running so nothing would be served on localhost.

Now that my server is on EC2, the same thing happens ("obviously" one could say..) whenever I close my terminal.

So my question is how do I keep my Node.js server running on EC2 for like... forever..so that my site stays live.. forever :)

I read something about a node module called "forever" but I'm wondering (being new and all..) why isn't this "forever" functionality a default setting of the Node.js-EC2 system ?

I mean, correct me if I'm wrong, but isn't the whole point of setting up a web server and pushing it live to have it stay live forever? Isn't that what servers are supposed to do anyway (infinitely listening for requests) ? And if that's the case why do we need extra modules/settings to achieve that ?

Thanks for your help.. As you can tell I'm not only looking for a solution but an explanation as well because I got really confused.. :-)

EDIT (a few details you might need) - After installing my app on EC2 these are the steps that I follow on the terminal (The app is running on Amazon Linux by the way) :

  • I type ssh -i xxxxxxxxxxx.pem ec2-user@ec2-xx-xx-xx-x.eu-west-1.compute.amazonaws.com on the terminal

  • After logging onto the Amazon machine I then go to the relevant folder and execute node app.js

  • There are 3 folders in the machine : node, node_modules and *name of my app*

  • app.js resides in *name of my app*

  • After that, the site goes live on my EC2 IP

  • Once I close the terminal, everything is switched off

解决方案

Before you invoke Node.js, run the command:

screen

This will create a persistent environment which will allow your process to keep running after you disconnect.

When you reconnect, you can use this command to reconnect to that environment:

screen -r

Here's a random link to learn more about screen:

http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/

However, this won't help you if your EC2 instance restarts. There are many different ways to do that. Adding your startup command to /etc/rc.local is one way. Here's a link to an Amazon guide which includes adding something to /etc/rc.local.

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/building-shared-amis.html

Hope this helps,

James

这篇关于如何让 EC2 上的 Node.js 服务器永远运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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