Deploying a Java Application on VPS: A Step-by-Step Guide from A to Z
Developing a Java application is only half the battle. The second, equally important part is deploying it on a server. Many developers face difficulties when transferring code from a local machine to a remote VPS. In this article, we will break down the complete process of deploying a Java application (using Spring Boot as an example) on a virtual private server (VPS) running Ubuntu 22.04. You will learn how to set up the environment, build the project, run it as a service, and provide secure access via Nginx and SSL.
1. Preparing the VPS and Installing Necessary Software
Before uploading the code, you need to prepare the server. We will use Ubuntu 22.04 LTS. Connect to the server via SSH:
ssh root@your-ip-addressUpdate the package manager and install basic utilities:
apt update && apt upgrade -yapt install curl git -yNow install OpenJDK 17 (the recommended version for most modern projects):
apt install openjdk-17-jdk -yVerify the installation:
java -versionYou should see something like:
openjdk version "17.0.9" 2023-10-17Next, install Maven to build the project (if you use Gradle, replace the commands accordingly):
apt install maven -ymvn -versionThis completes the basic environment setup.
2. Building and Uploading the Java Application to the Server
Build your Java application locally. Navigate to the project root folder and run:
mvn clean package -DskipTestsAfter a successful build, a JAR file will appear in the target folder (e.g., myapp-0.0.1-SNAPSHOT.jar). Now you need to upload it to the server. Use scp (run from your local machine):
scp target/myapp-0.0.1-SNAPSHOT.jar root@your-ip-address:/opt/myapp/On the server, create a directory for the application and move the file:
mkdir -p /opt/myappmv myapp-0.0.1-SNAPSHOT.jar /opt/myapp/myapp.jarVerify that the JAR file is in place and run it manually for testing:
cd /opt/myappjava -jar myapp.jarIf the application starts without errors, proceed to the next step.
3. Configuring systemd for Automatic Startup
To make the application start automatically on system boot and restart in case of a failure, create a systemd service.
Create the service file:
nano /etc/systemd/system/myapp.serviceInsert the following content (replace youruser with the username under which the application will run, or leave it as root):
[Unit]Description=My Java Spring Boot ApplicationAfter=network.target
[Service]User=rootWorkingDirectory=/opt/myappExecStart=/usr/bin/java -jar /opt/myapp/myapp.jarSuccessExitStatus=143Restart=alwaysRestartSec=10
[Install]WantedBy=multi-user.targetExplanation of parameters:
After=network.target— the service starts after the network is up.Restart=always— automatic restart on failure.RestartSec=10— a 10-second pause before restarting.SuccessExitStatus=143— correct handling of the stop signal.
Now enable and start the service:
systemctl daemon-reloadsystemctl enable myappsystemctl start myappCheck the status:
systemctl status myappYou should see Active: active (running). Your application will now start