DEV Community

Shakhzhakhan Maxudbek
Shakhzhakhan Maxudbek

Posted on • Edited on • Originally published at args.tech

1 1 1 1

Deploy Java Spring boot project as systemd service

How to run Java Spring Boot based web application in Linux? You may just run in terminal:

java -jar /home/user/your_project_name/your_project_name.jar
Enter fullscreen mode Exit fullscreen mode

Note: in this case you can't escape from terminal.

Second way - using nohup:

nohup java -jar /home/user/your_project_name/your_project_name.jar &
Enter fullscreen mode Exit fullscreen mode

Ok, it works, but better way for run your application, especially at production server - create systemd service for fully control them (start, stop, see status, etc...).

We need to install Java Development Kit (JDK). Install opensource version on Debian-based systems:

sudo apt install openjdk-17-jdk -y
Enter fullscreen mode Exit fullscreen mode

Oracle Java official installers here.

Now find JAVA binary file's full path:

which java
Enter fullscreen mode Exit fullscreen mode

You must see output like this:

/usr/bin/java
Enter fullscreen mode Exit fullscreen mode

We use this path when configure systemd file.

Create service file for your project:

sudo nano /etc/systemd/system/your_project_name.service
Enter fullscreen mode Exit fullscreen mode

Insert below configuration:

[Unit]
Description=Webserver daemon

[Service]
ExecStart=/usr/bin/java -jar /home/user/your_project_name/your_project_name.jar --spring.profiles.active=production --spring.config.location=/home/user/your_project_name/application.properties
User=user

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

After you save daemon file, reload systemd manager configuration:

sudo systemctl daemon-reload
Enter fullscreen mode Exit fullscreen mode

Reload the systemd manager configuration. This will rerun all generators (see systemd.generator(7)), reload all unit files, and recreate the entire dependency tree. While the daemon is being reloaded, all sockets systemd listens on behalf of user configuration will stay accessible.
This command should not be confused with the reload command. See documentation for more details.

Next step - start your service:

sudo systemctl start your_project_name.service
Enter fullscreen mode Exit fullscreen mode

Project run as service, but it can't start after system reboot. Here need enable autorun:

sudo systemctl enable your_project_name.service
Enter fullscreen mode Exit fullscreen mode

Are my posts is helpful? You may support me on Patreon.

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay