【超详细】前后端项目部署(原始Nginx+SpringBoot)

[复制链接]
发表于 2026-10-3 22:07:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

把一套前后端项目放到线上,最省事的组合是 Nginx + SpringBoot:Nginx 负责静态资源与反向代理,SpringBoot 打成 jar 直接跑在 JVM 里。本文按前端、后端两条线完整走一遍,从源码编译 Nginx 到 jar 后台常驻,全部命令可直接抄。

一、前端部署

用 Xshell 连上服务器

源码编译安装 Nginx

去 Nginx 官网选一个稳定版本,复制下载链接。先建个目录放源码:

[root@VM-24-5-centos ~]# mkdir services
[root@VM-24-5-centos ~]# cd services/
[root@VM-24-5-centos services]# curl -o nginx-1.26.1.tar.gz https://nginx.org/download/nginx-1.26.1.tar.gz
[root@VM-24-5-centos services]# tar -zxvf nginx-1.26.1.tar.gz

进入目录后先装好相关依赖,再设置编译参数:

[root@VM-24-5-centos nginx-1.26.1]# yum install pcre pcre-devel -y
[root@VM-24-5-centos nginx-1.26.1]# yum install openssl openssl-devel -y
[root@VM-24-5-centos nginx-1.26.1]# ./configure --with-http_ssl_module --with-http_v2_module --with-stream

SSL、HTTP/2、stream 这三个模块按需开启,一般都要带上。接着编译安装:

[root@VM-24-5-centos nginx-1.26.1]# make
[root@VM-24-5-centos nginx-1.26.1]# make install

加环境变量

[root@VM-24-5-centos nginx-1.26.1]# vim /etc/profile
# shift+g 跳到最后一行加入
export PATH=$PATH:/usr/local/nginx/sbin
[root@VM-24-5-centos nginx-1.26.1]# source /etc/profile

查看运行状态

[root@VM-24-5-centos nginx-1.26.1]# nginx
[root@VM-24-5-centos nginx-1.26.1]# netstat -ntlp

改配置文件

cd /usr/local/nginx
cd conf
cp nginx.conf nginx.default.conf   # 先备份一份
cat nginx.conf

配置里 listen 决定访问端口(默认 80),root 指向根目录对应的 html 目录,index 是默认主页名。

上传前端产物并解压

把前端打包好的文件压缩后传到服务器,解压到指定目录:

[root@VM-24-5-centos services]# unzip dist.zip -d UserSphere-front

配置站点并重载

编辑 /usr/local/nginx/conf/nginx.conf,把 root 改成刚解压出来的前端目录,用户与启动用户保持一致,然后:

[root@VM-24-5-centos conf]# nginx -s reload

浏览器访问公网地址,前端页面就能出来了。

二、后端部署

装 Java

[root@VM-24-5-centos local]# yum install -y java-1.8.0-openjdk*
# yum 安装不需要再单独配环境变量
[root@VM-24-5-centos services]# java -version

装 Maven

[root@VM-24-5-centos services]# curl -o apache-maven-3.9.8-bin.tar.gz https://dlcdn.apache.org/maven/maven-3/3.9.8/binaries/apache-maven-3.9.8-bin.tar.gz
[root@VM-24-5-centos services]# tar -zxvf apache-maven-3.9.8-bin.tar.gz

然后 vim /etc/profile 在末尾追加 Maven 的 bin 路径,source /etc/profile 生效,mvn -v 能打印版本即成功。

拉代码

[root@VM-24-5-centos services]# yum install -y git
[root@VM-24-5-centos services]# git clone https://github.com/xxxxxxx/UserSphere-backend.git

Maven 打包

[root@VM-24-5-centos services]# mvn package -DskipTests

跳过测试打包会快很多;也可以本地编译好了直接拖上去。编译产物就是那个 jar 包。

运行 jar 包

# 给所有用户增加执行权限
[root@VM-24-5-centos UserSphere-backend]# chmod a+x UserSphere-backend-0.0.1-SNAPSHOT.jar
# 以生产模式前台运行
[root@VM-24-5-centos UserSphere-backend]# java -jar UserSphere-backend-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
# 后台形式运行
[root@VM-24-5-centos UserSphere-backend]# nohup java -jar UserSphere-backend-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod &
# 查看进程
[jobs]
netstat -ntlp

用 nohup 加 & 把服务挂到后台,终端断开也不影响;再用 jobs 或 netstat -ntlp 确认端口已监听。

小结

整套部署的骨架就是:编译装 Nginx → 前端产物放进 root 目录 → 配反向代理 → 服务器上装 JDK 与 Maven → git 拉码 → mvn 打包 → nohup 跑 jar。Nginx 与后端各占一层,后面再想加 HTTPS、加负载均衡,只需改 Nginx 配置重新 reload,不用动后端。

qidao123.com企服评测-应用市场 编辑提示

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表