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.
我建立了我的第一个网站,我建立了我的第一个节点。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 :)
我的问题是如何保持结点。在EC2上运行的js服务器。永远. .这样我的网站就能一直运行下去。永远:)
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 ?
我读到关于一个名为“永远”的节点模块的内容,但我在想,为什么这个“永久”功能并不是节点的默认设置。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 Linux
by the way) :
编辑(一些您可能需要的细节)——在EC2上安装我的应用后,这些是我在终端上遵循的步骤(顺便说一下,应用程序在Amazon Linux上运行):
-
I type ssh -i xxxxxxxxxxx.pem ec2-user@ec2-xx-xx-xx-x.eu-west-1.compute.amazonaws.com on the terminal
我输入ssh - xxxxxxxxxxx。在终端上的pem ec2-user@ec2-xx- x.eu-west-1 compute.amazonaws.com
-
After logging onto the Amazon machine I then go to the relevant folder and execute node app.js
登录到Amazon机器之后,我进入相关文件夹并执行node app.js
-
There are 3 folders in the machine :
node
,node_modules
and*name of my app*
机器中有3个文件夹:节点、node_modules和*名称*。
-
app.js
resides in*name of my app*
js驻留在我的app的*名称中
-
After that, the site goes live on my EC2 IP
之后,该站点将在我的EC2 IP上运行
-
Once I close the terminal, everything is switched off
一旦我关闭终端,一切都被关闭
5 个解决方案
#1
47
Before you invoke Node.js, run the command:
在您调用节点。js,运行以下命令:
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
屏幕- 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当地是一种方法。这里有一个指向Amazon指南的链接,其中包括向/etc/rc. local.com添加内容。
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
詹姆斯
#2
19
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.
希望这能帮助像我这样的新手。
#3
11
There's a better way. Use forever.js.
有一个更好的方法。使用forever.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/
#4
1
Or use tmux Just Enter a tmux screen run node server Ctrl+b Hit D and you're done.
或者使用tmux输入tmux屏幕运行节点服务器Ctrl+b按D就可以了。
#5
1
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实例上的步骤(在此之前,请确保在包中有pm2的启动脚本。json文件):
npm install pm2 -g
npm安装pm2 - g
pm2 startup systemd
pm2启动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启动系统d -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"
)
(在运行下一个命令之前,在包中为pm2提供一个新的开始脚本。json文件e。g:“pm2-start”:“pm2 start ./bin/www”)
mpm run pm2-start
mpm pm2-start运行
for more info follow the link.
更多信息请点击链接。
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04
#1
47
Before you invoke Node.js, run the command:
在您调用节点。js,运行以下命令:
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
屏幕- 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当地是一种方法。这里有一个指向Amazon指南的链接,其中包括向/etc/rc. local.com添加内容。
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
詹姆斯
#2
19
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.
希望这能帮助像我这样的新手。
#3
11
There's a better way. Use forever.js.
有一个更好的方法。使用forever.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/
#4
1
Or use tmux Just Enter a tmux screen run node server Ctrl+b Hit D and you're done.
或者使用tmux输入tmux屏幕运行节点服务器Ctrl+b按D就可以了。
#5
1
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实例上的步骤(在此之前,请确保在包中有pm2的启动脚本。json文件):
npm install pm2 -g
npm安装pm2 - g
pm2 startup systemd
pm2启动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启动系统d -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"
)
(在运行下一个命令之前,在包中为pm2提供一个新的开始脚本。json文件e。g:“pm2-start”:“pm2 start ./bin/www”)
mpm run pm2-start
mpm pm2-start运行
for more info follow the link.
更多信息请点击链接。
https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04