Ubuntu 下部署 SpringBoot

Ubuntu 下部署 SpringBoot

上一篇:Ubuntu 下搭建 MySql 环境

第一步: 下载 Idea

  • 我这次是一切从 0 开始的,所以 Idea 也是电脑上也是没有,安装过程就不水文了,不会的去网上搜一下,教程一大把。
  • PoJie 教程也可以去网上搜,我 Idea 下载的是目前官方最新版(2021.1.3 Ultimate x64),PoJie 教程亲测今日可用(2021.7.12)
  • 多说一句,2021 版的 Idea 第一次用,默认的关键字颜色变了,挺好看的~

第二步: 新建 SpringBoot 项目

  • 这一步也不水文了,网上教程太多了。

  • 这里把 pom.xml 文件、包结构、新增的控制器代码贴一下

    • pom.xml

      <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <parent>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-parent</artifactId>
              <version>2.5.2</version>
              <relativePath/> <!-- lookup parent from repository -->
          </parent>
          <groupId>com.example</groupId>
          <artifactId>demo</artifactId>
          <version>0.0.1-SNAPSHOT</version>
          <name>demo</name>
          <description>Demo project for Spring Boot</description>
          <properties>
              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
              <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
              <java.version>1.8</java.version>
          </properties>
          <dependencies>
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-web</artifactId>
              </dependency>
      
              <dependency>
                  <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-test</artifactId>
                  <scope>test</scope>
              </dependency>
          </dependencies>
      
          <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
          </build>
      </project>
      

      在这里插入图片描述

第三步: 打包 .jar 文件

在这里插入图片描述

第四步: 在 Ubuntu 上部署 .jar 包

  • mkdir demo 命令创建一个名为 demo 的命令
  • cd demo 命令进入该目录
  • nohup java -jar demo-0.0.1-SNAPSHOT.jar 命令启动 jar 包。
    • nohup xxx & : 是 no hang up 的缩写,意为不挂起,用于在系统后台不断运行命令,退出终端不会影响程序的运行

    • 运行完之后会生成一个 nohup.out 文件,里面是启动过程的一些日志

    • 打开 nohup.out 文件,如果正常启动的话,会看到类似这样的输出

      Started DemoApplication in xxx seconds
      
    • 但是,我就没那么幸运了,我遇到的是这样的

          .   ____          _            __ _ _
          /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
          ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
          \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
          '  |____| .__|_| |_|_| |_\__, | / / / /
          =========|_|==============|___/=/_/_/_/
          :: Spring Boot ::                (v2.5.2)
      
          2021-07-14 22:42:22.087  INFO 39193 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_291 on iZwz92d94t8mb03s9z327hZ with PID 39193 (/opt/xiaodudu/demo/demo-0.0.1-SNAPSHOT.jar started by root in /opt/xiaodudu/demo)
          2021-07-14 22:42:22.090  INFO 39193 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
          2021-07-14 22:42:23.276  INFO 39193 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
          2021-07-14 22:42:23.291  INFO 39193 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
          2021-07-14 22:42:23.291  INFO 39193 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.48]
          2021-07-14 22:42:23.345  INFO 39193 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
          2021-07-14 22:42:23.345  INFO 39193 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1141 ms
          2021-07-14 22:42:23.673  WARN 39193 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Failed to start bean 'webServerStartStop'; nested exception is org.springframework.boot.web.server.PortInUseException: Port 8080 is already in use
          2021-07-14 22:42:23.677  INFO 39193 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
          2021-07-14 22:42:23.691  INFO 39193 --- [           main] ConditionEvaluationReportLoggingListener : 
      
          Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
          2021-07-14 22:42:23.713 ERROR 39193 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 
      
          ***************************
          APPLICATION FAILED TO START
          ***************************
      
          Description:
      
          Web server failed to start. Port 8080 was already in use.
      
          Action:
      
          Identify and stop the process that's listening on port 8080 or configure this application to listen on another port.        
      
      • Port 8080 was already in use. 端口被占用,是比较常见的一个异常。

      • 执行 netstat -anp | grep 8080 查看占用端口的程序 pid。(netstat 有很多参数,可以使用 netstat -h 查询)

        root@iZwz92d94t8mb03s9z327hZ:~# netstat -anp | grep 8080
        tcp6       0      0 :::8080                 :::*                    LISTEN      19032/java
        
      • 执行 kill -9 xxxxx 命令终止该程序

        root@iZwz92d94t8mb03s9z327hZ:~# kill -9 19032
        
      • 重新执行 nohup xxx & 命令部署 jar 程序。(nohup.out 是否删除可以自己决定,删除就是重新生成,不删除就是追加内容)

        root@iZwz92d94t8mb03s9z327hZ:/opt/xiaodudu/demo# nohup java -jar demo-0.0.1-SNAPSHOT.jar &
        [1] 39342
        root@iZwz92d94t8mb03s9z327hZ:/opt/xiaodudu/demo# nohup: ignoring input and appending output to 'nohup.out'
        
      • 查看 nohup.out 文件

             .   ____          _            __ _ _
            /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
            ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
            \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
            '  |____| .__|_| |_|_| |_\__, | / / / /
            =========|_|==============|___/=/_/_/_/
            :: Spring Boot ::                (v2.5.2)
        
            2021-07-14 23:05:16.089  INFO 39342 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT using Java 1.8.0_291 on iZwz92d94t8mb03s9z327hZ with PID 39342 (/opt/xiaodudu/demo/demo-0.0.1-SNAPSHOT.jar started by root in /opt/xiaodudu/demo)
            2021-07-14 23:05:16.093  INFO 39342 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
            2021-07-14 23:05:17.228  INFO 39342 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
            2021-07-14 23:05:17.239  INFO 39342 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
            2021-07-14 23:05:17.240  INFO 39342 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.48]
            2021-07-14 23:05:17.300  INFO 39342 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
            2021-07-14 23:05:17.300  INFO 39342 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1095 ms
            2021-07-14 23:05:17.755  INFO 39342 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
            2021-07-14 23:05:17.764  INFO 39342 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 2.338 seconds (JVM running for 2.769)
        

