search

Home  >  Q&A  >  body text

node.js - 额,第一天接触linux,终端退出后nodejs服务器也停止

今天申请了一块阿里云,在上面安装了node,用终端链接了服务器,写了一段代码开启了一个http服务器,但是我电脑上的终端退出,那个服务器也访问不了了,这个咋整,唉,学linux第一天好难受

伊谢尔伦伊谢尔伦2863 days ago585

reply all(6)I'll reply

  • 迷茫

    迷茫2017-04-17 13:03:04

    The node program can consider installing forever on the server, and then start it with forever start your_js_file.js. After that, you can use forever list to view the running programs, and use forever stop to stop the process.

    The more common method is to use the nohup command to start the process. The disadvantage is that the program will not automatically restart when it stops.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 13:03:04

    pm2, or forever, are both open source on github, search them

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 13:03:04

    1. supervisord

    2. screen

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 13:03:04

    I happened to see something about this when I was reading Ghost’s documentation a few days ago. You can refer to it:

    让 Ghost 一直运行
    
    前面提到的启动 Ghost 使用 npm start 命令。这是一个在开发模式下启动和测试的不错的选择,但是通过这种命令行启动的方式有个缺点,即当你关闭终端窗口或者从 SSH 断开连接时,Ghost 就停止了。为了防止 Ghost 停止工作,有两种方式解决这个问题。
    
    Forever (https://npmjs.org/package/forever)
    你可以使用 forever 以后台任务运行 Ghost 。forever 将会按照 Ghost 的配置,当进程 crash 后重启 Ghost。
    
    通过 npm install forever -g 安装 forever
    为了让 forever 从 Ghost 安装目录运行,输入 NODE_ENV=production forever start index.js
    通过 forever stop index.js 停止 Ghost
    通过 forever list 检查 Ghost 当前是否正在运行
    Supervisor (http://supervisord.org/)
    流行的 Linux 发行版——例如 Fedora, Debian 和 Ubuntu,都包含一个 Supervisor 包:一个进程控制系统,允许在启动的时候无需初始化脚本就能运行 Ghost。不像初始化脚本一样,Supervisor 可以移植到不同的发行版和版本。
    
    根据不同的 Linux 发行版 安装 Supervisor 。如下所示:
    Debian/Ubuntu: apt-get install supervisor
    Fedora: yum install supervisor
    其他大多数发行版: easy_install supervisor
    通过 service supervisor start 确保 Supervisor 运行
    为 Ghost 创建一个启动脚本。通常为 /etc/supervisor/conf.d/ghost.conf ,例如:
    
    [program:ghost]
    command = node /path/to/ghost/index.js
    directory = /path/to/ghost
    user = ghost
    autostart = true
    autorestart = true
    stdout_logfile = /var/log/supervisor/ghost.log
    stderr_logfile = /var/log/supervisor/ghost_err.log
    environment = NODE_ENV="production"
    使用 Supervisor 启动 Ghost:supervisorctl start ghost
    
    停止 Ghost: supervisorctl stop ghost
    
    详细内容请参阅 Supervisor 文档。

    Excerpted from
    http://docs.ghostchina.com/zh/installation/deploy/

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:03:04

    It is recommended to use pm2, it is very easy to use, nodejs process manager

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:03:04

    Thank you everyone above, I just encountered this problem too.

    Use forever to manage node.js processes

    Off topic, nginxIt’s really useful.

    reply
    0
  • Cancelreply