Vue-CLI 3.X 部署項目至生產(chǎn)服務器的方法
本教程主要講解的是 Vue-CLI 3.x 腳手架搭建的vue項目, 先構建生成dist文件(純靜態(tài)應用), 然后自動化部署到靜態(tài)文件服務器 Nginx。
一、Nginx服務器文件的配置
server {
listen 80;
server_name www.xxxxxx.com;#生產(chǎn)環(huán)境
location / {
root /usr/local/www/xxx_program/;
index index.html;
try_files $uri $uri/ /index.html;
}
}
server {
listen 80;
server_name test.xxxxxx.com; #測試環(huán)境
location / {
root /usr/local/www/xxx_program_test/;
index index.html;
try_files $uri $uri/ /index.html;
}
}
二、配置生產(chǎn)/測試環(huán)境 服務器SSH遠程登陸賬號
在項目根目錄下, 創(chuàng)建 .env 文件 (當前環(huán)境變量)
VUE_APP_SERVER_ID變量指代 當前需部署的服務器ID為0
VUE_APP_SERVER_ID=0
在項目根目錄下, 創(chuàng)建 deploy/products.js 文件
該文件功能如下:
(1) 讀取env環(huán)境變量
const fs = require('fs')
const path = require('path')
// env環(huán)境變量的路徑
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) 定義多個服務器賬號 及 根據(jù) SERVER_ID 導出當前環(huán)境服務器賬號
const SERVER_LIST = [
{
id: 0,
name: 'A-生產(chǎn)環(huán)境',
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-測試環(huán)境',
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]
三、創(chuàng)建自動化部署腳本 (使用scp2庫)
在項目根目錄下, 創(chuàng)建 deploy/index.js 文件
const scpClient = require('scp2')
const ora = require('ora')
const chalk = require('chalk')
const server = require('./products')
const spinner = ora('正在發(fā)布到生產(chǎn)服務器...')
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(' 發(fā)布失敗.\n'))
throw err
} else {
console.log(chalk.green(' Success! 成功發(fā)布到生產(chǎn)服務器! \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"
}
五、執(zhí)行部署任務
在項目根目錄下 執(zhí)行 npm run deploy 命令, 或 使用 vue ui控制面板執(zhí)行deploy任務, 即可自動打包并部署至線上服務器
備注: 要切換部署的服務器, 只需修改 .env文件中的服務器ID, 然后再執(zhí)行deploy任務即可.
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
vue使用jsencrypt實現(xiàn)rsa前端加密的操作代碼
這篇文章主要介紹了vue使用jsencrypt實現(xiàn)rsa前端加密,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
Vue自定義組件實現(xiàn)?v-model?的幾種方式
在?Vue?中,v-model?是一個常用的指令,用于實現(xiàn)表單元素和組件之間的雙向綁定,當我們使用原生的表單元素時,直接使用?v-model?是很方便的,本文給大家介紹了Vue自定義組件實現(xiàn)?v-model?的幾種方式,需要的朋友可以參考下2024-02-02
vue實現(xiàn)離線地圖+leaflet+高德瓦片的詳細代碼
這篇文章主要給大家介紹了關于vue實現(xiàn)離線地圖+leaflet+高德瓦片的詳細代碼,Vue Leaflet是一種結合了Vue框架和Leaflet庫的前端技術,用于展示和操作天地圖,需要的朋友可以參考下2023-10-10

