视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Vue-CLI 3.X 部署项目至生产服务器的方法
2020-11-27 21:59:50 责编:小采
文档


本教程主要讲解的是 Vue-CLI 3.x 脚手架搭建的vue项目, 先构建生成dist文件(纯静态应用), 然后自动化部署到静态文件服务器 Nginx。

一、Nginx服务器文件的配置

server {
 listen 80;
 server_name www.xxxxxx.com;#生产环境
 location / {
 root /usr/local/www/xxx_program/;
 index index.html;
 try_files $uri $uri/ /index.html;
 }
 }
 server {
 listen 80;
 server_name test.xxxxxx.com; #测试环境
 location / {
 root /usr/local/www/xxx_program_test/;
 index index.html;
 try_files $uri $uri/ /index.html;
 }
 }

二、配置生产/测试环境 服务器SSH远程登陆账号

在项目根目录下, 创建 .env 文件 (当前环境变量)

VUE_APP_SERVER_ID变量指代 当前需部署的服务器ID为0

VUE_APP_SERVER_ID=0

在项目根目录下, 创建 deploy/products.js 文件

该文件功能如下:

(1) 读取env环境变量

const fs = require('fs')
const path = require('path')
// env环境变量的路径
const envPath = path.resolve(__dirname, '../.env')
// env对象
const envObj = parse(fs.readFileSync(envPath, 'utf8'))
const SERVER_ID = parseInt(envObj['VUE_APP_SERVER_ID'])

function parse (src) {
 // 解析KEY=VAL的文件
 const res = {}
 src.split('\n').forEach(line => {
 // matching "KEY' and 'VAL' in 'KEY=VAL'
 const keyValueArr = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/)
 // matched?
 if (keyValueArr != null) {
 const key = keyValueArr[1]
 let value = keyValueArr[2] || ''

 // expand newlines in quoted values
 const len = value ? value.length : 0
 if (len > 0 && value.charAt(0) === '"' && value.charAt(len - 1) === '"') {
 value = value.replace(/\\n/gm, '\n')
 }

 // remove any surrounding quotes and extra spaces
 value = value.replace(/(^['"]|['"]$)/g, '').trim()

 res[key] = value
 }
 })
 return res
}

(2) 定义多个服务器账号 及 根据 SERVER_ID 导出当前环境服务器账号

const SERVER_LIST = [
 {
 id: 0,
 name: 'A-生产环境',
 domain: 'www.xxx.com',
 host: 'XX.XX.XX.XX',
 port: 22,
 username: 'root',
 password: 'xxxxxxx',
 path: '/usr/local/www/xxx_program/'
 },
 {
 id: 1,
 name: 'B-测试环境',
 domain: 'test.xxx.com',
 host: 'XX.XX.XX.XX',
 port: 22,
 username: 'root',
 password: 'xxxxxxx',
 path: '/usr/local/www/xxx_program_test/'
 }, 
]

module.exports = SERVER_LIST[SERVER_ID]

三、创建自动化部署脚本 (使用scp2库)

在项目根目录下, 创建 deploy/index.js 文件

const scpClient = require('scp2')
const ora = require('ora')
const chalk = require('chalk')
const server = require('./products')
const spinner = ora('正在发布到生产服务器...')
spinner.start()
scpClient.scp('dist/', {
 host: server.host,
 port: server.port,
 username: server.username,
 password: server.password,
 path: server.path
}, function(err) {
 spinner.stop()
 if (err) {
 console.log(chalk.red(' 发布失败.\n'))
 throw err
 } else {
 console.log(chalk.green(' Success! 成功发布到生产服务器! \n'))
 }
})

四、添加 package.json 中的 scripts 命令, 自定义名称为 “deploy”

"scripts": {
 "serve": "vue-cli-service serve",
 "build": "vue-cli-service build",
 "lint": "vue-cli-service lint",
 "deploy": "npm run build && node ./deploy"
 }

五、执行部署任务

在项目根目录下 执行 npm run deploy 命令, 或 使用 vue ui控制面板执行deploy任务, 即可自动打包并部署至线上服务器

备注: 要切换部署的服务器, 只需修改 .env文件中的服务器ID, 然后再执行deploy任务即可.

下载本文
显示全文
专题