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 Step-by-Step Guide

Developing in Kotlin is only half the battle. To make your application available to users, it needs to be deployed on a server. In this guide, we will walk through the process of deploying a Kotlin (JVM) application on a VPS (Virtual Private Server). You will learn how to prepare the server, build the application, configure auto-start, and ensure a secure HTTPS connection. This article is aimed at developers familiar with the basics of Kotlin and Linux.



1. Server and Environment Preparation

Before uploading the code, you need to prepare the VPS. We will use Ubuntu 22.04 LTS. Connect to the server via SSH:

ssh root@your-server-ip

Update packages and install the necessary components:

sudo apt update && sudo apt upgrade -ysudo apt install openjdk-17-jdk git nginx certbot python3-certbot-nginx -y

Check the Java version:

java -version

Now, let's create a user for the application (running processes as root is not safe):

sudo useradd -m -s /bin/bash kotlinappsudo usermod -aG sudo kotlinappsu - kotlinapp

Make sure the user can run Java.



2. Building and Uploading the Kotlin Application

Let's consider two popular frameworks: Ktor and Spring Boot. Both are built into an executable JAR file. Example for Ktor:

// build.gradle.kts (fragment)plugins {    kotlin("jvm") version "1.9.22"    application    id("io.ktor.plugin") version "2.3.7"}

application { mainClass.set("com.example.ApplicationKt")}

ktor { fatJar { archiveFileName.set("myapp.jar") }}

Build the fat JAR locally:

./gradlew buildFatJar

Upload the JAR to the server via SCP (run on your local machine):

scp build/libs/myapp.jar kotlinapp@your-ip:/home/kotlinapp/app/

On the server, create the directory structure:

mkdir -p /home/kotlinapp/app/logs


3. Configuring systemd for Auto-Start

To ensure the application starts automatically when the server boots and restarts on failure, we will create a systemd service. Create the file /etc/systemd/system/kotlinapp.service:

sudo nano /etc/systemd/system/kotlinapp.service

Insert the following configuration:

[Unit]Description=Kotlin Application ServiceAfter=network.target

[Service]User=kotlinappGroup=kotlinappWorkingDirectory=/home/kotlinapp/appExecStart=/usr/bin/java -jar myapp.jarRestart=alwaysRestartSec=10StandardOutput=append:/home/kotlinapp/app/logs/out.logStandardError=append:/home/kotlinapp/app/logs/err.logEnvironment="PORT=8080"

[Install]WantedBy=multi-user.target

Enable and start the service:

sudo systemctl daemon-reloadsudo systemctl enable kotlinappsudo systemctl start kotlinapp

Check the status:

sudo systemctl status kotlinapp

You can view logs with the command:

journalctl -u kotlinapp -f


4. Configuring Nginx and HTTPS (SSL)

The Nginx proxy server will forward requests from ports 80/443 to port 8080 of your Kotlin application. Create the configuration:

sudo nano /etc/nginx/sites-available/kotlinapp

Example configuration:

server {    listen 80;    server_name yourdomain.com www.yourdomain.com;

location / { proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; }}

Activate the site and obtain an SSL certificate:

sudo ln -s /etc/ngin                

Blogs

Book Recommendations