A Spring Boot application is a Java-based web application built using the Spring Boot framework. Spring Boot is an open-source framework designed to simplify the process of creating production-grade, stand-alone Spring-based applications. It provides various features and conventions that streamline configuration, dependency management, and application deployment.
In this tutorial, we will show you how to deploy spring boot application on Ubuntu 22.04.
Step 1 – Install Java JDK
Spring Boot requires Java to be installed on your server. You can install it using the following command:
apt install openjdk-18-jdk-headless -y
After the successful installation, verify the Java version using the following command:
java -version
Output:
openjdk version "18.0.2-ea" 2022-07-19 OpenJDK Runtime Environment (build 18.0.2-ea+9-Ubuntu-222.04) OpenJDK 64-Bit Server VM (build 18.0.2-ea+9-Ubuntu-222.04, mixed mode, sharing)
Step 2 – Install Spring Boot CLI
Spring Boot CLI (Command Line Interface) is a command-line tool provided by the Spring Boot framework for quickly developing and testing Spring Boot applications without needing to set up a full-fledged project structure. It allows developers to write, run, and test Spring Boot applications with minimal configuration.
There are multiple ways to install Spring Boot CLI. In this section, we will install Spring Boot CLI using SDKMAN.
First, install the Zip and Unzip utility.
apt install unzip zip
Next, install SDKMAN using the following command:
curl -s https://get.sdkman.io | bash
Output:
All done! You are subscribed to the STABLE channel. Please open a new terminal, or run the following in the existing one: source "/root/.sdkman/bin/sdkman-init.sh" Then issue the following command: sdk help Enjoy!!!
Next, source the new SDKMAN! shell from the terminal window.
source "/root/.sdkman/bin/sdkman-init.sh"
Next, install the Spring Boot CLI using SDK.
sdk install springboot
Step 3 – Create a Spring Boot Application
In this section, we will create a simple “Hello World” Spring Boot application using Maven.
First, install Gradle using the following command.
sdk install gradle 7.5.1
Next, generate a simple Spring Boot project named “hello” in the “hello-world” directory, with Gradle as the build tool.
spring init --build=gradle --dependencies=web --name=hello hello-world --type=gradle-project
Next, edit the HelloApplication.java file.
nano ~/hello-world/src/main/java/com/example/helloworld/HelloApplication.java
Modify is as shown below:
package com.example.helloworld; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; @SpringBootApplication public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); } } @RestController class Hello { @RequestMapping("/") String index() { return "Hello world"; } }
Save and close the file.
Then, navigate inside your application directory and create a new build with the following command:
cd hello-world ./gradlew build
Step 4 – Run Spring Boot Application
First, run the application without building the jar file to test it quickly.
gradle bootRun
If everything is fine, you will see the following output:
> Task :bootRun . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v3.2.4)
Press CTRL+C to stop the application.
Step 5 – Create a Systemd File
It is always a good idea to create a systemd file to manage a Spring Boot application. You can create it using the following command:
nano /etc/systemd/system/helloworld.service
Add the following lines:
[Unit] Description=Spring Boot HelloWorld After=syslog.target After=network.target[Service] User=root Type=simple [Service] ExecStart=/usr/bin/java -jar /root/hello-world/build/libs/hello-world-0.0.1-SNAPSHOT.jar Restart=always StandardOutput=syslog StandardError=syslog SyslogIdentifier=helloworld [Install] WantedBy=multi-user.target
Save the file, then reload the systemd daemon.
systemctl daemon-reload
Now, start the Spring Boot service using the following command.
systemctl start helloworld
To check your application running status, run:
systemctl status helloworld
Output:
● helloworld.service - Spring Boot HelloWorld Loaded: loaded (/etc/systemd/system/helloworld.service; disabled; vendor preset: enabled) Active: active (running) since Sun 2024-03-24 08:26:48 UTC; 4s ago Main PID: 169887 (java) Tasks: 36 (limit: 4579) Memory: 109.4M CPU: 3.850s CGroup: /system.slice/helloworld.service └─169887 /usr/bin/java -jar /root/hello-world/build/libs/hello-world-0.0.1-SNAPSHOT.jar
Step 6 – Configure Nginx as a Reverse Proxy
You will also need to configure Nginx reverse proxy to implement the application as a web service.
First, install the Nginx package.
apt install nginx -y
Next, create an Nginx virtual host:
nano /etc/nginx/conf.d/helloworld.conf
Add the following configuration:
server { listen 80; server_name helloworld.example.com; location / { proxy_pass http://localhost:8080/; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Port $server_port; } }
Save the file and then edit the Nginx main configuration file:
nano /etc/nginx/nginx.conf
Add the following line after the line http{:
server_names_hash_bucket_size 64;
Save and close the file, then verify Nginx for any syntax errors.
nginx -t
Output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Finally, restart the Nginx service to apply the changes.
systemctl restart nginx
Step 7 – Access Spring Boot Application
At this point, your Spring Boot application is created with Nginx as a reverse proxy. You can now access it using the URL http://helloworld.example.com.
Conclusion
The Spring Boot application empowers developers to confidently build and deploy robust, production-grade applications while optimizing resource utilization and enhancing the development workflow. You can easily create and deploy your Spring Boot application on dedicated server hosting from Atlantic.Net!