跳到主要内容基于宝塔面板与 Nginx 部署智能协同云图库项目实战 | 极客日志JavaSaaS大前端java
基于宝塔面板与 Nginx 部署智能协同云图库项目实战
智能协同云图库基于腾讯云服务器、宝塔 Linux 面板及 Nginx 部署方案详解。内容包含服务器初始化、环境依赖配置、前后端打包部署及 Nginx 反向代理设置。针对跨域问题、WebSocket 转发及单页面应用路由刷新 404 等常见部署难点提供解决方案,附带生产环境配置文件示例与排查思路,帮助快速完成企业级项目上线。
云服务器初始化
安装宝塔 Linux 面板
登录服务器后,执行宝塔面板安装命令。安装过程中会提示设置面板的默认账号和密码,建议直接记录或修改为自定义安全凭证。

防火墙端口开放
在服务器控制台的安全组规则中,放行宝塔面板默认端口 8888,以便通过外网访问管理界面。

点击添加规则,开放宝塔 Linux 端口 8888。

登录与管理
使用浏览器访问 http://{服务器 IP}:8888,输入刚才设置的账号密码登录。首次登录需粘贴复制的命令并执行以验证身份。



打开外网 IPv4 面板地址,复制账号秘密并登录。

激活 LNMP 环境
激活后选择 LNMP 依赖套餐,务必勾选 PHP 版本。如果安装过程中遇到超时或失败,可在服务器重置系统中选择重装宝塔面板。
安装过程中可点击设置面板,修改默认账号、密码以提升安全性。
部署规划
源码与架构
本项目前端采用 Vue 技术栈,后端基于 Spring Boot。为提高效率,前后端均通过宝塔面板进行管理。
- 前端:遵循 Vue 项目部署模式,基于 Nginx 运行。
- 后端:利用宝塔的 Java 项目管理器运行 JAR 包。
为什么要用 Nginx 转发?
前后端域名一致,保证不会出现跨域问题。
地址规划
- 前端:通过 Nginx 进行转发,访问地址为
http://{域名}。
- 后端:通过 Nginx 进行转发,访问地址为
http://{域名}/api。实际运行在 8123 端口。
- JDK 建议选择 17 版本(caffeine 要求 JDK > 11)。
- Nginx:服务器 80 端口,默认已安装。
- 数据库:服务器 3306 端口,默认已安装。
- Redis:服务器 6379 端口,需要手动安装。
注意事项
做好规划后,需要在腾讯云控制台的防火墙中开通需要外网访问的服务端口,比如 MySQL 和 Redis。
安装依赖
1. 数据库配置
宝塔面板已自动安装 MySQL 数据库,直接使用即可。先为后端项目添加一个数据库,名称需与项目配置保持一致,注意用户名、密码和访问权限。
在 IDE 中打开后端项目,通过数据库面板在本地检查连接是否正常。


