前后端分离项目

前端配置:

public里面新建一个static文件夹,创建一个config.js文件,作用是配置ip地址来代替localhost,在这个全局文件中配置ip地址,在页面直接调用,以后ip地址有变,直接修改此处配置,就不需要一个一个去改了。

config.js

window.server={
    filesUploadUrl:"localhost"//需改为ip
}

index.html(页面调用配置好的js)

<head>
<script src="static/Sconfig.js"></script>
</head>

终端:

npm run build

生成dist文件,上传到云服务器中,假设存在/home/user/server,再修改配置如下:

[root@VM-12-2-centos server]# vim dist/static/config.js

window.server={
    filesUploadUrl:"ip"//需改为ip
}

后端配置:

方法一:更改配置文件:

application.yml,主要是更改一些本地localhost的东西变成ip的。

server:
  port: 9090
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://ip:3306/demo?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: 你猜

点击idea的右上角的maven,项目名,Lifecycle,依次点击,clean,complie,package。打包完成,把jar(在项目目录target下)包上传云服务器,假设是存在/home/user/server/springboot,在云服务器后台启动。

[root@VM-12-2-centos springboot]# nohup java -jar springvuedemo-0.0.1-SNAPSHOT.jar &

//更改默认配置文件启动,pord是application-pord.xml
[root@VM-12-2-centos springboot]# nohup java -jar springvuedemo-0.0.1-SNAPSHOT.jar --spring.profiles.active=pord &

//查看日志
[root@VM-12-2-centos springboot]# tailf nohup.out

启动成功。

关闭进程:

//关闭进程
ps -aux | grep java
kill -9 pid

nginx配置:

[root@VM-12-2-centos springboot]# cd /usr/local/nginx
[root@VM-12-2-centos nginx]# vim conf/nginx.conf

//更改配置
location / {
#配置前端所在目录
root   /home/user/server/dist;
index  index.html index.htm;
#防止刷新出现404
try_files $uri $uri/ /index.html;
}

#反向代理配置,api,因为前端用/api代替了下面的地址,localhost可以填ip
location /api/ {
proxy_pass http://localhost:9090/;
}

//重启nginx
[root@VM-12-2-centos nginx]# ./sbin/nginx -s reload

创建数据库,建表。

posted @ 2022-09-13 23:21  chandol  阅读(105)  评论(0编辑  收藏  举报