Deploy Spring Boot Applications with NGINX and Ubuntu
Tags: webdev, java, springboot, spring
Published: 2024-06-07 09:01:09

**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.

```sh
sudo apt install software-properties-common
```

## Install Amazon Corretto 21

```sh
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
```

## Installing NGINX

```sh
sudo apt install nginx

sudo systemctl status nginx

sudo systemctl restart nginx
```

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

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

sudo ufw status
```
## install `unzip` and `zip` too

```sh
sudo apt install unzip zip
```

## Installing the Spring Boot CLI

```sh
# 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
```

## Install Maven

```sh
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
```

## Build the jar file

```sh
java -jar build/libs/app-name-0.0.1-SNAPSHOT.jar
```
## Creating an Init Script for the Spring Boot Application

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

```sh
[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
```
## start the service

```sh
sudo systemctl start appname
```

## Configuring a Reverse Proxy for the Spring Boot Application

```sh
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;
        }
}
```

```sh
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
```