在一台Debian服务器上,我安装了Node.js。我知道如何用这个命令行从putty启动一个应用程序:

node /srv/www/MyUserAccount/server/server.js

并在地址50.51.52.53:8080 (IP和端口)上访问它。

但是当我一关闭putty,我就无法到达50.51.52.53:8080这个地址了。

如何让一个Node.js应用程序永久运行?

你可以猜到,我是Linux和Node.js的初学者。


当前回答

我推荐使用PM2,这是一个Node.js应用程序的进程管理器。PM2提供了一种简单的方法来管理和守护应用程序(将它们作为服务运行)。

参考这个链接- https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-centos-7

其他回答

如果你只是想在终端中运行你的节点应用程序,只需要使用screen。

安装在ubuntu/ debian上:

sudo apt-get install screen

用法:

$ screen
$ node /path/to/app.js

CTRL + a,然后CTRL + d取消

get就是回来:

一屏:screen -r

如果有多个屏幕,可以使用screen -ls列出所有屏幕

然后:screen -r pid_number

我发现永远都做得很好。

假设你已经安装了npm,如果没有,就这样做

sudo apt-get install npm

然后永远安装

npm install forever --global

现在你可以这样运行它

forever start app.js

https://codingweb.io/run-nodejs-application-background/

尝试pm2以使应用程序永远运行。

NPM安装-g pm2

然后使用

启动server.js

要列出和停止应用程序,使用命令

pm2列表

Pm2停止0

不需要安装其他包。

执行此命令

node server.js > stdout.txt 2> stderr.txt &

server.js是你的服务器文件,也可以是api.js

之后,点击“退出”关闭终端

exit

nohup工作,我检查了AWS ubto vm遵循正确的语法

ubuntu@ip-172-00-00-00:~/ms$ nohup node server.js &

然后按enter键,您将看到这一行

ubuntu@ip-172-00-00-00:~/ms$ nohup: ignoring input and appending output to ‘nohup.out’

然后输入这个

rm nohup.out