VUE生产环境搭建,编译、启动、守护

生产环境搭建

第一步:安装node环境

源码安装 (非常推荐)

1、源码安装
wget https://nodejs.org/download/release/v8.11.1/node-v8.11.1-linux-x64.tar.gz

2、解压代码
tar xzvf node-v* && cd node-v*

3、安装必要的编译软件
sudo yum install gcc gcc-c++

4、编译
./configure
make

5、编译&安装
make install

//查看版本
node -v
npm -v
//npm升级
npm install -g npm

//可以选择淘宝镜像cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org

//cnpm升级
cnpm install -g cnpm

以下所有所述均在项目开发完成,并成功执行build的情况下

进入项目目录

第二步:安装Node express框架

cnpm install express

第三步:项目下创建app.js

const express = require('express')
const history = require('connect-history-api-fallback')
const env = process.env.NODE_ENV || 'development'

const path = require('path')
const app = express()

app.use(history())

if (env !== 'development') {
  app.use(express.static(path.join(__dirname, 'dist')))
}

// 错误处理
app.use((err, req, res, next) => {
  res.status(442).send({ error: err.message })
})

const server = app.listen(8086, () => {
  console.log(`Express started in ${app.get('env')} mode on http://127.0.0.1:8086`)
})

第五步:全局安装pm2

cnpm install -g pm2

第六步:项目实现pm2自动部署、启动

1、创建ecosystem.json,括号中注视

{
  "apps": [
    {
      "name": "项目名称",//pm2启动项目名称
      "script": "app.js",//node启动程序
      "env": {
        "COMMON_VARIABLE": "true"
      },
      "env_production" : {
        "NODE_ENV": "production"
      }
    }
  ],
  "deploy": {
    "production": { //生产环境
      "user": "root",//ssh登录名
      "host": ["10.15.?.???"],//ssh host
      "port":"22",//ssh 端口
      "ref": "origin/stg",//git 分支
      "repo": "git@nc-gitlab.webinsights.cn:webinsights/wi-v5-bi-frontend.git",//git url地址
      "path": "/data/www/wi-v5-bi-frontend",//服务器部署目录
      "ssh_options": "StrictHostKeyChecking=no",//ssh缓存是否开启
      "post-deploy" : "cnpm install && npm run build && pm2 startOrRestart ecosystem.json --env production",//git 部署完成后,启动项目的命令集
      "env"  : {
        "NODE_ENV": "production"
      }
    },
    "dev":{//测试环境
      "user": "root",
      "host": ["10.15.?.???"],
      "ref": "origin/stg",
      "repo": "git@nc-gitlab.webinsights.cn:webinsights/wi-v5-bi-frontend.git",
      "path": "/data/www/wi-v5-bi-frontend",
      "ssh_options": "StrictHostKeyChecking=no",
      "post-deploy" : "cnpm install && npm run build && pm2 startOrRestart ecosystem.json --env production",
      "env"  : {
        "NODE_ENV": "production"
      }
    }
  }
}

第七步:开始自动部署

进入项目目录

==注意:一定要把package.json里devDependencies中的依赖放到dependencies中==

//自动部署、测试环境steup(更新部署update)
pm2 deploy ecosystem.json dev update
//自动部署、正式环境steup(更新部署update)
pm2 deploy ecosystem.json production steup

时间会有点长,稍等片刻

[PM2] Spawning PM2 daemon with pm2_home=/Users/gavinwang/.pm2
[PM2] PM2 Successfully daemonized
--> Deploying to dev environment
--> on host *.*.*.*
  ○ deploying origin/stg
  ○ executing pre-deploy-local
  ○ hook pre-deploy
  ○ fetching updates
  ○ full fetch
root@*.*.*.*'s password:
正在获取 origin
来自 git@******.git
   cfaffbc..c26e619  stg        -> origin/stg
   cfaffbc..c26e619  master     -> origin/master
  ○ resetting HEAD to origin/stg
root@*.*.*.*'s password:
HEAD 现在位于 c26e619 登录页动画调整
root@*.*.*.*'s password:
root@*.*.*.*'s password:
  ○ executing post-deploy `export NODE_ENV=production && cnpm install && npm run build && pm2 startOrRestart ecosystem.json --env production`
root@*.*.*.*'s password:

【build】

All packages installed (used 2s, speed 0B/s, json 0(0B), tarball 0B)

> wi-v5-bi-frontend@3.6.1 build /data/www/wi-v5-bi-frontend/source
> node build/build.js

Hash: 1242552a777e3c869b0f
Version: webpack 3.10.0
Time: 196151ms


Build complete.

  Tip: built files are meant to be served over an HTTP server.
  Opening index.html over file:// won't work.

[PM2] Applying action restartProcessId on app [wi-v5-bi-frontend](ids: 0)
[PM2] [wi-v5-bi-frontend](0) ✓
┌───────────────────┬────┬──────┬───────┬────────┬─────────┬────────┬─────┬───────────┬──────┬──────────┐
│ App name          │ id │ mode │ pid   │ status │ restart │ uptime │ cpu │ mem       │ user │ watching │
├───────────────────┼────┼──────┼───────┼────────┼─────────┼────────┼─────┼───────────┼──────┼──────────┤
│ wi-v5-bi-frontend │ 0  │ fork │ 21737 │ online │ 1       │ 0s     │ 60% │ 9.4 MB    │ root │ disabled │
│ wi-v5-frontend    │ 1  │ fork │ 2248  │ online │ 0       │ 6h     │ 0%  │ 15.4 MB   │ root │ disabled │
└───────────────────┴────┴──────┴───────┴────────┴─────────┴────────┴─────┴───────────┴──────┴──────────┘
 Use `pm2 show <id|name>` to get more details about an app
  ○ hook test
  ○ successfully deployed origin/stg
--> Success

【出现Success字样,发布完成】

第八步:开启守护

进入服务器

cd /data/www/wi-v5-bi-frontend

这个时候你会发现和你平时看到的不太一样

有current、source两个目录,source是源码、current是build后产生目录,真正的生产环境

查看pm2进程

pm2 list
App name id mode pid status restart uptime cpu mem user watching
wi-v5-bi-frontend 0 fork 2242 online 0 8s 0% 27.1 MB root disabled

保存pm2列表

pm2 save

设置服务器重启后自动启动

pm2 startup

Complete!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,099评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,956评论 25 709
  • http://www.jianshu.com/p/fa0d833673be?utm_campaign=hugo&u...
    冰J冰阅读 239评论 0 0
  • 欲望的激情, 即便有虚妄的自信张扬, 一场沉没生命的永远, 瞬间寂静了梦里的不朽芳华。 以为着的自由, 放纵了时光...
    心羽自心阅读 225评论 0 2
  • 空荡的办公室,一个个凌乱的桌面堆着厚厚的卷宗。办公室的灯光异常的明亮,明亮得有些虚幻,只有空调还在呼呼的吐着热气。...
    很饿的杜喆阅读 316评论 0 1