Introduction
Developing a Java application is only half the battle. The second, equally important part is competent deployment to a production server. Many developers face difficulties when moving an application from a local machine to a VPS: environment setup, building, running in the background, and ensuring security. In this article, we will break down the complete process of deploying a Java application to a VPS (Virtual Private Server) — from purchasing the server to setting up HTTPS. All steps will be accompanied by code examples and configurations.
We will use Ubuntu 22.04 LTS as the VPS operating system, and as an example Java application — a simple Spring Boot REST application packaged as a JAR file. If you use a different framework (Jakarta EE, Micronaut, Quarkus), the general principles will remain the same.
1. VPS Preparation and Java Installation
1.1. Connecting to the Server
After purchasing a VPS, you will receive an IP address, a username (usually root), and a password or SSH key. Connect to the server via terminal:
ssh root@your-server-ipIf you are using an SSH key, specify the path to it:
ssh -i /path/to/private_key root@your-server-ip1.2. System Update and Java Installation
First, update the package manager and install the Java Development Kit (JDK). For most modern Java applications, OpenJDK 17 or 21 is sufficient:
apt update && apt upgrade -yapt install openjdk-17-jdk -yVerify the installation:
java -versionYou should see something like:
openjdk version "17.0.10" 2024-01-16 LTSOpenJDK Runtime Environment (build 17.0.10+7-Ubuntu-122.04.1)OpenJDK 64-Bit Server VM (build 17.0.10+7-Ubuntu-122.04.1, mixed mode, sharing)1.3. Environment Variables Setup (Optional)
If your application requires specific variables (e.g., JAVA_HOME), add them to the ~/.bashrc or /etc/environment file:
echo "export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64" >> ~/.bashrcsource ~/.bashrc2. Building and Uploading the Application to the Server
2.1. Building JAR/WAR on the Local Machine
Assume you have a Maven project. Perform the build:
mvn clean package -DskipTestsAs a result, a file myapp-0.0.1-SNAPSHOT.jar (or .war for web applications) will appear in the target/ folder.
2.2. Uploading the JAR File to the VPS
Use scp to copy the file to the server:
scp target/myapp-0.0.1-SNAPSHOT.jar root@your-server-ip:/opt/myapp/Create a directory for the application on the server if it doesn't exist:
mkdir -p /opt/myapp2.3. Checking the Structure on the Server
After uploading, verify that the file is in place:
ls -la /opt/myapp/You should see myapp-0.0.1-SNAPSHOT.jar.
3. Configuring Application Launch (systemd + Nginx)
3.1. Running the JAR File Manually (Test)
Before setting up persistent launch, make sure the application starts correctly:
java -jar /opt/myapp/myapp-0.0.1-SNAPSHOT.jar --server.port=8080If the application starts without errors, proceed to creating the systemd service.
3.2. Creating a systemd Service
Systemd allows you to run the Java application as a daemon (in the background) and automatically restart it if it crashes or the server reboots.
Create the service file:
nano /etc/systemd/system/myapp.serviceInsert the following configuration:
[Unit]Description=My Java ApplicationAfter=network.target
[Service]Type=simpleUser=rootWorkingDirectory=/opt/myappExecStart=/usr/bin/java -jar /opt/myapp/myapp-0.0.1-SNAPSHOT.j