Deploying Java Application on VPS: Step-by-Step Guide

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

Introduction: Why VPS is the Ideal Choice for Java Applications

Deploying a Java application on a Virtual Private Server (VPS) is the gold standard for developers who want full control over the runtime environment. Unlike shared hosting, a VPS provides dedicated resources (CPU, RAM, disk), allows you to configure the Java Virtual Machine (JVM), install any libraries, and configure the server for your project's specific needs.

In this article, we will cover the complete deployment cycle: from preparing the VPS and installing the JDK to running the application in production mode using systemd. All steps are accompanied by code examples and commands for Linux (Ubuntu 22.04 LTS).



1. Preparing the VPS and Installing Required Software

1.1. Connecting to the Server

After purchasing a VPS, you will receive an IP address and root password. Connect via SSH:

ssh root@your_server_IP

It is recommended to immediately create an unprivileged user for daily work:

adduser deployerusermod -aG sudo deployer


1.2. Installing Java (JDK 17 LTS)

OpenJDK 17 is best suited for modern Java applications. Run:

sudo apt updatesudo apt install openjdk-17-jdk -y

Verify the installation:

java -version# Output: openjdk version "17.0.10" 2024-01-16 LTS


1.3. Installing Git and Maven/Gradle

To build the project from the repository:

sudo apt install git maven -y# Or for Gradle:# sudo apt install gradle -y


2. Building and Preparing the Application

2.1. Cloning the Repository

Assume your Spring Boot application is on GitHub:

cd /optsudo git clone https://github.com/your_account/my-java-app.gitcd my-java-app


2.2. Building the JAR File

For Maven:

mvn clean package -DskipTests

As a result, the file my-app-0.0.1-SNAPSHOT.jar will appear in target/. Make sure it runs:

java -jar target/my-app-0.0.1-SNAPSHOT.jar --server.port=8080

Check that it works: open http://your_IP:8080 in your browser.



2.3. Optimizing the JAR for Production

Add production parameters to application.properties:

server.port=8080spring.datasource.url=jdbc:postgresql://localhost:5432/mydbspring.datasource.username=app_userspring.datasource.password=secure_passwordspring.jpa.hibernate.ddl-auto=validate

Never store real passwords in code — use environment variables or Vault.



3. Configuring the Database and Environment

3.1. Installing PostgreSQL

sudo apt install postgresql postgresql-contrib -ysudo systemctl start postgresqlsudo systemctl enable postgresql

Create the database and user:

sudo -u postgres psqlCREATE DATABASE mydb;CREATE USER app_user WITH PASSWORD 'secure_password';GRANT ALL PRIVILEGES ON DATABASE mydb TO app_user;\\q


3.2. Configuring Environment Variables

Create the file /etc/environment.d/java-app.conf:

DB_URL=jdbc:postgresql://localhost:5432/mydbDB_USER=app_userDB_PASSWORD=secure_passwordJAVA_OPTS="-Xms256m -Xmx512m"

Reload the environment:

source /etc/environment.d/java-app.conf


4. Running the Application as a systemd Service

4.1. Creating the Unit File

Create the file /etc/systemd/system/java-app.service:

[Unit]Description=Java Spring Boot ApplicationAfter=network.target postgresql.service

[Service]User=deployerWorkingDirectory=/opt/my-java-appExecStart=/usr/bin/java $JAVA_OPTS -jar /opt/my-java-app/target/my-app-0.0.1-SNAPSHOT.jarSuccessExitStatus=143Restart=alwaysRest

Blogs

Book Recommendations