Node.js 环境搭建与 PM2 进程管理 Print

  • 21

Node.js 是构建高性能网络应用的 JavaScript 运行时环境。

安装 Node.js(使用 NVM)

# 安装 NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
source ~/.bashrc

# 安装 Node.js LTS
nvm install 18
nvm use 18
nvm alias default 18

# 验证
node --version
npm --version

直接安装(不使用 NVM)

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs

npm/yarn 包管理

# npm
npm init
npm install express
npm install -g pm2
npm run start

# yarn
npm install -g yarn
yarn add express
yarn dev

PM2 进程管理

# 安装
npm install -g pm2

# 启动应用
pm2 start server.js
pm2 start server.js --name myapp
pm2 start npm --name web -- run start

# 管理
pm2 list
pm2 logs
pm2 monit
pm2 restart myapp
pm2 stop myapp
pm2 delete myapp

# 开机自启
pm2 startup
pm2 save

集群模式

pm2 start server.js -i max   # 使用所有 CPU 核心

Was this answer helpful?

« Back