Deploy Your SpringBoot App as a Service in Under-a-Minute!
If you want to do a quick deployment of your spring-boot application as a service on Linux, here’s how you can quickly do it through a fully executable jar.
Step 1
Create your service file under `/lib/systemd/system/` as `/lib/systemd/system/myapp.service`
nano myapp.service
Add this content:
[Unit]
Description=myapp port 8086
After=syslog.target[Service]
User=myusername
ExecStart=java -jar /home/ubuntu/myapp/target/myapp-1.0.jar --server.port=8086
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5[Install]
WantedBy=multi-user.target
Specifying ‘- - server.port=8086’ in above file is optional, you can specify this at runtime as well.
Step 2
Reload daemon and enable the service:
systemctl reload daemon
systemctl enable myapp
Check status by testing the app or by checking the service status:
systemctl status myapp
Prerequisites & Tips:
1. Use ‘sudo’ in front of above commands if you’re not already running as root or are tired of typing password every time.
2. This solution requires you jar to be fully executable. To create a ‘fully executable’ jar with Maven, use the following plugin configuration:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
The following example shows the equivalent Gradle configuration:
tasks.named('bootJar') {
launchScript()
}
3. If your application fails to start, check the log file written to /var/log/<appname>.log for errors.