The following configuration is required in build.gradle file in Spring Boot projects.
build.gradle
jar {
baseName = 'your-app'
version = version
}
springBoot {
buildInfo()
executable = true
mainClass = "com.shunya.App"
}
executable = true
This is required to make fully executable jar on unix system (Centos and Ubuntu)
Create a .conf file
If you want to configure custom JVM properties or Spring Boot application run arguments, then you can create a .conf file with the same name as the Spring Boot application name and place it parallel to jar file.
Considering that your-app.jar is the name of your Spring Boot application, then you can create the following file.
JAVA_OPTS="-Xms64m -Xmx64m"
RUN_ARGS=--spring.profiles.active=prod
LOG_FOLDER=/custom/log/folder
This configuration will set 64 MB ram for the Spring Boot application and activate prod profile.
Create a new user in linux
For enhanced security we must create a specific user to run the Spring Boot application as a service.
Create a new user
sudo useradd -s /sbin/nologin springboot
On Ubuntu / Debian, modify the above command as follow:
sudo useradd -s /usr/sbin/nologin springboot
Set password
sudo passwd springboot
Make springboot owner of the executable file
chown springboot:springboot your-app.jar
Prevent the modification of jar file
chmod 500 your-app.jar
This will configure jar’s permissions so that it can not be written and can only be read or executed by its owner springboot.
You can optionally make your jar file as immutable using the change attribute (chattr) command.
sudo chattr +i your-app.jar
Appropriate permissions should be set for the corresponding .conf file as well. .conf requires just read access (Octal 400) instead of read + execute (Octal 500) access
chmod 400 your-app.conf
Create Systemd service
/etc/systemd/system/your-app.service
[Unit]
Description=Your app description
After=syslog.target
[Service]
User=springboot
ExecStart=/var/myapp/your-app.jar
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
Automatically restart process if it gets killed by OS
Append the below two attributes (Restart and RestartSec) to automatically restart the process on failure.
/etc/systemd/system/your-app.service
[Service]
User=springboot
ExecStart=/var/myapp/your-app.jar
SuccessExitStatus=143
Restart=always
RestartSec=30
The change will make Spring Boot application restart in case of failure with a delay of 30 seconds. If you stop the service using systemctl command then restart will not happen.
Schedule service at system startup
To flag the application to start automatically on system boot, use the following command:
Enable Spring Boot application at system startup
sudo systemctl enable your-app.service
Start an Stop the Service
systemctl can be used in Ubuntu 16.04 LTS and 18.04 LTS to start and stop the process.
Start the process
sudo systemctl start your-app
Stop the process
sudo systemctl stop your-app
References
https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html