Deploying a Kotlin Application on VPS: Step by Step from A to Z
You've written a cool application in Kotlin (Ktor, Spring Boot, or just a console service), and now you need to run it on a real server. In this article, we will cover the full deployment cycle of a Kotlin application on a VPS: from purchasing a server and setting up SSH to launching via systemd and connecting a domain with HTTPS. All examples are working and tested on Ubuntu 22.04.
1. VPS Preparation and Connection
First, rent a VPS (for example, from Timeweb, Selectel, or DigitalOcean). Minimum requirements: 1 vCPU, 1 GB RAM, 20 GB SSD. Operating system — Ubuntu 22.04 LTS.
After receiving the IP address and root password, connect via SSH:
ssh root@your_server_ipImmediately update the system and create an unprivileged user (working as root is bad practice):
apt update && apt upgrade -yadduser kotlinuserusermod -aG sudo kotlinusersu - kotlinuserSet up SSH keys for convenient and secure login:
ssh-keygen -t ed25519 -C "your_email@example.com"ssh-copy-id kotlinuser@your_server_ipNow disable password login in /etc/ssh/sshd_config (the line PasswordAuthentication no) and restart SSH: sudo systemctl restart sshd.
2. Installing Java (JRE/JDK) and Building the Application
Kotlin applications run on the JVM. Install OpenJDK 17 or 21 (recommended for modern projects):
sudo apt install openjdk-17-jdk -yjava -versionNow, on your local computer (where development is done), build the application into a JAR file. For Gradle (Ktor or Spring Boot):
./gradlew clean buildThe finished JAR file will be in build/libs/. For example, myapp-1.0.0.jar. Upload it to the server:
scp build/libs/myapp-1.0.0.jar kotlinuser@your_server_ip:~/app/Create the folder structure on the server:
mkdir -p ~/app/logs ~/app/configPlace configuration files (application.conf, application.yml) in ~/app/config/. Check that the application starts manually:
cd ~/appjava -jar myapp-1.0.0.jar --spring.config.location=file:./config/If everything works, move on to automatic startup.
3. Setting up systemd for Autostart
To make the application automatically start after a server reboot and not crash on errors, we will create a systemd service.
Create the file /etc/systemd/system/kotlin-app.service:
sudo nano /etc/systemd/system/kotlin-app.servicePaste the following content (replace the paths with your own):
[Unit]Description=Kotlin Application ServiceAfter=network.target
[Service]Type=simpleUser=kotlinuserWorkingDirectory=/home/kotlinuser/appExecStart=/usr/bin/java -jar /home/kotlinuser/app/myapp-1.0.0.jar --spring.config.location=file:/home/kotlinuser/app/config/Restart=on-failureRestartSec=10StandardOutput=append:/home/kotlinuser/app/logs/app.logStandardError=append:/home/kotlinuser/app/logs/error.log
[Install]WantedBy=multi-user.targetExplanation of key parameters:
- User — who to run it as (not root);
- ExecStart — the full launch command with the config specified;
- Restart=on-failure — automatic restart on crash;
- StandardOutput/Error — logging to files.
Activate the service:
sudo systemctl daemon-reloadsudo systemctl enable kotlin-app.servicesudo systemctl start kotlin-app.serviceCheck the status:
sudo systemctl status kotlin-app.service