Spring Boot makes it easy to build and deploy Java web applications with minimal configuration. By default, Spring Boot includes an embedded Tomcat server, allowing applications to be packaged as standalone JARs. For production environments, managing your Spring Boot application as a systemd service ensures reliability, automatic restarts, and easy monitoring.
In this guide, we demonstrate how to deploy a spring boot application as a systemd service.
Prerequisites:
For the success of this deployment, ensure you have access to a Linux server with the right permissions. See the defaults used in this guide and adjust them as needed.
Working directory: /home/ubuntu/dtechnologies.co.ke
Domain name: dtechnologies.co.ke
Systemd Service name: dtechnologies.service
Snapshot name: dtect-springboot-0.0.1-SNAPSHOT.jar
Step 1: Build Your Spring Boot Application
Before deploying, ensure your application is properly built. Run the following command to generate a JAR file:
mvn clean install
This will create a .jar file in the target/ directory, such as:
target/dtect-springboot-0.0.1-SNAPSHOT.jar
Step 2. Prepare the Server
Access the server and install the required services. Update your system using the command:
sudo apt update && sudo apt upgrade -y
Install Java on the Server
Your Spring Boot application requires Java to run. For most Linux-based servers, you can install OpenJDK.
sudo apt install openjdk-17-jdk -y
java -version #check the version.
Step 3: Transfer the JAR to the Server
Use SCP or any file transfer method to move the JAR file to your Linux server:
scp target/dtect-springboot-0.0.1-SNAPSHOT.jar user@ServerIP:/home/ubuntu/
#Access the server and move the jar file to the workdirectory
mv dtect-springboot-0.0.1-SNAPSHOT.jar dtechnologies.co.ke
Ensure the directory /home/domain-name exists and is accessible by your user:
mkdir -p /home/ubuntu/dtechnologies.co.ke
Change permissions/Ownership
To ensure the success of this deployment, permissions needs to be set right. Adjust the permissions of the document root as illustrated below. Ensure to adjust the paths to suit your case.
# Change ownership of the application directory to the www-data user and group.
# This ensures the web server (e.g., Nginx, running as www-data) has the necessary permissions
# to access and serve files from this directory.
sudo chown -R www-data:www-data /home/ubuntu/dtechnologies.co.ke
# Set directory permissions to 755:
# - Owner (www-data) gets read, write, and execute (7).
# - Group and others get read and execute (5).
# This allows the web server to read files and execute scripts if needed while maintaining security.
sudo chmod 755 /home/ubuntu/dtechnologies.co.ke
# Add the www-data user to the ubuntu group as a supplementary group.
# This allows the web server (www-data) to access files and resources that belong to the ubuntu user,
# which is useful when working with shared directories, logs, or socket files.
sudo usermod -aG ubuntu www-data
Step 4: Create a Systemd Service File
Create a new systemd service file in /etc/systemd/system/:
sudo nano /etc/systemd/system/dtechnologies.service
Add the following configuration:
[Unit]
Description=Spring Boot Web Application
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory= /home/ubuntu/dtechnologies.co.ke
ExecStart=/usr/bin/java -jar /home/ubuntu/dtechnologies.co.ke/dtect-springboot-0.0.1-SNAPSHOT.jar
SuccessExitStatus=143
Restart=always
RestartSec=10
Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64"
Environment="SPRING_PROFILES_ACTIVE=prod"
[Install]
WantedBy=multi-user.target
Step 5: Reload Systemd and Start the Service
Run the following commands to enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable dtechnologies.service
sudo systemctl start dtechnologies.service
Check the status:
sudo systemctl status dtechnologies.service
If everything is set up correctly, the application should be running! If the service is yet to start, you can inspect the service logs and take the necessary actions
journalctl -u dtechnologies.service -f
Now, you can access your app via:
http://your-server-ip:8080/
Step 6: Managing the Service
Use these commands to manage the service:
Restart the service: sudo systemctl restart dtechnologies.service
Stop the service: sudo systemctl stop dtechnologies.service
View logs: sudo journalctl -u dtechnologies.service -f
Step 7: Configure a domain name
To expose your Spring Boot app to the internet and make it accessible via HTTP (port 80), set up a reverse proxy with Nginx
Install Nginx
Run the following command to install nginx
sudo apt install nginx -y
Configure Nginx for Reverse Proxy
Create a configuration file for your site in /etc/nginx/sites-available/ and link it to /etc/nginx/sites-enabled/
sudo nano /etc/nginx/sites-available/springboot-web
Add the following configuration:
server {
listen 80;
server_name dtechnologies.co.ke www.dtechnologies.co.ke; # Replace with your domain name
location / {
proxy_pass http://localhost:8080; # Change the port if needed
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Save the file, then create a symbolic link to enable the site:
sudo ln -s /etc/nginx/sites-available/springboot-web /etc/nginx/sites-enabled/
Test and Restart Nginx
Test the created nginx configuration and proceed to restart the service:
sudo nginx -t
sudo systemctl restart nginx #If the test is ok, restart nginx.
step 8: Secure Your Domain with SSL
Once a domain name has been configured, it is essential to secure our application using an SSL certificate. In this guide, we review how to install the Free ssl certificate.
Install Certbot (Let’s Encrypt)
Certbot is a tool that helps you automatically set up SSL certificates for your domain using Let’s Encrypt.
sudo apt install certbot python3-certbot-nginx -y
Get SSL Certificate
Run Certbot to obtain and install an SSL certificate for your domain:- follow the prompts.
sudo certbot --nginx -d dtechnologies.co.ke -d www.dtechnologies.co.ke
Conclusion:
By setting up your Spring Boot application as a systemd service, you ensure that it runs smoothly, restarts automatically in case of failure, and starts up on system boot. This approach is ideal for production environments.
Make a donation to support us
Web Hosting and email hosting Packages
Related Articles: