Deploying a Kotlin 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

Deploying a Kotlin Application on VPS: A Complete Step-by-Step Guide



Developing in Kotlin is only half the battle. Sooner or later, the question arises: how to move an application from a local environment to a real server? In this article, we will thoroughly examine deploying a Kotlin application (using Spring Boot as an example) on a VPS. You will learn how to prepare the server, build a JAR file, configure auto-start, and ensure a secure connection via HTTPS. This guide is suitable for both beginners and developers who want to systematize their knowledge.



1. Preparing the VPS and Installing Necessary Software



Before uploading the code, you need to prepare the server. We recommend using Ubuntu 22.04 LTS or newer. Connect to the server via SSH:



ssh root@your-server-ip


Update the package database and install Java (Kotlin compiles to JVM bytecode, so we need JDK 17 or 21):



sudo apt update && sudo apt upgrade -ysudo apt install openjdk-17-jdk -yjava -version


Make sure the Java version matches your build. Also install Git (if using a repository) and utilities for working with archives:



sudo apt install git unzip curl -y


To work with a Kotlin project on the server, it is not necessary to install the Kotlin compiler itself — we will perform the build locally or via CI/CD. However, if you plan to compile on the server, add SDKMAN! and Kotlin:



curl -s "https://get.sdkman.io" | bashsource "$HOME/.sdkman/bin/sdkman-init.sh"sdk install kotlin


Important: At this stage, also configure the firewall (UFW) for basic security:



sudo ufw allow OpenSSHsudo ufw allow 8080/tcp  # application portsudo ufw enable


2. Building the Kotlin Application and Transferring it to the Server



Consider a typical Spring Boot project with Kotlin. Build the JAR file locally (or on the server if you cloned the repository). In the project root, run:



./gradlew clean bootJar


Or, if using Maven:



mvn clean package


After the build, a file like app-0.0.1-SNAPSHOT.jar will appear in the build/libs/ (Gradle) or target/ (Maven) folder. Copy it to the server using SCP:



scp build/libs/app-0.0.1-SNAPSHOT.jar root@your-ip:/opt/myapp/app.jar


Create a directory for the application on the server and move the JAR:



sudo mkdir -p /opt/myappsudo mv app.jar /opt/myapp/


Check that the JAR starts manually:



cd /opt/myappjava -jar app.jar --server.port=8080


If the application starts and responds on port 8080, everything is fine. Stop it (Ctrl+C) and proceed to configure persistent startup.



3. Configuring systemd for Auto-Start and Monitoring



To make the application automatically start when the server reboots and recover after crashes, we will create a systemd service. Create a file:



sudo nano /etc/systemd/system/kotlin-app.service


Insert the following template (replace the parameters for your application):



[Unit]Description=Kotlin Spring Boot ApplicationAfter=network.target

[Service]Type=simpleUser=rootWorkingDirectory=/opt/myappExecStart=/usr/bin/java -jar /opt/myapp/app.jar --server.port=8080Restart=on-failureRestartSec=10StandardOutput=journalStandardError=journal

[Install]WantedBy=multi-user.target


Reload the systemd configuration and start the service:



sudo systemctl daemon-reloadsudo systemctl start kotlin-appsudo systemctl status kotlin-app


If the status is active (running), everything is working. Add the service to auto-start:



sudo systemctl enable kotlin-app

Blogs

Book Recommendations