Deploying a Java Application on VPS: A Step-by-Step Guide from A to Z

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Deploying a Java Application on VPS: A Step-by-Step Guide from A to Z

\n\n

Developing a Java application is only half the battle. The second, equally important part is deploying it on a production server. In this guide, we will go through the full deployment cycle of a Java (Spring Boot) application on a Virtual Private Server (VPS) running Ubuntu 22.04. You will learn how to set up the server, build the project, run it as a system service, and provide secure access through a web server.

\n\n

1. Preparing the VPS and Installing Required Software

\n\n

The first step is to connect to your VPS via SSH and install the Java Runtime Environment (JRE) and Git.

\n\n

Connecting to the Server

\n
ssh root@your-server-ip
\n\n

Updating Packages and Installing Java (OpenJDK 17)

\n

For Spring Boot 3.x, Java 17 is recommended. Let's install OpenJDK.

\n
apt update && apt upgrade -y\napt install openjdk-17-jdk git -y\njava -version
\n

You should see information about the Java 17 version.

\n\n

Installing Nginx (for reverse proxy)

\n
apt install nginx -y\nsystemctl start nginx\nsystemctl enable nginx
\n\n

2. Building the Java Application (Maven/Gradle)

\n\n

On your local machine (or directly on the server if you clone the repository), you need to build the JAR file. Let's consider both options.

\n\n

Option A: Local Build and Copy to Server

\n

Assume you have a Spring Boot project with Maven.

\n
# On the local machine\nmvn clean package -DskipTests\n# The file will appear in target/my-app-0.0.1-SNAPSHOT.jar
\n

Then copy it to the server via SCP:

\n
scp target/my-app-0.0.1-SNAPSHOT.jar root@your-ip:/opt/myapp/
\n\n

Option B: Building Directly on the Server

\n

Clone the repository and build the project:

\n
cd /opt\ngit clone https://github.com/your-username/your-repository.git myapp\ncd myapp\n./mvnw clean package -DskipTests\n# Or, if Maven is installed globally:\nmvn clean package -DskipTests
\n\n

3. Configuring Production Configuration and Creating a systemd Service

\n\n

To ensure the application starts automatically on system boot and restarts in case of failure, we will create a systemd unit file.

\n\n

Creating a Startup Script

\n

Create a directory for the application (if you haven't already):

\n
mkdir -p /opt/myapp\n# Move the JAR file to this directory if it is not already there
\n\n

Creating a systemd Service

\n

Create the file /etc/systemd/system/myapp.service:

\n
nano /etc/systemd/system/myapp.service
\n

Insert the following content (replace paths and parameters for your project):

\n
[Unit]\nDescription=My Java Spring Boot Application\nAfter=syslog.target network.target\n\n[Service]\nUser=root\nWorkingDirectory=/opt/myapp\nExecStart=/usr/bin/java -Xms256m -Xmx512m -jar /opt/myapp/my-app-0.0.1-SNAPSHOT.jar --server.port=8080\nSuccessExitStatus=143\nRestart=always\nRestartSec=10\n\n[Install]\nWantedBy=multi-user.target
\n

Explanation of parameters:

\n
    \n
  • -Xms256m -Xmx512m — initial and maximum Java heap size.
  • \n
  • --server.port=8080 — the port on which the application will run.
  • \n
  • Restart=always — automatic restart on failure.
  • \n
\n\n

Starting and Checking the Service

\n
systemctl daemon-reload\nsystemctl start myapp\nsystemctl status myapp\nsystemctl enable myapp  # Enable auto-start on boot
\n

Check that the application responds locally:

\n
curl http://localhost:8080/actuator/health
\n

You should receive a JSON response with the status UP.

\n\n

4. Setting Up a Reverse Proxy via Nginx

\n\n

Direct access to the application via port 8080 is not secure. Let's configure Nginx as a proxy server that will accept requests on

Blogs

Book Recommendations