zoukankan      html  css  js  c++  java
  • ubuntu 下安装nodejs以及pm2

     ubuntu 12.04服务器可以使用apt-get方式安装Node JS,但是,安装完后的版本为v0.6.12的版本,如果我们想要使用新一点的版本需要做如下配置:

    1
    2
    3
    4
    apt-get install python-software-properties
    apt-add-repository ppa:chris-lea/node.js
    apt-get update
    apt-get install nodejs

    安装完成后可以检查一下版本,使用如下命令可以查看:

    1
    2
    root@test:#node -v
    v0.10.28

     接下来我们需要安装pm2这个软件

     首先我们需要安装一个npm的软件,具体命令如下:

    1
    2
    apt-get install npm
    npm -v

    安装完成,我们就可以安装pm2这个软件,使用如下命令安装:

    1
    root@test:# npm install -g pm2

    在shell中输入pm,使用table补全,如果能出现pm2,说明已经正常安装。

    现在我们可以使用PM2命令来启动node

    输入如下命令,启动我们的app.js

    1
    pm2 start app.js

    那我们如何让pm2在开机的时候自动运行我们的程序呢?或者说我们想要启动,停止或者重启的时候用什么命令呢?我们可以做如下操作:

    1
    sudo env PATH=$PATH:/usr/local/bin pm2 startup app.js -u safeuser   #(你启动node的用户)
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    addam@AYZ:/web/webtest$ sudo env PATH=$PATH:/usr/local/bin pm2 startup app.js
    PM2 Generating system init script in /etc/init.d/pm2-init.sh
    PM2 Making script booting at startup...
    PM2 -ubuntu- Using the command chmod +x /etc/init.d/pm2-init.sh; update-rc.d pm2-init.sh defaults
     Adding system startup for /etc/init.d/pm2-init.sh ...
       /etc/rc0.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
       /etc/rc1.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
       /etc/rc6.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
       /etc/rc2.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
       /etc/rc3.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
       /etc/rc4.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
       /etc/rc5.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
    PM2 Done.

    这样我们就可以使用/etc/init.d/pm2-init.sh start|stop|restart 命令来启动关闭和重启我们的程序了,而且开机的时候也会自动启动,如果你使用kill来杀死这个进程,当你再次使用原来的pm2 start app.js
    来启动的时候,会发现报错,无法找到套接字等问题。所以,使用这样平滑的方式启动停止,对我们的程序有很大的好处。

     PM2的主要功能:

    Main features

    • Built-in load balancer (using the native cluster module)

    • Script daemonization

    • 0s downtime reload for Node apps

    • Generate SystemV/SystemD startup scripts (Ubuntu, Centos...)

    • Pause unstable process (avoid infinite loop)

    • Restart on file change with --watch

    • Monitoring in console

    PM2地址:https://github.com/Unitech/pm2

    参考文献:https://www.digitalocean.com/community/articles/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps

  • 相关阅读:
    在python3中如何加载静态文件详版步骤
    django 过滤器总结
    通过虚拟环境创建并开始一个django
    关于django中模板的理解
    python 初学 正则表达式入门
    python 初学 错误类型以及编码规范
    获取地址的经纬度,根据经纬度反查地址
    mybatisz中一个可以替代between..and 的技巧
    linux指令和文件系统
    auto.js入门笔记
  • 原文地址:https://www.cnblogs.com/lcword/p/8099394.html
Copyright © 2011-2022 走看看