简介:本文详细解析了基于Vue.js与SpringBoot的前后端分离项目,如何通过宝塔面板部署至云服务器的完整流程,涵盖环境配置、项目构建、服务器设置、安全优化等关键步骤,助力开发者高效完成线上部署。
选择云服务器时,需根据项目规模确定配置:
宝塔面板简化服务器管理,安装步骤如下:
# CentOS 7/8yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh# Ubuntu 20.04wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh
在宝塔面板中安装必要软件:
本地构建:
# 进入前端项目目录cd /path/to/vue-project# 安装依赖npm install# 构建生产环境包npm run build
生成dist目录,包含静态资源(HTML、CSS、JS)。
上传至服务器:
dist目录上传至服务器指定路径(如/www/wwwroot/frontend)。添加站点:
example.com),根目录选择前端项目路径(如/www/wwwroot/frontend)。配置反向代理:
修改Nginx配置文件(通常位于/www/server/nginx/conf/nginx.conf或站点配置文件),添加反向代理规则:
location /api/ {proxy_pass http://127.0.0.1:8080/; # 后端SpringBoot地址proxy_set_header Host $host;proxy_set_header X-Real-IP $remote_addr;}
重启Nginx生效。
Maven构建:
# 进入后端项目目录cd /path/to/springboot-project# 清理并打包mvn clean package
生成target/your-project.jar文件。
上传JAR包:
/www/wwwroot/backend)。使用Screen运行(避免SSH断开后进程终止):
# 安装screen(如未安装)yum install -y screen # CentOSapt install -y screen # Ubuntu# 创建并进入screen会话screen -S springboot# 运行JAR包java -jar /www/wwwroot/backend/your-project.jar --server.port=8080# 按Ctrl+A+D退出screen会话(进程继续运行)
宝塔计划任务(可选):
创建数据库:
修改后端配置:
application.yml或application.properties,配置数据库连接:
spring:datasource:url: jdbc//localhost:3306/your_db?useSSL=false&serverTimezone=UTC
username: your_usernamepassword: your_password
if ($scheme != "https") {return 301 https://$host$request_uri;}
gzip on;)。-Xms512m -Xmx1024m)。netstat -tulnp | grep 8080。kill -9 [PID],或修改后端端口。
@Configurationpublic class CorsConfig implements WebMvcConfigurer {@Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS").allowedHeaders("*");}}
通过宝塔面板部署前后端分离项目,可显著降低运维复杂度。关键步骤包括:
建议开发者在部署后进行全面测试,包括功能测试、性能测试和安全测试,确保项目稳定运行。