Ubuntu 下部署 SpringBoot
第一步: 下载 Idea
- 我这次是一切从 0 开始的,所以 Idea 也是电脑上也是没有,安装过程就不水文了,不会的去网上搜一下,教程一大把。
- PoJie 教程也可以去网上搜,我 Idea 下载的是目前官方最新版(2021.1.3 Ultimate x64),PoJie 教程亲测今日可用(2021.7.12)
- 多说一句,2021 版的 Idea 第一次用,默认的关键字颜色变了,挺好看的~
第二步: 新建 SpringBoot 项目
第三步: 打包 .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:~
tcp6 0 0 :::8080 :::* LISTEN 19032/java
-
执行 kill -9 xxxxx 命令终止该程序 root@iZwz92d94t8mb03s9z327hZ:~
-
重新执行 nohup xxx & 命令部署 jar 程序。(nohup.out 是否删除可以自己决定,删除就是重新生成,不删除就是追加内容) root@iZwz92d94t8mb03s9z327hZ:/opt/xiaodudu/demo
[1] 39342
root@iZwz92d94t8mb03s9z327hZ:/opt/xiaodudu/demo
-
查看 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
|