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

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

问题描述

您可以通过我的问题告诉您,我是新手...

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

我建立了第一个网站,建立了第一个Node.js服务器服务,然后将所有内容推送到EC2上。

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

我在EC2 IP地址上测试了所有内容,一切似乎都可以正常工作。

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

到现在为止,我一直在本地测试我的应用程序,因此有意义的是,每当关闭终端时,app.js都会停止运行,因此在本地主机上将无法提供任何服务。

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.

现在,我的服务器位于EC2上,只要关闭终端,就会发生同样的事情(显然有人会说..)。

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

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

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 :)

我读了一些有关名为 forever的节点模块的信息,但我想知道(是新的和所有..)为什么 forever功能不是Node.js-EC2系统的默认设置吗?

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 ?

我的意思是,如果我错了,请纠正我,但是不是。设置Web服务器并将其推向现场以使其永远保持在线状态的整个目的是什么?这不是服务器应该做什么(无限监听请求)吗?如果是这种情况,为什么我们需要额外的模块/设置来实现这一目标?

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

编辑(您可能需要一些详细信息)-安装后我在EC2上的应用程序是我在终端上执行的步骤(顺便说一下,该应用程序在 Amazon Linux 上运行):

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) :


  • 我在上键入ssh -i xxxxxxxxxxx.pem ec2-user@ec2-xx-xx-xx-x.eu-west-1.compute.amazonaws.com
    终端

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

登录到亚马逊计算机后,我转到相关文件夹并执行节点app.js

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

机器中有3个文件夹: node node_modules *我的应用名称*

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

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

之后,该站点在我的EC2上运行IP

After that, the site goes live on my EC2 IP

关闭终端后,一切都将关闭

Once I close the terminal, everything is switched off

推荐答案

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

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/

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

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

希望这会有所帮助,

James

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

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