应用部署

在本地开发时,我们使用 npm run dev 来启动服务,但是在部署应用的时候不可以这样使用。因为 npm run dev 会针对本地开发做很多处理,而生产运行需要一个更加简单稳定的方式。所以本章主要讲解如何部署你的应用。

一般从源码代码到真正运行,我们会拆分成构建和部署两步,可以做到一次构建多次部署

构建

$ npm install
$ npm run build

目前执行完 npm run build 后,会在当前目录下生成dist目录, 此目录下既有server bundle,也有client bundle

后续优化:

  • 使用gulp工具分开,client bundle 部署到CDN
  • 打包完成后, server side 只安装 dependencies 的依赖。 devDependencies 中的模块过大而且在生产环境不会使用,安装后也可能遇到未知问题。

部署

  • 操作系统:Linux
  • 运行环境:NodeJS >=8,建议选择 LTS 版本
  • 安装pm2 npm i -g pm2
  • 安装pm2-intercom pm2 install pm2-intercom

启动命令

$    pm2 start pm2.config.js --env production

启动配置项

pm2.config.js中配置指定启动配置。

//pm2.config.js
module.exports = {
    apps: [{
......
        name: 'vue-ssr-admin', // app名称
        script: 'server/index.js', // 要运行的脚本的路径。
        args: '', // 由传递给脚本的参数组成的字符串或字符串数​​组。
        output: './log/out.log',
        error: './log/error.log',
        log: './log/combined.outerr.log',
        merge_logs: true, // 集群的所有实例的日志文件合并
        log_date_format: "YYYY-MM-DD HH:mm Z",
        instances: "max",  // 进程数 1、数字 2、'max'根据cpu内核数
        instance_var: "INSTANCE_ID",
        max_memory_restart: '1G', // 当内存超过1024M时自动重启
        watching: true,
        env: {
            NODE_ENV: 'production',
            PORT: 8066
        },
......
    }],
}

停止命令

$ pm2 delete vue-ssr-admin
Copyright ©大数据项目组 2019 all right reserved,powered by Gitbook本文档更新于: 2019-11-25 02:41

results matching ""

    No results matching ""