安装Nodejs
-
下载安装包,可选择其他版本 node-v10.16.0-linux-x64.tar.xz
-
将下载文件上传至linux服务器并解压
tar -xvf node-v10.16.0-linux-x64.tar.xz
-
移动
cd /usr/local/ mv 解压文件路径/node-v10.16.0.0-linux-64/ nodejs
-
npm与node命令生效
ln -s /usr/local/nodejs/bin/npm /usr/local/bin/ ln -s /usr/local/nodejs/bin/node /usr/local/bin/
-
查看nodejs是否安装成功
node -v npm -v
安装pm2
-
npm install -g pm2
-
指定软连接
ln -s /usr/local/nodejs/bin/pm2 /usr/local/bin/pm2
-
验证
pm2 -v
部署vue
-
npm run build 生成的dist文件夹复制到服务器上
-
在dist同级目录创建app.js文件,内容如下
const fs = require('fs'); const path = require('path'); const express = require('express'); const app = express(); app.use(express.static(path.resolve(__dirname, './dist'))) app.get('*', function(req, res) { const html = fs.readFileSync(path.resolve(__dirname, './dist/index.html'), 'utf-8') res.send(html) }) app.listen(8092);
-
初始化package.json
{ "name": "test_mp", "version": "1.0.0", "description": "", "main": "app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "haiyifeng", "license": "ISC", "dependencies": { "express": "^4.15.3" } }
-
安装依赖
npm install
-
运行
pm2 start app.js
-
运行状态 pm2 ls