目录

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 环境配置

  1. 本文 JDK 使用 jdk 21 版本

  2. MySQL使用MySQL8.0.40 版本

  3. 如果你要部署 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 环境配置

  1. 本文 JDK 使用 jdk 21 版本

  2. MySQL使用MySQL8.0.40 版本

  3. 如果你要部署 web 应用,可以使用 tomcat,部署Spring Boot 请忽略此第3点

2.2 Spring Boot 项目搭建

  1. 创建 Spring Boot 项目

    https://i-blog.csdnimg.cn/direct/8b5cc0bcbc5d461089051d7e82dd9171.png

  2. 选择一些构建工具

    https://i-blog.csdnimg.cn/direct/574b6435acb0437b918439e9c5a640b2.png

  3. 创建以下目录结构和文件

    https://i-blog.csdnimg.cn/direct/973e433f3c2e455e9afb62271d34eb31.png

  4. 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;
    }
  5. 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> {
    }
  6. 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();
        }
    }
  7. 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
  8. 配置文件说明

    https://i-blog.csdnimg.cn/direct/3b575ec739b74eefac2e862a44daa358.png

2.3 mysql 配置

  1. 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. 图文讲解

    https://i-blog.csdnimg.cn/direct/4fd330d35c134d9f84465754ad5576eb.png

2.4 测试项目

在地址栏输入以下内容,即可通过项目访问数据库内容。

localhost:8080/user/index

https://i-blog.csdnimg.cn/direct/84c81130c5c442b2a391f2856b9b9332.png

2.5 将Spring Boot 项目部署到 Linux 中

我们在本地已经测试通过了,现在让我们进入虚拟机(或者远程服务器)将我们的项目部署到 Linux 系统中。

  1. 在 Linux 系统中需要配置 jdk 21 环境,和mysql 8.0 环境,如果你已经都配置好了,请或略此段,如果你还未配置,请点击查看详细配置:
  1. 将刚刚我们构建的项目打成 jar 包:

    https://i-blog.csdnimg.cn/direct/1362842730bb4dbd87fcd39acf12271e.png

  • 将项目拖拽到远程服务器或者虚拟机中

    https://i-blog.csdnimg.cn/direct/31dac902f18b4f958c4bf2d61f5baf3c.png

  • 在 Linux 中运行项目

    https://i-blog.csdnimg.cn/direct/fff09066384b456096aec0ba8acff8be.png

  • 部署成功 https://i-blog.csdnimg.cn/direct/4cb4a7d217e24a33ac03d37cdcca793a.png

三、Web 项目部署

如果你需要在 Linux 中部署 Web 项目,你还需要在配置一个 tomcat 容器, 运行时,只需要将项目打包,打成war 包,将 war 包丢到 tomcat 的 webapps 目录下即可。

注意:项目在本地运行正常,在linux 系统中无法运行,可能是你环境配置的问题, 如 开启防火墙开放端口 等。

  • https://i-blog.csdnimg.cn/direct/8a98e3b47d1e417589bf54c1ea50b2e8.png

    https://i-blog.csdnimg.cn/direct/237be9f1544749fd9c1d4e70ac2f4348.png


END Spring Boot 项目,Web项目,部署完成