项目打包

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

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. Ubuntu 上使能 SELinux

    首发公号:Rand_cs 此文档说明如何在 ubuntu 上启用 SELinux,测试环境为虚拟机,开始前一定一定一定先来个快照,不要问我为什么有三个一定. 卸载 apparmor(可选) ubunt ...

  2. .NET5 ASP.NET CORE 发布到IIS 文件无法替换

    由于默认是:进程内托管.要在IIS里停止网站,才能替换文件. 建议解决方案是:进程外(out-of-process)托管 记事本修改项目的  .csproj 文件(或在VS上,选中web项目,右键-编 ...

  3. http请求方式-RestTemplate

    http请求方式-RestTemplate import com.alibaba.fastjson.JSON; import com.example.core.mydemo.http.OrderReq ...

  4. 博客更换新域名为52ecy.cn

    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解` 博客更换新域名为52ecy.cn 日期:2017-10-2 ...

  5. 聊一聊 Monitor.Wait 和 Pluse 的底层玩法

    一:背景 1. 讲故事 在dump分析的过程中经常会看到很多线程卡在Monitor.Wait方法上,曾经也有不少人问我为什么用 !syncblk 看不到 Monitor.Wait 上的锁信息,刚好昨天 ...

  6. 5分钟了解LangChain的路由链

    上上篇文章<5分钟理透LangChain的Chain>里用到了顺序链SequentialChain,它可以将多个链按顺序串起来.本文介绍LangChain里的另外1个重要的链:路由链. 1 ...

  7. windows系统安装或使用inspect.exe工具

    确认是否安装? 结合工具everything,进行搜索 选择对应操作系统的版本,右键->选择打开路径,进到inspect.exe的安装路径,双击打开软件 软件开启后,就会自动开始抓取目前软件界面 ...

  8. 图最短路径之Dijkstra

    Dijkstra's shortest path algorithm 算法参考地址:Dijsktra's algorithm (geeksforgeeks.org) 算法的简介: 1)该算法用来计算最 ...

  9. Linux Redis 服务设置开机自启动

    @ 目录 前言 一.准备工作 二.操作步骤 2.1 修改redis.conf文件 2.2 创建启动脚本 2.3 设置redis 脚本权限 2.4 设置开机启动 2.5 验证 总结 前言 请各大网友尊重 ...

  10. Python脚本报错:DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working import pymssql

    报错信息: monitor_mssql.py:10: DeprecationWarning: Using or importing the ABCs from 'collections' instea ...