Spring Boot is a popular framework for building production-grade Java applications with minimal configuration. Deploying a Spring Boot application on FreeBSD involves setting up the Java runtime environment, configuring your database, and managing your application as a service. This guide will walk you through the process step-by-step.
Prerequisites
Before you begin, ensure you have the following:
- A FreeBSD server with root access.
- Basic knowledge of the command line.
- A Spring Boot application ready for deployment.
- Java Development Kit (JDK) installed on your system.
Step 1: Install Java
Spring Boot applications require Java to run. We'll install OpenJDK, an open-source implementation of the Java Platform.
1.1 Update the Package Repository
First, update your package repository:
sudo pkg update
sudo pkg upgrade1.2 Install OpenJDK
Install the OpenJDK package. Spring Boot 3 and later need Java 17 or newer, so this guide uses OpenJDK 21, a Long-Term Support (LTS) release:
sudo pkg install openjdk211.3 Set JAVA_HOME Environment Variable
Set the JAVA_HOME environment variable to point to your JDK installation. Add the following lines to your shell profile (e.g., .bashrc, .zshrc):
export JAVA_HOME=/usr/local/openjdk21
export PATH=$JAVA_HOME/bin:$PATHReload your shell profile:
source ~/.bashrcVerify the Java installation:
java -versionStep 2: Install a Database (Optional)
If your Spring Boot application requires a database, install and configure the database server. In this guide, we'll use PostgreSQL.
2.1 Install PostgreSQL
Install the PostgreSQL server:
sudo pkg install postgresql18-server postgresql18-contrib2.2 Initialize and Start PostgreSQL
Initialize the database cluster:
sudo service postgresql initdbEnable and start the PostgreSQL service:
sudo sysrc postgresql_enable=yes
sudo service postgresql start2.3 Configure PostgreSQL
Switch to the PostgreSQL user and create a new database and user for your Spring Boot application:
sudo su - postgres
psqlInside the PostgreSQL shell, run the following commands:
CREATE DATABASE myappdb;
CREATE USER myappuser WITH ENCRYPTED PASSWORD 'password';
ALTER DATABASE myappdb OWNER TO myappuser;
\q
exitReplace myappdb, myappuser, and password with your desired database name, username, and password. Making the application user the database owner matters on PostgreSQL 15 and later: the public schema belongs to the database owner, so GRANT ALL PRIVILEGES ON DATABASE alone no longer lets the application create its tables.
Step 3: Deploy Your Spring Boot Application
3.1 Transfer Your Application
Transfer your Spring Boot application JAR file to your FreeBSD server using scp or another file transfer method:
scp path/to/your/app.jar username@your_freebsd_server:/path/to/deploy/3.2 Create an rc.d Service Script
FreeBSD manages services with rc.d scripts rather than systemd units. Locally installed services belong in /usr/local/etc/rc.d; /etc/rc.d is reserved for the base system. Create a script for your application:
sudo nano /usr/local/etc/rc.d/myappAdd the following content:
#!/bin/sh
# PROVIDE: myapp
# REQUIRE: LOGIN
# KEYWORD: shutdown
. /etc/rc.subr
name="myapp"
rcvar="myapp_enable"
load_rc_config $name
: ${myapp_enable:="NO"}
: ${myapp_user:="myuser"}
: ${myapp_group:="mygroup"}
: ${myapp_home:="/path/to/deploy"}
: ${myapp_java_opts:=""}
: ${myapp_jar:="app.jar"}
pidfile="/var/run/${name}.pid"
procname="/usr/local/openjdk21/bin/java"
command="/usr/sbin/daemon"
command_args="-f -p ${pidfile} ${procname} ${myapp_java_opts} -jar ${myapp_home}/${myapp_jar}"
start_precmd="${name}_prestart"
myapp_prestart()
{
# rc.subr starts the service as ${myapp_user}, which can't create files
# in /var/run, so create the pidfile for it first.
install -o ${myapp_user} -g ${myapp_group} /dev/null ${pidfile}
}
run_rc_command "$1"Two lines matter more than they look. Because myapp_user is set, rc.subr starts the application as that user rather than root — which is why myapp_prestart creates the pidfile first, since an unprivileged process can't create files in /var/run. And procname tells rc.subr that the process recorded in the pidfile is Java rather than daemon, so service myapp status and service myapp stop can find it.
Make the script executable:
sudo chmod +x /usr/local/etc/rc.d/myapp3.3 Configure the Service
Enable the service by adding the following line to /etc/rc.conf:
sudo sysrc myapp_enable="YES"3.4 Start the Service
Start your Spring Boot application as a service:
sudo service myapp startCheck the status of the service to ensure it is running:
sudo service myapp statusStep 4: Configure Firewall (Optional)
If you have a firewall enabled, ensure that the port your Spring Boot application is listening on (e.g., 8080) is open. Configure pf (Packet Filter) to allow traffic on this port:
4.1 Edit the pf Configuration File
sudo nano /etc/pf.confAdd a rule to allow traffic on port 8080:
pass in on em0 proto tcp from any to any port 80804.2 Enable and Reload pf
Enable pf and reload the configuration:
sudo sysrc pf_enable="YES"
sudo service pf reloadStep 5: Access Your Application
Your Spring Boot application should now be running and accessible. Open a web browser and navigate to http://your_freebsd_server:8080 to see your application in action.
Conclusion
Deploying a Spring Boot application on FreeBSD involves setting up the Java runtime environment, configuring a database (if needed), and managing your application as a service. By following this guide, you should have a fully functional Spring Boot application running on your FreeBSD server. This setup provides a robust and flexible environment for running your Java applications, leveraging the stability and performance of FreeBSD. Happy coding!
Follow me on:
X: @maxiujun
Threads.net @xiujunma
BlueSky: @maxiujun.bsky.social
