如何在 Ubuntu 服务器上安装 Spring 启动应用程序?

How to install Spring boot app on Ubuntu server?

我在 Digital Ocean 上有 Ubuntu 服务器,我编写了 Spring 网络应用程序,现在我想将其投入生产。

我通过 FTP 将其上传到服务器,然后通过 Putty 打开我的控制台并使用此命令:

java -jar name.jar

Spring 在那之后启动,当我打开我的网络应用程序时一切正常,但是当我关闭我的 Putty 会话时我的 Spring 网络应用程序不再工作。似乎当我关闭我的 Putty 会话时,Spring 网络应用程序也关闭了。

如何解决?

只需使用 java -jar name.jar & 即可在新进程线程中启动应用程序。

通过直接在末尾添加 > log.txt,您也会有一个日志。

虽然 KLHauser 的建议可行,但如果虚拟机在云中重新启动(这会发生),您的应用程序将不会自动重新启动。此外,使用 kill -9 停止您的应用程序很容易出错且很危险,因为您不小心可能会终止错误的进程。

请参阅 运行 作为 Spring Boot 文档的 Linux 服务部分,了解如何执行此操作。

If you’ve configured Spring Boot’s Maven or Gradle plugin to generate a fully executable jar, and you’re not using a custom embeddedLaunchScript, then your application can be used as an init.d service. Simply symlink the jar to init.d to support the standard start, stop, restart and status commands.

The script supports the following features:

  • Starts the services as the user that owns the jar file
  • Tracks application’s PID using /var/run//.pid
  • Writes console logs to /var/log/.log

Assuming that you have a Spring Boot application installed in /var/myapp, to install a Spring Boot application as an init.d service simply create a symlink:

$ sudo ln -s /var/myapp/myapp.jar /etc/init.d/myapp Once installed, you can start and stop the service in the usual way. For example, on a Debian based system:

$ service myapp start