远程访问接口

  • 访问 Ubuntu 服务器IP: 8080/hello/springboot

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Ubuntu部署Spring Boot项目的步骤如下: 1. 首先,确保你已经安装了Java Development Kit (JDK)。你可以使用以下命令来检查是否已经安装了JDK: ``` java -version ``` 如果没有安装,请使用以下命令安装OpenJDK: ``` sudo apt update sudo apt install openjdk-8-jdk ``` 2. 然后,将你的Spring Boot项目打包成可执行的jar文件。在项目的根目录下,使用以下命令: ``` mvn package -DskipTests ``` 这将在项目的`target`目录下生成一个可执行的jar文件。 3. 将打包好的jar文件上传到Ubuntu服务器上。你可以使用scp命令将文件从本地复制到服务器上,例如: ``` scp /path/to/your/project/target/your-project.jar username@server-ip:/path/to/destination ``` 其中,`/path/to/your/project/target/your-project.jar`是你的jar文件的路径,`username`是你的服务器用户名,`server-ip`是你的服务器IP地址,`/path/to/destination`是你希望将文件复制到的目标路径。 4. 登录到Ubuntu服务器上,使用以下命令运行你的Spring Boot项目: ``` java -jar /path/to/your-project.jar ``` 其中,`/path/to/your-project.jar`是你的jar文件的路径。 请注意,这只是一个简单的部署流程示例,具体的步骤可能因项目的不同而有所变化。你可能还需要配置服务器的防火墙规则、安装数据库等其他操作,以适应你的项目需求。 #### 引用[.reference_title] - *1* [在Ubuntu部署Springboot项目](https://blog.csdn.net/qq_25447799/article/details/122481813)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Ubuntu部署SpringBoot项目](https://blog.csdn.net/qq_33187368/article/details/129921562)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HolaSecurity

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值