Linux-部署-Spring-Boot-项目,-Web项目2025版
Linux 部署 Spring Boot 项目, Web项目(2025版)
主要介绍:在 Linux 系统中配置运行Java 程序所需要的配置环境,如JDK,MySQL,Tomcat 等,
之后,将你的 Spring Boot 项目使用 Maven 工具打包成 jar包,丢到 Linux 系统中,
java -jar demo.jar
运行即可。本文项目版本使用:Windows 11,jdk-21,IntelliJ IDEA 2024.3.4.1,mysql-8.0, Navicat Premium 17
本文使用的项目源码,见文章最上方。
一、简洁版
1.1 Linux 环境配置
本文 JDK 使用 jdk 21 版本
MySQL使用MySQL8.0.40 版本
如果你要部署 web 应用,可以使用 tomcat,部署Spring Boot 请忽略此第3点
1.2 将Spring Boot 项目部署到 Linux 中
在 IntelliJ IDEA 中,使用 Maven 工具,将 Spring Boot 项目打成 jar 包,在 Linux 系统中,运行以下指令,启动项目即可。其中:demo.jar 是你打包好的 jar 包。
注意:项目在本地运行正常,在linux 系统中无法运行,可能是你环境配置的问题, 如
开启防火墙
,开放端口
等。
java -jar demo.jar
二、详细版
2.1 Linux 环境配置
本文 JDK 使用 jdk 21 版本
MySQL使用MySQL8.0.40 版本
如果你要部署 web 应用,可以使用 tomcat,部署Spring Boot 请忽略此第3点
2.2 Spring Boot 项目搭建
创建 Spring Boot 项目
选择一些构建工具
创建以下目录结构和文件
User.java
package com.example.demo.entity; import jakarta.persistence.Entity; import jakarta.persistence.Id; import lombok.Data; import java.util.Date; @Data @Entity public class User { @Id private Integer id; private String name; private Date birthday; }
UserRepository.java
package com.example.demo.repository; import com.example.demo.entity.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository<User, Integer> { }
UserController.java
package com.example.demo.controller; import com.example.demo.entity.User; import com.example.demo.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/user") public class UserController { @Autowired private UserRepository userRepository; @GetMapping("/index") public List<User> findAll() { return userRepository.findAll(); } }
application.yml
server: port: 8080 spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver jpa: show-sql: true properties: hibernate: format_sql: true
配置文件说明
2.3 mysql 配置
tset.sql
create database test character set utf8 collate utf8_general_ci; use test; create table user( id int primary key auto_increment, name varchar(20), birthday datetime ); insert into user(name, birthday) values ('小明', '2000-01-01'); insert into user(name, birthday) values ('小华', '2001-01-01');
图文讲解
2.4 测试项目
在地址栏输入以下内容,即可通过项目访问数据库内容。
localhost:8080/user/index
2.5 将Spring Boot 项目部署到 Linux 中
我们在本地已经测试通过了,现在让我们进入虚拟机(或者远程服务器)将我们的项目部署到 Linux 系统中。
- 在 Linux 系统中需要配置 jdk 21 环境,和mysql 8.0 环境,如果你已经都配置好了,请或略此段,如果你还未配置,请点击查看详细配置:
将刚刚我们构建的项目打成 jar 包:
将项目拖拽到远程服务器或者虚拟机中
在 Linux 中运行项目
部署成功
三、Web 项目部署
如果你需要在 Linux 中部署 Web 项目,你还需要在配置一个 tomcat 容器, 运行时,只需要将项目打包,打成war 包,将 war 包丢到 tomcat 的
webapps
目录下即可。注意:项目在本地运行正常,在linux 系统中无法运行,可能是你环境配置的问题, 如
开启防火墙
,开放端口
等。
END Spring Boot 项目,Web项目,部署完成