项目打包

在项目目录下执行打包目录进行打包

yarn build
// 或者
npm run build

打包完成后会生成一个dist文件夹,这样就打包完成了(我这样做了SEO的,所有目录结构有点不一样,但是整体都是差不多的)

nginx安装

下载 nginx

解压安装

将下载的nginx上传到服务器,运行以下命令执行解压安装

[root@VM-4-13-centos website]# tar -zxvf nginx-1.22.0.tar.gz				# 解压nginx
[root@VM-4-13-centos website]# cd nginx-1.22.0/ # 进入目录
[root@VM-4-13-centos nginx-1.22.0]# ./configure && make && make install # 安装

这样nginx就安装完成了,将刚打包的文件上传到/usr/local/nginx/目录中

进入安装目录,执行启动命令就可以启动nginx

[root@VM-4-13-centos nginx-1.22.0]# cd /usr/local/nginx/					# 进入安装目录
[root@VM-4-13-centos nginx]# ./sbin/nginx -c ./conf/nginx.conf # 启动nginx

这样就可以在浏览器输入IP:端口进行访问了,但是有的需要手动开启端口或关闭防火墙(两个只要执行一个就可以了)

./sbin/nginx 启动

./sbin/nginx -s stop 关闭

./sbin/nginx -s reload 重启

1.开启端口

[root@VM-4-13-centos ~]# firewall-cmd --permanent --add-port=80/tcp	   # 放开端口
[root@VM-4-13-centos ~]# firewall-cmd --permanent --add-port=443/tcp
[root@VM-4-13-centos ~]# firewall-cmd --reload # 重载防火墙

2.关闭防火墙

[root@VM-4-13-centos ~]# systemctl status firewalld.service    # 查看防火墙状态 ( 绿的running表示防火墙开启 )
[root@VM-4-13-centos ~]# systemctl stop firewalld.service # 执行关闭命令
[root@VM-4-13-centos ~]# systemctl status firewalld.service # 再次执行查看防火墙命令
[root@VM-4-13-centos ~]# systemctl disable firewalld.service # 执行开机禁用防火墙自启命令

nginx配置

我们需要配置端口号、请求的代理转发等,这里我主要就加了一个代理

[root@VM-4-13-centos conf]# cd /usr/local/nginx/conf	# 进入nginx的conf目录
[root@VM-4-13-centos conf]# vim nginx.conf # 编辑配置文件 server {
listen 80; # 端口号
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
}
# 拦截的上下文路径
location /website/ {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'POST,GET,PUT,PATCH,DELETE,OPTIONS';
add_header Access-Control-Allow-Headers 'Content-Type, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name,Token,Cookie,authorization';
# 转发到后端的地址
proxy_pass http://127.0.0.1:8081/website/;
}
}

修改完成后重启nginx服务,在页面上就可以看到请求能够正常的访问了;

当我们刷新页面的时候,或者访问路由配置页面的时候会直接404,这是因为通常我们做的vue项目属于单页面开发。所以只有index.html。解决这个bug也很简单。只需要将访问重定向到index.html这个页面。交由 index.html 去处理对应的路由跳转就好.

		location / {
alias html/;
index index.html index.htm;
try_files $uri $uri/ @router; # 需要指向下面的@router否则会出现vue的路由在nginx中刷新出现404
} location /website/ {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'POST,GET,PUT,PATCH,DELETE,OPTIONS';
add_header Access-Control-Allow-Headers 'Content-Type, X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Date, X-Api-Version, X-File-Name,Token,Cookie,authorization';
proxy_pass http://127.0.0.1:8081/website/;
}
# 对应上面的@router,主要原因是路由的路径资源并不是一个真实的路径,所以无法找到具体的文件
# 因此需要rewrite到index.html中,然后交给路由在处理请求资源
location @router {
rewrite ^.*$ /index.html last;
}

修改完成后重启nginx服务,这样就没有问题了(配置下面的内容记得先把 nginx 停掉)

注册为服务

每次开机都需要去手动启动就会很麻烦,如果将nginx注册为服务,设置为开机自启的状态,每次开机服务就会自动启动

创建nginx.service文件

[root@VM-4-13-centos ~]# vim /lib/systemd/system/nginx.service

加入下面的内容

[Unit]
#描述服务
Description=nginx
#描述服务类别
After=network.target #服务运行参数的设置,注意【Service】的启动、重启、停止命令都要用绝对路径
[Service]
#后台运行的形式
Type=forking
#服务具体运行的命令
ExecStart=/usr/local/nginx/sbin/nginx
#重启命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
#停止命令
ExecStop=/usr/local/nginx/sbin/nginx -s quit
#表示给服务分配独立的临时空间
PrivateTmp=true #运行级别下服务安装的相关设置,可设置为多用户,即系统运行级别为3
[Install]
WantedBy=multi-user.target

完成后保存退出

设置开机启动

[root@VM-4-13-centos ~]# systemctl enable nginx.service

其它命令

systemctl start nginx.service    #启动nginx服务
systemctl stop nginx.service #停止nginx服务
systemctl restart nginx.service #重新启动服务
systemctl enable nginx.service #设置开机自启动
systemctl disable nginx.service #停止开机自启动
systemctl status nginx.service #查看服务当前状态
systemctl list-units --type=service #查看所有已启动的服务

