Avís
Aquest article és antic i és possible que part de la informació estigui desactualitzada o ja no sigui vàlida actualment.
Si tens cap dubte o necessites confirmar algun detall, et recomanem contactar amb el nostre equip de suport per rebre informació actualitzada i assistència personalitzada.
Spring Boot és un framework gratuït i de codi obert basat en Java desenvolupat per Pivotal Team que s'utilitza per crear un microservei. Un microservei permet desenvolupar i implementar serveis de manera independent.
Spring Boot proporciona una bona plataforma per desenvolupar fàcilment una aplicació independent i d'entorn de producció. S'ha dissenyat especialment per evitar configuracions XML complexes, reduir el temps de desenvolupament i oferir una manera fàcil de començar amb l'aplicació. No cal configurar res, tot es configura automàticament.
En aquest tutorial, t'expliquem com implementar Spring Boot amb Nginx com a servidor intermediari invers a Ubuntu 24.04.
Requisits
- Un servidor al núvol que executi Ubuntu 24.04.
- Una contrasenya root al teu servidor al núvol.
- Java en la versió 8 o superior
Comencem
Abans de començar, assegura't que el teu servidor està actualitzat. Si no és així, pots actualitzar el teu servidor amb l'ordre següent:
# apt update && apt upgrade -yA continuació instal·larem el paquet zip ja que el necessitarem més endavant:
# apt install zip -ySpring Boot és una aplicació basada en Java, per la qual cosa hauràs d'instal·lar Java al teu servidor. Pots instal·lar Java 21 amb l'ordre següent:
# apt install openjdk-17-jdk -yUn cop instal·lat, pots comprovar la versió de Java amb l'ordre següent:
# java -versionHauries de veure el resultat següent:
openjdk 17.0.18 2026-01-20
OpenJDK Runtime Environment (build 17.0.18+8-Ubuntu-124.04.1)
OpenJDK 64-Bit Server VM (build 17.0.18+8-Ubuntu-124.04.1, mixed mode, sharing)
Instal·la l'eina Spring Boot CLI
A continuació, hauràs d'instal·lar Spring Boot CLI i altres eines com Gradle al teu servidor al núvol. Pots crear un projecte nou fàcilment amb l'eina Spring Boot CLI.
En primer lloc, hauràs d'instal·lar SDKMAN al teu servidor VPS al núvol. Pots fer-ho amb l'script següent:
# curl -s https://get.sdkman.io | bashUn cop completada la instal·lació, hauries de veure el resultat següent:
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!!!
A continuació, activa SDKMAN amb l'ordre següent:
# source "/root/.sdkman/bin/sdkman-init.sh"Pots comprovar totes les opcions disponibles amb sdk utilitzant l'ordre següent:
# sdk helpHauries de veure el resultat següent:
NAME
sdk - The command line interface (CLI) for SDKMAN!
SYNOPSIS
sdk <subcommand> [candidate] [version]
DESCRIPTION
SDKMAN! is a tool for managing parallel versions of multiple JVM related
Software Development Kits on most Unix based systems. It provides a
convenient Command Line Interface (CLI) and API for installing, switching,
removing and listing Candidates.
SUBCOMMANDS & QUALIFIERS
help [subcommand]
install <candidate> [version] [path]
uninstall <candidate> <version>
list [candidate]
use <candidate> <version>
config no qualifier
default <candidate> [version]
home <candidate> <version>
env [init|install|clear]
current [candidate]
upgrade [candidate]
version no qualifier
selfupdate [force]
update no qualifier
flush [tmp|metadata|version]
EXAMPLES
sdk install java 17.0.0-tem
sdk help install
A continuació, pots instal·lar Spring Boot utilitzant l'ordre que es mostra més avall:
# sdk install springbootUn cop instal·lat, hauries d'obtenir el resultat següent:
Downloading: springboot 4.0.6
In progress...
###################################################################################################################################### 100.0%
Installing: springboot 4.0.6
Done installing!
Setting springboot 4.0.6 as default.
També pots comprovar la versió de Spring Boot amb l'ordre següent:
# spring versionHauries de veure el resultat següent:
Spring CLI v4.0.6
A continuació, instal·la gradle executant l'ordre següent:
# sdk install gradle 9.4.1Crea un fitxer Jar amb Gradle
A continuació, crea un projecte nou anomenat hello-world amb Spring Boot CLI i Gradle executant l'ordre següent:
# spring init --type=gradle-project --dependencies=web --name=hello hello-world
Aquesta ordre crearà un directori de projecte nou anomenat hello-world al teu directori principal, tal com es mostra més avall:
Using service at https://start.spring.io
Project extracted to '/root/hello-world'Pots llistar tots els paràmetres disponibles amb Spring Boot CLI executant l'ordre següent:
# spring init --listEntre moltes altres opcions, pots especificar opcions per compilar el projecte i diferents paràmetres:
Project types (* denotes the default)
+-----------------------+--------------------------------------------------------------+--------------------------------------------+
| Id | Description | Tags |
+-----------------------+--------------------------------------------------------------+--------------------------------------------+
| gradle-build | Generate a Gradle build file. | build:gradle,format:build |
| | | |
| gradle-project * | Generate a Gradle based project archive using the Groovy | build:gradle,dialect:groovy,format:project |
| | DSL. | |
| | | |
| gradle-project-kotlin | Generate a Gradle based project archive using the Kotlin | build:gradle,dialect:kotlin,format:project |
| | DSL. | |
| | | |
| maven-build | Generate a Maven pom.xml. | build:maven,format:build |
| | | |
| maven-project | Generate a Maven based project archive. | build:maven,format:project |
+-----------------------+--------------------------------------------------------------+--------------------------------------------+
Parameters
+-------------------------+------------------------------------------+------------------+
| Id | Description | Default value |
+-------------------------+------------------------------------------+------------------+
| artifactId | project coordinates (infer archive name) | demo |
| bootVersion | spring boot version | 4.0.6 |
| configurationFileFormat | configuration file format | properties |
| description | project description | |
| groupId | project coordinates | com.example |
| javaVersion | language level | 17 |
| language | programming language | java |
| name | project name (infer application name) | |
| packageName | root package | com.example.demo |
| packaging | project packaging | jar |
| type | project type | gradle-project |
| version | project version | 0.0.1-SNAPSHOT |
+-------------------------+------------------------------------------+------------------+
A continuació, canvia al directori hello-world i obre el teu fitxer hello-word de Java:
# cd hello-world
# nano src/main/java/com/example/helloworld/HelloApplication.javaFes els canvis següents:
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";
}
}
Desa i tanca el fitxer. Després, crea l'aplicació amb l'ordre següent:
# ./gradlew buildHauries de veure el resultat següent:
Downloading https://services.gradle.org/distributions/gradle-9.4.1-bin.zip
.............10%.............20%.............30%.............40%.............50%.............60%..............70%.............80%.............90%.............100%
Welcome to Gradle 9.4.1!
Here are the highlights of this release:
- Java 26 support
- Non-class-based JVM tests
- Enhanced console progress bar
For more details see https://docs.gradle.org/9.4.1/release-notes.html
Starting a Gradle Daemon (subsequent builds will be faster)
BUILD SUCCESSFUL in 53s
7 actionable tasks: 7 executed
Després, executa l'aplicació amb l'ordre següent:
# java -jar build/libs/hello-world-0.0.1-SNAPSHOT.jarAra la teva aplicació s'està executant en un servlet Tomcat al port 8080, tal com es mostra més avall:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v4.0.6)
2026-04-27T13:46:22.426+02:00 INFO 4439 --- [hello] [ main] c.example.hello_world.HelloApplication : Starting HelloApplication v 0.0.1-SNAPSHOT using Java 17.0.18 with PID 4439 (/root/hello-world/build/libs/hello-world-0.0.1-SNAPSHOT.jar started by root in /root/hello-w orld)
2026-04-27T13:46:22.437+02:00 INFO 4439 --- [hello] [ main] c.example.hello_world.HelloApplication : No active profile set, fall ing back to 1 default profile: "default"
2026-04-27T13:46:24.315+02:00 INFO 4439 --- [hello] [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat initialized with por t 8080 (http)
2026-04-27T13:46:24.353+02:00 INFO 4439 --- [hello] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2026-04-27T13:46:24.353+02:00 INFO 4439 --- [hello] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [A pache Tomcat/11.0.21]
2026-04-27T13:46:24.614+02:00 INFO 4439 --- [hello] [ main] b.w.c.s.WebApplicationContextInitializer : Root WebApplicationContext: initialization completed in 2046 ms
2026-04-27T13:46:25.838+02:00 INFO 4439 --- [hello] [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2026-04-27T13:46:25.871+02:00 INFO 4439 --- [hello] [ main] c.example.hello_world.HelloApplication : Started HelloApplication in 4.594 seconds (process running for 5.906)
Després, prem CTRL+C per aturar l'aplicació i tornar-la a executar amb Gradle:
# gradle bootRunHauries de veure el resultat següent:
> Task :bootRun
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v4.0.6)
2026-04-27T13:47:54.415+02:00 INFO 4523 --- [hello] [ main] c.example.hello_world.HelloApplication : Starting HelloApplication using Java 17.0.18 with PID 4523 (/root/hello-world/build/classes/java/main started by root in /root/hello-world)
2026-04-27T13:47:54.419+02:00 INFO 4523 --- [hello] [ main] c.example.hello_world.HelloApplication : No active profile set, falling back to 1 default profile: "default"
2026-04-27T13:47:55.269+02:00 INFO 4523 --- [hello] [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat initialized with port 8080 (http)
2026-04-27T13:47:55.281+02:00 INFO 4523 --- [hello] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2026-04-27T13:47:55.282+02:00 INFO 4523 --- [hello] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/11.0.21]
2026-04-27T13:47:55.426+02:00 INFO 4523 --- [hello] [ main] b.w.c.s.WebApplicationContextInitializer : Root WebApplicationContext: initialization completed in 962 ms
2026-04-27T13:47:55.911+02:00 INFO 4523 --- [hello] [ main] o.s.boot.tomcat.TomcatWebServer : Tomcat started on port 8080 (http) with context path '/'
2026-04-27T13:47:55.925+02:00 INFO 4523 --- [hello] [ main] c.example.hello_world.HelloApplication : Started HelloApplication in 1.986 seconds (process running for 2.511)
Crea un fitxer de servei Systemd per a Spring Boot
A continuació, hauràs de crear un fitxer de servei systemd per gestionar la teva aplicació. Pots fer-ho amb l'ordre següent:
# nano /etc/systemd/system/helloworld.serviceAfegeix les línies següents:
[Unit]
Description=Spring Boot HelloWorld
After=syslog.target network.target
[Service]
User=root
Type=simple
ExecStart=/usr/bin/java -jar /root/hello-world/build/libs/hello-world-0.0.1-SNAPSHOT.jar
Restart=always
StandardOutput=journal
StandardError=journal
SyslogIdentifier=helloworld
[Install]
WantedBy=multi-user.target
Desa i tanca el fitxer. Aleshores, torna a carregar el dimoni systemd amb l'ordre següent:
# systemctl daemon-reloadA continuació, inicia el servei helloworld i habilita'n l'inici durant l'arrencada utilitzant l'ordre següent:
# systemctl start helloworld
# systemctl enable helloworldAra ja pots comprovar l'estat del teu servei amb l'ordre següent:
# systemctl status helloworldHauries de veure el resultat següent:
● helloworld.service - Spring Boot HelloWorld
Loaded: loaded (/etc/systemd/system/helloworld.service; disabled; preset: enabled)
Active: active (running) since Mon 2026-04-27 13:52:48 CEST; 4s ago
Main PID: 4670 (java)
Tasks: 18 (limit: 2263)
Memory: 97.1M (peak: 97.4M)
CPU: 4.865s
CGroup: /system.slice/helloworld.service
└─4670 /usr/bin/java -jar /root/hello-world/build/libs/hello-world-0.0.1-SNAPSHOT.jar
Apr 27 13:52:50 amb-dev helloworld[4670]: \\/ ___)| |_)| | | | | || (_| | ) ) ) )
Apr 27 13:52:50 amb-dev helloworld[4670]: ' |____| .__|_| |_|_| |_\__, | / / / /
Apr 27 13:52:50 amb-dev helloworld[4670]: =========|_|==============|___/=/_/_/_/
Apr 27 13:52:50 amb-dev helloworld[4670]: :: Spring Boot :: (v4.0.6)
Apr 27 13:52:50 amb-dev helloworld[4670]: 2026-04-27T13:52:50.377+02:00 INFO 4670 --- [hello] [ main] c.example.hello_world.HelloApplication : St>
Apr 27 13:52:50 amb-dev helloworld[4670]: 2026-04-27T13:52:50.387+02:00 INFO 4670 --- [hello] [ main] c.example.hello_world.HelloApplication : No>
Apr 27 13:52:52 amb-dev helloworld[4670]: 2026-04-27T13:52:52.299+02:00 INFO 4670 --- [hello] [ main] o.s.boot.tomcat.TomcatWebServer : To>
Apr 27 13:52:52 amb-dev helloworld[4670]: 2026-04-27T13:52:52.330+02:00 INFO 4670 --- [hello] [ main] o.apache.catalina.core.StandardService : St>
Apr 27 13:52:52 amb-dev helloworld[4670]: 2026-04-27T13:52:52.331+02:00 INFO 4670 --- [hello] [ main] o.apache.catalina.core.StandardEngine : St>
Apr 27 13:52:52 amb-dev helloworld[4670]: 2026-04-27T13:52:52.563+02:00 INFO 4670 --- [hello] [ main] b.w.c.s.WebApplicationContextInitializer : Ro>
Configura Nginx com a servidor intermediari invers
A continuació, hauràs de configurar Nginx com a servidor intermediari invers per a una petició de servidor intermediari dels ports 80 a 8080. Primer, instal·la el servidor Nginx executant l'ordre següent:
# apt install nginx -yUn cop instal·lat, crea un fitxer nou de configuració d'amfitrió virtual per a la teva aplicació amb l'ordre següent:
# nano /etc/nginx/conf.d/helloworld.confAfegeix les línies següents:
server {
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;
}
}No oblidis substituir el nom de domini example.com pel teu domini.
Desa i tanca el fitxer. Després, reinicia el servei Nginx per aplicar els canvis de configuració amb l'ordre següent:
# systemctl restart nginxProva la teva aplicació
L'aplicació Spring Boot ja està instal·lada i configurada amb Nginx. Hi pots accedir visitant l'URL http://ejemplo.com al teu navegador. Hauries de veure la teva aplicació a la pàgina següent:
Conclusió
Enhorabona! Has instal·lat correctament l'aplicació Spring Boot al teu servidor amb Nginx com a servidor intermediari invers. Per a més informació, pots consultar la documentació de Spring Boot.