Ubuntu下部署SpringBoot
Ubuntu下部署SpringBoot
第一步: 安装java环境
1 | # 更新软件源 |
第二步: 安装下载Idea并新建SpringBoot项目
到官网下载(https://www.jetbrains.com/idea/download/?section=windows)
这里把
pom.xml文件、包结构、新增的控制器代码贴一下pom.xml
1 |
|
第三步: 打包 .jar 文件
对于Maven 可以使用使令:mvn clean package
对于Gradle 可以使用使令:./gradlew clean bootJar
第四步: 在 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
第五步: 在 Ubuntu 上使用进程管理
直接用 java -jar 启动的应用在终端关闭后会终止,且意外崩溃后无法自动重启。
可使用进程管理工具(systemd)确保应用后台运行并自动恢复,通过系统服务管理 JAR 应用,支持开机自启、状态监控、日志管理。
1.创建服务文件(如 /etc/systemd/system/myapp.service):
1 | [Unit] |
2.启用并启动服务:
1 | sudo systemctl daemon-reload # 刷新配置 |
3.常用命令:
1 | sudo systemctl status myapp # 查看状态 |
4.如果出错,按照以下排查:
- 检查目录和文件是否存在
1 | # 检查工作目录是否存在 |
- 创建缺失的目录和权限
1 | # 创建应用目录 |
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 易锦风的博客!
评论







/baaa5c67e3754abe83e0bbb2f2e14c58.png)




