linux下安裝node環境以及配置軟連線、pm2管理node程式

microcosm1994發表於2019-02-16
安裝

在官方網站下載linux系統的安裝包,然後上傳到伺服器進行解壓安裝

  • 執行解壓命令:tar -xvf node-v6.10.0-linux-x64.tar.xz
  • 重新命名: mv node-v6.10.0-linux-x64 nodejs
  • 確認一下nodejs下bin目錄是否有node 和npm檔案,如果有執行軟連線,如果沒有重新下載執行上邊步驟;

通過原始碼編譯,在官方網站下載Source code檔案,

  • tar xvf node-v0.10.28.tar.gz
  • cd node-v0.10.28
  • ./configure
  • make
  • make install
  • cp /usr/local/bin/node /usr/sbin/ 路徑自己選擇
  • 檢視當前安裝的Node的版本 : node -v
建立軟連線,配置全域性環境變數
  • ln -s /usr/local/nodejs/bin/npm /usr/local/bin/
  • ln -s /usr/local/nodejs/bin/node /usr/local/bin/

檢查是否配置好

  • node -v
  • npm -v
使用pm2進行node程式管理
  • 安裝:npm i pm2 -g
  • 執行:pm2 start app.js
  • 停止所有程式:pm2 stop all
  • 重啟所有程式: pm2 restart all
pm2命令
# General 
    $ npm install pm2 -g            # Install PM2 
    $ pm2 start app.js              # Start, Daemonize and auto-restart application (Node) 
    $ pm2 start app.py              # Start, Daemonize and auto-restart application (Python) 
    $ pm2 start npm -- start        # Start, Daemonize and auto-restart Node application 
 
# Cluster Mode (Node.js only) 
    $ pm2 start app.js -i 4         # Start 4 instances of application in cluster mode 
                                    # it will load balance network queries to each app 
    $ pm2 reload all                # Zero Second Downtime Reload 
    $ pm2 scale [app-name] 10       # Scale Cluster app to 10 process 

# Process Monitoring 
    $ pm2 list                      # List all processes started with PM2 
    $ pm2 list --sort=<field>       # Sort all processes started with PM2 
    $ pm2 monit                     # Display memory and cpu usage of each app 
    $ pm2 show [app-name]           # Show all information about application 
 
# Log management 
    $ pm2 logs                      # Display logs of all apps 
    $ pm2 logs [app-name]           # Display logs for a specific app 
    $ pm2 logs --json               # Logs in JSON format 
    $ pm2 flush
    $ pm2 reloadLogs
 
# Process State Management 
    $ pm2 start app.js --name="api" # Start application and name it "api" 
    $ pm2 start app.js -- -a 34     # Start app and pass option "-a 34" as argument 
    $ pm2 start app.js --watch      # Restart application on file change 
    $ pm2 start script.sh           # Start bash script 
    $ pm2 start app.json            # Start all applications declared in app.json 
    $ pm2 reset [app-name]          # Reset all counters 
    $ pm2 stop all                  # Stop all apps 
    $ pm2 stop 0                    # Stop process with id 0 
    $ pm2 restart all               # Restart all apps 
    $ pm2 gracefulReload all        # Gracefully reload all apps in cluster mode 
    $ pm2 delete all                # Kill and delete all apps 
    $ pm2 delete 0                  # Delete app with id 0 
 
# Startup/Boot management 
    $ pm2 startup                   # Detect init system, generate and configure pm2 boot on startup 
    $ pm2 save                      # Save current process list 
    $ pm2 resurrect                 # Restore previously saved processes 
    $ pm2 unstartup                 # Disable and remove startup system 
     
    $ pm2 update                    # Save processes, kill PM2 and restore processes 
    $ pm2 generate                  # Generate a sample json configuration file 
 
# Deployment 
    $ pm2 deploy app.json prod setup    # Setup "prod" remote server 
    $ pm2 deploy app.json prod          # Update "prod" remote server 
    $ pm2 deploy app.json prod revert 2 # Revert "prod" remote server by 2 
     
# Module system 
    $ pm2 module:generate [name]    # Generate sample module with name [name] 
    $ pm2 install pm2-logrotate     # Install module (here a log rotation system) 
    $ pm2 uninstall pm2-logrotate   # Uninstall module 
    $ pm2 publish                   # Increment version, git push and npm publish 

相關文章