DEV Community

Sumer Ahmed
Sumer Ahmed

Posted on

Deploy Spring Boot Applications with NGINX and Ubuntu

Step by step, do the following:

Installing Java JDK 17 or 21 However, a reasonably recent (LTS) release is recommended.

Ensure software-properties-common is installed.

sudo apt install software-properties-common
Enter fullscreen mode Exit fullscreen mode

Install Amazon Corretto 21

wget -O - https://apt.corretto.aws/corretto.key | sudo gpg --dearmor -o /usr/share/keyrings/corretto-keyring.gpg && \
echo "deb [signed-by=/usr/share/keyrings/corretto-keyring.gpg] https://apt.corretto.aws stable main" | sudo tee /etc/apt/sources.list.d/corretto.list

sudo apt-get update; sudo apt-get install -y java-21-amazon-corretto-jdk

java -version
Enter fullscreen mode Exit fullscreen mode

Installing NGINX

sudo apt install nginx

sudo systemctl status nginx

sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Be shore you stop apache2 & allow OpenSSH and nginx ports throw UFW

sudo ufw allow OpenSSH
sudo ufw allow in "Nginx Full"
sudo ufw enable

sudo ufw status
Enter fullscreen mode Exit fullscreen mode

install unzip and zip too

sudo apt install unzip zip
Enter fullscreen mode Exit fullscreen mode

Installing the Spring Boot CLI

# Install sdkman The Software Development Kit Manager
curl -s "https://get.sdkman.io" | bash

# Install springboot CLI
sdk install springboot

# Note: if you get sdk not found, do the following
nano ~/.bashrc

## Add this line in the end of file
export PATH=$PATH:/usr/local/sdkman/bin

## Finally
source ~/.bashrc
sdk install springboot

## Then we can init new project like this

spring version

spring init --list
spring init --build=maven --java-version=21 --dependencies=web,data-jpa --packaging=jar app-name.zip
Enter fullscreen mode Exit fullscreen mode

Install Maven

wget https://dlcdn.apache.org/maven/maven-3/3.9.6/binaries/apache-maven-3.9.6-bin.tar.gz
tar -xvf apache-maven-3.9.6-bin.tar.gz
mv apache-maven-3.9.6 /opt/

#  Setting M2_HOME and Path Variables
M2_HOME='/opt/apache-maven-3.9.6'
PATH="$M2_HOME/bin:$PATH"
export PATH

# Show the mvn version
mvn -version
Enter fullscreen mode Exit fullscreen mode

Build the jar file

java -jar build/libs/app-name-0.0.1-SNAPSHOT.jar
Enter fullscreen mode Exit fullscreen mode

Creating an Init Script for the Spring Boot Application

Do nano /etc/systemd/system/helloworld.service and add the content:

[Unit]
Description=Spring Boot app-name
After=syslog.target
After=network.target[Service]
User=username
Type=simple

[Service]
ExecStart=/usr/bin/java -jar /home/userdir/app-name/build/libs/app-name-0.0.1-SNAPSHOT.jar
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=appname

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

start the service

sudo systemctl start appname
Enter fullscreen mode Exit fullscreen mode

Configuring a Reverse Proxy for the Spring Boot Application

server {
        listen 80;
        listen [::]:80;

        server_name 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;
        }
}
Enter fullscreen mode Exit fullscreen mode
sudo ln -s /etc/nginx/sites-available/appname.conf /etc/nginx/sites-enabled/
# Unlink the default
sudo unlink /etc/nginx/sites-enabled/default
# Test the conf
sudo nginx -t
# Restart nginx
sudo systemctl restart nginx
Enter fullscreen mode Exit fullscreen mode

Top comments (0)