Vue打包部署到CentOS 7的更多相关文章

  1. Vue 打包部署上线

    1,VUE逻辑编写完成后在当前项目下打包 npm run build 需要注意的是,当打包完毕后,需要将入口的index.html的项目dist路径改成相对路径 另外需要注意的一点是,一旦打包vue. ...

  2. vue 打包部署到服务器上 配置nginx访问

    坑一 css,js资源引入不正确 webpack配置文件config/index.js 需要更改: 方法一 当部署到带有文件夹的项目中,这种绝对路径就会出现问题,因为把配置的static文件夹当成了根 ...

  3. vue打包部署(含2.0)

    到这里vue的所有平时使用的知识点都写完了 先补充一下vue2.x的安装 ## 全局脚手架 npm install vue/cli -g ## 查看版本 vue --version ## 新建项目 v ...

  4. -webkit-line-clamp、-webkit-box-orient vue 打包部署后不起作用??

    场景分析:实际开发中,文字描述过长,需要两行或三行显示缩略显示: 实现过程: 实现过程遇到的问题:打包到线上后发现并没有-webkit-box-orient属性,导致省略号并没有按预期展示: 解决方法 ...

  5. Vue 打包部署到服务器后,非主页刷新后出现404问题解决

    开心把项目部署到服务上,从头到尾点了一遍,发现没有问题,以为就可以大功告成,没想到刷新页面时出现 被破泼了一脸的凉水,更奇怪的事首页没有问题,只有其他页面出现,在调戏了很久的度娘后,才从坑里跳出来,以 ...

  6. vue项目 打包部署上线

    1. npm run dev:本地开发的时候做调试用的. 2. npm run build:打包部署上线,生成一个 dist 文件夹. 注意:用 npm run build 时,常遇到因引用路径不对导 ...

  7. Vue+elementUI build打包部署后字体图标丢失问题

    错误描述:Vue+elementUI  build打包部署后字体图标丢失,控制台显示文件element-icons.woff和element-icons.ttf文件404 错误展现: 控制台报错截图 ...

  8. vue-multi-module【多模块集成的vue项目,多项目共用一份配置,可以互相依赖,也可以独立打包部署】

    基于 vue-cli 2 实现,vue 多模块.vue多项目集成工程 Github项目地址 : https://github.com/BothEyes1993/vue-multi-module 目标: ...

  9. vue项目打包部署到nginx 服务器上

    假如要实现的效果如下 http://ip/vue    =>是进入首页访问的路径是  usr/local/nginx/html/vue http://ip/website     =>是进 ...

  10. 阿里云安装Nginx+vue项目部署

    阿里云安装Nginx+vue项目部署 nginx安装包下载 http://nginx.org/en/download.html nginx安装 首先先安装PCRE pcre-devel 和Zlib,因 ...

随机推荐

  1. android端http请求重发问题定位过程

    昨天生产系统上报出一个问题:用户做一次扫码交易,出现了两条交易记录.幸好支付渠道对支付码有限制只成功了一笔,没有出现多扣钱的问题.现在我们要排查一下,为什么做一次操作会出现两条交易记录.我们的后台服务 ...

  2. vue3使用表格el-table-infinite-scroll.js:18 Uncaught (in promise) Error: [el-table-infinite-scroll]: .el-scrollbar__wrap element not found.

    先看下表格里面有没有这个el-scrollbar__wrap class类 没有的话升级一下element-plus到最新的就行 你可以先查看element-plus的版本 npm view elem ...

  3. 后端给前端rtmp和flv直播 播放方法

    const suffixal = this.videoObj.videoServer .split('?')[0] .split('.') .pop() var router = this.$rout ...

  4. 通过 Helm Chart 部署 Easysearch

    Easysearch 可以通过 Helm 快速部署了,快来看看吧! Easysearch 的 Chart 仓库地址在这里 https://helm.infinilabs.com. 使用 Helm 部署 ...

  5. nginx resolver 指定多个DNS (2个DNS)

    nginx resolver 指定多个DNS (2个DNS) 直接在 resolver 后边填2个DNS,中间用空格 location / { resolver 223.5.5.5 114.114.1 ...

  6. Apollo启动配置排查,超时时间的配置

    Apollo启动配置排查 1.排查下来是 本地的服务 apollo 配置fake发布到线上去了.2.或者是引用的apollo jar包中指向的apollo服务器地址是否正确. 3.超时时间的配置 ## ...

  7. Java编码规范-字符串与Integer的比较,BigDecimal非空参数

    Java编码规范-字符串与Integer的比较,BigDecimal非空参数 package com.example.core.mydemo; import java.math.BigDecimal; ...

  8. MongoDB文档存储

    非关系型数据库存储 NoSQL,全称 Not Only SQL,意为不仅仅是 SQL,泛指非关系型数据库.NoSQL 是基于键值对的,而且不需要经过 SQL 层的解析,数据之间没有耦合性,性能非常高. ...

  9. Sealos 5.0 正式发布,云本应该是操作系统

    把所有资源抽象成一个整体,一切皆应用,这才是云应该有的样子. 2018 年 8 月 15 日 Sealos 提交了第一行代码. 随后开源社区以每年翻倍的速度高速增长. 2022 年我们正式创业,经历一 ...

  10. ansible(1)---师傅领进门

    背景 在企业里,运维需要配合开发进行产品上架,说白了就是把写好的代码上服务器.那么,就会出现这样的问题:需要运维人员配置好系统,配置好环境,配置好网络,配置好程序,配置好所有所有的依赖环境.     ...