如何让 EC2 上的 Node.js 服务器永远运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26245942/
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 leave Node.js server on EC2 running forever?
提问by Sprout Coder
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.
我建立了我的第一个网站,我设置了我的第一个 Node.js 服务器来为其提供服务,然后将所有内容推送到 EC2 上。
I tested everything on my EC2 IP address and everything seems to be working.
我测试了我的 EC2 IP 地址上的所有内容,似乎一切正常。
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.
到现在为止,我一直在本地测试我的应用程序,因此每当我关闭终端时,app.js 都会停止运行,因此不会在 localhost 上提供任何服务。
Now that my server is on EC2, the same thing happens ("obviously" one could say..) whenever I close my terminal.
现在我的服务器在 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 :)
所以我的问题是如何让我的 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 ?
我读了一些关于名为“永远”的节点模块的内容,但我想知道(新的和所有......)为什么这个“永远”功能不是 Node.js-EC2 系统的默认设置?
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 ?
我的意思是,如果我错了,请纠正我,但设置 Web 服务器并推送它以使其永远存在的全部意义不是吗?这不是服务器应该做的事情(无限地监听请求)吗?如果是这种情况,为什么我们需要额外的模块/设置来实现这一目标?
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 Linuxby the way) :
编辑(您可能需要的一些细节) - 在 EC2 上安装我的应用程序后,这些是我在终端上遵循的步骤(Amazon Linux顺便说一下,该应用程序正在运行):
I type ssh -i xxxxxxxxxxx.pem [email protected] 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_modulesand*name of my app*app.jsresides in*name of my app*After that, the site goes live on my EC2 IP
Once I close the terminal, everything is switched off
我在终端上输入 ssh -i xxxxxxxxxxx.pem [email protected]
登录亚马逊机器后,我转到相关文件夹并执行 node app.js
机器中有 3 个文件夹:
node,node_modules和*name of my app*app.js居住在*name of my app*之后,该站点在我的 EC2 IP 上上线
一旦我关闭终端,一切都将关闭
回答by James Wilson
Before you invoke Node.js, run the command:
在调用 Node.js 之前,运行以下命令:
screen
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
screen -r
Here's a random link to learn more about screen:
这是一个随机链接,可以了解有关屏幕的更多信息:
http://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
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.
但是,如果您的 EC2 实例重新启动,这对您没有帮助。有很多不同的方法可以做到这一点。将启动命令添加到 /etc/rc.local 是一种方法。这是亚马逊指南的链接,其中包括向 /etc/rc.local 添加内容。
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/building-shared-amis.html
http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/building-shared-amis.html
Hope this helps,
希望这可以帮助,
James
詹姆士
回答by Juan Camilo Mejia
I worked with the valid answer for a while but some times the screen just end with no reason also screen has no balance loader and others features that in a production enviroment you should care , Currently I use a npm component to do this job.
我使用了一段时间的有效答案,但有时屏幕会无缘无故地结束,屏幕也没有平衡加载器和其他在生产环境中您应该关心的功能,目前我使用 npm 组件来完成这项工作。
https://www.npmjs.com/package/pm2
https://www.npmjs.com/package/pm2
This is so easy to use.
这很容易使用。
$ npm install pm2 -g
then just start your app with pm2 like this
然后像这样用 pm2 启动你的应用程序
$ pm2 start app.js
In the above link you can find diferents tasks to perform if you need.
在上面的链接中,您可以根据需要找到要执行的不同任务。
Hope this help the newbies like me.
希望对我这样的新手有所帮助。
回答by Joseph Juhnke
There's a better way. Use forever.js.
有更好的方法。使用永远.js。
See it here: https://github.com/foreverjs/forever
在这里看到它:https: //github.com/foreverjs/forever
This is a nice tutorial for how to use chkconfig with forever on CENTOS.
这是一个关于如何在 CENTOS 上永远使用 chkconfig 的很好的教程。
http://aronduby.com/starting-node-forever-scripts-at-boot-w-centos/
http://aronduby.com/starting-node-forever-scripts-at-boot-w-centos/
回答by Pritam Roy
Or use tmux Just Enter a tmux screen run node server Ctrl+b Hit D and you're done.
或者使用 tmux Just Enter a tmux screen run node server Ctrl+b Hit D 就大功告成了。
回答by Ritesh kumar Agrahari
I am very late to join the thread and seems its basic problem with every newbie. Follow the below to setup properly your first server.
我很晚才加入该线程,似乎每个新手都有基本问题。按照以下步骤正确设置您的第一台服务器。
follow the step on the ec2 instance(before doing this make sure you have a start script for pm2 in your package.json file):
按照 ec2 实例上的步骤操作(在执行此操作之前,请确保您的 package.json 文件中有 pm2 的启动脚本):
npm install pm2 -g
npm install pm2 -g
pm2 startup systemd
pm2 startup systemd
See the output and at the last line it must be like..
查看输出,在最后一行它必须像..
You have to run this command as root. Execute the following command: sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy
您必须以 root 身份运行此命令。执行以下命令: sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy
Take the last line command and run again with root privilege.
使用最后一行命令并以 root 权限再次运行。
(before running the next command, Provide a new start script for pm2 in your package.json file e.g: "pm2-start": "pm2 start ./bin/www")
(运行一个命令之前,在你的package.json文件,例如提供PM2一个新的启动脚本:"pm2-start": "pm2 start ./bin/www")
npm run pm2-start
npm run pm2-start
for more info follow the link.
欲了解更多信息,请点击链接。

