How to deploy a spring boot application to a Linux server and install it as a service

Here's how to deploy a spring boot application and install it as a service:

Preparation

  1. Package your application as a jar file, name it myapp.jar
  2. Install the JDK that suits your needs on the server


Installation

  1. Upload the jar file to your server, put it in /var/myapp directory
  2. Set it as executable by running this command: chmod +x myapp.jar
  3. Create a script named myapp.service and place it in /etc/systemd/system directory, the script contains something like this

    [Unit]
    Description=myapp
    After=syslog.target network.target

    [Service]
    ExecStart=/path/to/java/home/bin/java -jar /var/myapp/myapp.jar
    ExecStop=/bin/kill -15 $MAINPID
    SuccessExitStatus=143

    [Install]
    WantedBy=multi-user.target



  4. Flag the application to start automatically on system boot, run the following command: systemctl enable myapp.service
Once installed, you can start and stop the service in the usual way. For example, on a Debian-based system, you could start it with the following command: service myapp start

If your application fails to start, check the log file written to /var/log/<appname>.log for errors.