Key words first
- Java is a programming language.
- Spring Boot is a toolkit that helps people build Java web apps quickly.
- Standalone means the app carries its own small web server inside one file called a JAR. You do not need Tomcat installed on the side.
- Cluster means running several copies of the app, so work is shared and one failure does not stop the site.
Run one standalone app
- Install Java on the server. Use the Java version your app was built for.
- Upload your JAR file with SFTP. Put it in a folder such as
/opt/myapp. - Test it.
This starts the app. By default it listens on port 8080.java -jar /opt/myapp/myapp.jar - Press Ctrl + C to stop it when the test is done.
Keep it running with systemd
systemd is the part of Linux that starts and restarts services.
- Create the file
/etc/systemd/system/myapp.servicewith this text:
This tells Linux how to run the app and to restart it if it stops.[Unit] Description=My Spring Boot app After=network.target [Service] User=appuser ExecStart=/usr/bin/java -jar /opt/myapp/myapp.jar Restart=always [Install] WantedBy=multi-user.target - Load and start it.
This turns it on now and at every boot.systemctl daemon-reload systemctl enable --now myapp
Build a cluster
- Run the same JAR on different ports, for example 8081 and 8082. Start each with
--server.port=8081and so on.
This sets the port for that copy.java -jar myapp.jar --server.port=8081 - Put a load balancer in front. Nginx or Apache can do this job. A load balancer sends each visitor to a free copy.
- Do not store visitor sessions inside one copy. Use a shared store, such as a database or Redis, so any copy can serve any visitor.
- For several servers, run the same setup on each and point the load balancer at all of them.
Warning: Take a backup of your app and data before upgrading or moving it.
Tip: Run the app as a normal user, not root. Close its ports to the outside and let only the load balancer reach them.
Quick recap
- A standalone app is one JAR that runs by itself.
- Use systemd to start it and keep it alive.
- A cluster is many copies behind a load balancer.
- Share sessions so any copy can serve anyone.