centos 部署 vue项目

时间:2024-01-26 20:26:08

安装Nodejs

  1. 下载安装包,可选择其他版本 node-v10.16.0-linux-x64.tar.xz

  2. 将下载文件上传至linux服务器并解压

    tar -xvf node-v10.16.0-linux-x64.tar.xz
  3. 移动

    cd /usr/local/
    mv 解压文件路径/node-v10.16.0.0-linux-64/ nodejs
  4. npm与node命令生效

    ln -s /usr/local/nodejs/bin/npm /usr/local/bin/
     ln -s /usr/local/nodejs/bin/node /usr/local/bin/
  5. 查看nodejs是否安装成功

    node -v
    npm -v

安装pm2

  1. npm install -g pm2

  2. 指定软连接

    ln -s /usr/local/nodejs/bin/pm2 /usr/local/bin/pm2

  3. 验证

    pm2 -v

部署vue

  1. npm run build 生成的dist文件夹复制到服务器上

  2. 在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);
  3. 初始化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"
         }
     }
  4. 安装依赖

    npm install

  5. 运行

    pm2 start app.js

  6. 运行状态  pm2 ls