create database if not exists yu_picture;
use yu_picture;
create table if not exists user (
id bigint auto_increment comment 'id' primary key,
userAccount varchar(256) not null comment '账号',
userPassword varchar(512) not null comment '密码',
userName varchar(256) null comment '用户昵称',
userAvatar varchar(1024) null comment '用户头像',
userProfile varchar(512) null comment '用户简介',
userRole varchar(256) default 'user' not null comment '用户角色:user/admin',
editTime datetime default current_timestamp not null comment '编辑时间',
createTime datetime default current_timestamp not null comment '创建时间',
updateTime datetime default current_timestamp not null on update current_timestamp comment '更新时间',
isDelete tinyint default 0 not null comment '是否删除',
unique key uk_userAccount (userAccount),
index idx_userName (userName)
) comment '用户' collate = utf8mb4_unicode_ci;
create table if not exists picture (
id bigint auto_increment comment 'id' primary key,
url varchar(512) not null comment '图片 url',
name varchar(128) not null comment '图片名称',
introduction varchar(512) null comment '简介',
category varchar(64) null comment '分类',
tags varchar(512) null comment '标签(JSON 数组)',
picSize bigint null comment '图片体积',
picWidth int null comment '图片宽度',
picHeight int null comment '图片高度',
picScale double null comment '图片宽高比例',
picFormat varchar(32) null comment '图片格式',
userId bigint not null comment '创建用户 id',
createTime datetime default current_timestamp not null comment '创建时间',
editTime datetime default current_timestamp not null comment '编辑时间',
updateTime datetime default current_timestamp not null on update current_timestamp comment '更新时间',
isDelete tinyint default 0 not null comment '是否删除',
index idx_name (name),
index idx_introduction (introduction),
index idx_category (category),
index idx_tags (tags),
index idx_userId (userId)
) comment '图片' collate = utf8mb4_unicode_ci;
alter table picture add column reviewStatus INT default 0 not null comment '审核状态:0-待审核;1-通过;2-拒绝';
alter table picture add column reviewMessage VARCHAR(512) null comment '审核信息';
alter table picture add column reviewerId BIGINT null comment '审核人 ID';
alter table picture add column reviewTime DATETIME null comment '审核时间';
alter table picture add column thumbnailUrl varchar(512) null comment '缩略图 url';
create table if not exists space (
id bigint auto_increment comment 'id' primary key,
spaceName varchar(128) null comment '空间名称',
spaceLevel int default 0 null comment '空间级别:0-普通版 1-专业版 2-旗舰版',
maxSize bigint default 0 null comment '空间图片的最大总大小',
maxCount bigint default 0 null comment '空间图片的最大数量',
totalSize bigint default 0 null comment '当前空间下图片的总大小',
totalCount bigint default 0 null comment '当前空间下的图片数量',
userId bigint not null comment '创建用户 id',
createTime datetime default current_timestamp not null comment '创建时间',
editTime datetime default current_timestamp not null comment '编辑时间',
updateTime datetime default current_timestamp not null on update current_timestamp comment '更新时间',
isDelete tinyint default 0 not null comment '是否删除',
index idx_userId (userId),
index idx_spaceName (spaceName),
index idx_spaceLevel (spaceLevel)
) comment '空间' collate = utf8mb4_unicode_ci;
alter table picture add column spaceId bigint null comment '空间 id(为空表示公共空间)';
create index idx_spaceId on picture (spaceId);
alter table picture add column picColor varchar(16) null comment '图片主色调';
ALTER TABLE space ADD COLUMN spaceType int default 0 not null comment '空间类型:0-私有 1-团队';
CREATE INDEX idx_spaceType ON space (spaceType);
create table if not exists space_user(
id bigint auto_increment comment 'id' primary key,
spaceId bigint not null comment '空间 id',
userId bigint not null comment '用户 id',
spaceRole varchar(128) default 'viewer' null comment '空间角色:viewer/editor/admin',
createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间',
updateTime datetime default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP comment '更新时间',
UNIQUE KEY uk_spaceId_userId (spaceId, userId),
INDEX idx_spaceId (spaceId),
INDEX idx_userId (userId)
) comment '空间用户关联' collate = utf8mb4_unicode_ci;
2. Redis 配置
在宝塔面板的软件商店中搜索并安装 Redis,版本选择默认的即可。安装完成后,需要配置 Redis,开启远程访问并配置密码,否则本地无法连接。
最后,在 IDEA 数据库面板中验证本地能否连接远程 Redis。
如果 Redis 状态异常,可以在宝塔面板重启 Redis。
3. Java 环境
要部署 Java 项目,必须安装 JDK。在宝塔面板中,可以通过软件商店快速安装指定版本的 JDK。此处建议安装 JDK 17。
建议多安装几个版本,比如 JDK 8、11、17,需要用哪个版本的时候可以随时切换。
4. 其他服务
如需使用对象存储(如腾讯云 COS)或 AI 服务,请前往对应官网开通。注意,要给对象存储增加该服务器 IP(或者实际访问前端域名)的跨域配置,否则编辑图片时将无法正确加载图片。
后端部署
1. 修改配置
修改 application-prod 生产环境配置,包括数据库、Redis、对象存储、AI 服务的 Key 等,替换为上述安装依赖时指定的配置。
为了性能,还要关闭 MyBatis Plus 的日志;为了安全,要给 Knife4j 接口文档设置用户名和密码。
server:
port: 8123
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://云服务器 IP:3306/yu_picture
username: yu_picture_root
password: yu_picture_123456
redis:
database: 0
host: 101.34.40.20
port: 6379
timeout: 5000
password: yu_picture_123456
mybatis-plus:
configuration:
log-impl: ''
knife4j:
basic:
enable: true
username: yu_picture_root
password: yu_picture_123456
cos:
client:
host: xxx
secretId: xxx
secretKey: xxx
region: xxx
bucket: xxx
aliYunAi:
apiKey: xxx
由于配置文件存在敏感信息,使用 Git 提交代码时,应忽略该配置文件的提交。
2. 打包部署
首先更改 pom.xml 文件的打包配置,删除掉主类配置的 skip 配置,才能正常打包。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.yupi.yupicturebackend.YuPictureBackendApplication</mainClass>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
在 IDEA 中打开后端项目,忽略测试并打包。双击 package 打包,打包成功后得到 jar 包文件。
找到 Jar 包对应的路径,复制路径,方便在面板上传 Jar 包。
上传 jar 包到服务器,此处为了方便,就放到 web 根目录。
在项目执行命令中,必须指定生产环境的配置!还可以根据需要调整内存。
/www/server/java/jdk-17.0.8/bin/java -jar -Xmx1024M -Xms256M /www/wwwroot/yu-picture-backend-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
点击确认后,忽略后面数据库密码的校验,可能是没识别到而已。
此时无法通过浏览器访问接口文档 http://云服务器 IP:8123/api/doc.html。
这是因为我们的服务器防火墙没有放开 8123 端口。这里我们故意不放开,因为在之前的部署规划中,后端需要通过 Nginx 进行转发,从而解决跨域问题。
如果我们部署的 Jar 包部署后总是莫名其妙中断,就一定要看看项目对应的日志,比如以下问题,需要重装 Redis 并且重新过一遍设置 Redis 的 IP、密码的流程。
3. Nginx 转发
新建一个 Nginx 站点,域名填写当前服务器 IP 或者自己的域名,根目录随意填写即可(只要不包含中文)。
如果访问的是后端接口(地址有 /api 前缀),则 Nginx 将请求转发到后端服务,对应配置代码如下:
location /api {
proxy_pass http://127.0.0.1:8123;
proxy_set_header Host $proxy_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_set_header Connection "";
}
但是,对于本项目,光有 HTTP 转发配置还不够!后端还需要提供 WebSocket 连接,所以也要对 WebSocket 进行转发,再给 Nginx 补充下列配置:
# 代理 WebSocket 连接 (专门用于 WebSocket 请求)
location /api/ws {
proxy_pass http://127.0.0.1:8123;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
proxy_read_timeout 86400s;
}
修改完后,就可以通过 80 端口(可以省略)访问到接口了。
一定要注释掉下列配置!否则访问接口文档时,静态资源的加载可能会出错。因为浏览器会从本地缓存加载资源,而不是动态请求资源。
前端部署
前端部署可以参考 Vite 官方文档。分为修改配置、打包部署和 Nginx 转发这 3 个步骤。
1. 修改配置
线上的前端需要请求线上的后端接口,所以需要修改 request.ts 文件中的请求地址为线上:
const DEV_BASE_URL = "http://localhost:8123";
const PROD_BASE_URL = "http://云服务器 IP";
const myAxios = axios.create({
baseURL: PROD_BASE_URL,
timeout: 10000,
withCredentials: true,
});
此外,由于本项目用到了 WebSocket,还要同步修改 pictureEditWebSocket.ts 文件中的 WebSocket 的连接地址:
const DEV_BASE_URL = "ws://localhost:8123";
const PROD_BASE_URL = "ws://云服务器 IP";
const url = `${PROD_BASE_URL}/api/ws/picture/edit?pictureId=${this.pictureId}`;
2. 打包部署
参考 Vite 官网,在 package.json 文件中定义 pure-build 命令:
{
"scripts": {
"dev": "vite",
"pure-build": "vite build",
"build": "run-p type-check \"build-only {@}\" --"
}
}
为什么明明已经有 build 命令了,我们还要自己定义 pure-build 命令呢?
因为脚手架内置的 build 命令会执行类型检查,如果项目代码中有任何类型不规范的地方,都会导致打包失败!虽然可以自己一个个修复类型,但是太影响效率了,得不偿失,所以引入一个更干净的构建命令。
2)执行 pure-build 命令,执行打包构建。注意,如果 Node.js 版本较低,会构建失败,这时可以到官网安装更新的版本,比如 v20.17.0 等长期支持版本。
构建成功后,可以得到用于部署的静态文件 dist 目录。
把 dist 目录下的所有文件上传到服务器上(可以新建一个 yu-picture-frontend 目录)。
3. Nginx 转发
一般来说,用户无法直接访问服务器上的文件,需要使用 Nginx 提供静态文件的访问能力。
然后访问服务器地址(或者自己配置的域名),就能打开前端网站了。
但是经过验证,目前访问除了主页外的其他页面(比如 /add_picture),如果刷新页面,就会出现 404 错误。
这个问题是由于 Vue 是单页面应用(前端路由),打包后的文件只有 index.html,服务器上不存在对应的页面文件(比如 /add_picture.html),所以需要在 Nginx 配置转发。如果找不到某个页面文件,就加载主页 index.html 文件。
location / {
try_files $uri $uri/index.html /index.html;
}
相关免费在线工具
- Keycode 信息
查找任何按下的键的javascript键代码、代码、位置和修饰符。 在线工具,Keycode 信息在线工具,online
- Escape 与 Native 编解码
JavaScript 字符串转义/反转义;Java 风格 \uXXXX(Native2Ascii)编码与解码。 在线工具,Escape 与 Native 编解码在线工具,online
- JavaScript / HTML 格式化
使用 Prettier 在浏览器内格式化 JavaScript 或 HTML 片段。 在线工具,JavaScript / HTML 格式化在线工具,online
- JavaScript 压缩与混淆
Terser 压缩、变量名混淆,或 javascript-obfuscator 高强度混淆(体积会增大)。 在线工具,JavaScript 压缩与混淆在线工具,online
- Base64 字符串编码/解码
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
- Base64 文件转换器
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online