Deploying a Java Application on VPS: A Step-by-Step Guide from A to Z
\n\nDeveloping a Java application is only half the battle. The second, equally important part is deploying it on a production server. In this guide, we will go through the full deployment cycle of a Java (Spring Boot) application on a Virtual Private Server (VPS) running Ubuntu 22.04. You will learn how to set up the server, build the project, run it as a system service, and provide secure access through a web server.
\n\n1. Preparing the VPS and Installing Required Software
\n\nThe first step is to connect to your VPS via SSH and install the Java Runtime Environment (JRE) and Git.
\n\nConnecting to the Server
\nssh root@your-server-ip\n\nUpdating Packages and Installing Java (OpenJDK 17)
\nFor Spring Boot 3.x, Java 17 is recommended. Let's install OpenJDK.
\napt update && apt upgrade -y\napt install openjdk-17-jdk git -y\njava -version\nYou should see information about the Java 17 version.
\n\nInstalling Nginx (for reverse proxy)
\napt install nginx -y\nsystemctl start nginx\nsystemctl enable nginx\n\n2. Building the Java Application (Maven/Gradle)
\n\nOn your local machine (or directly on the server if you clone the repository), you need to build the JAR file. Let's consider both options.
\n\nOption A: Local Build and Copy to Server
\nAssume you have a Spring Boot project with Maven.
\n# On the local machine\nmvn clean package -DskipTests\n# The file will appear in target/my-app-0.0.1-SNAPSHOT.jar\nThen copy it to the server via SCP:
\nscp target/my-app-0.0.1-SNAPSHOT.jar root@your-ip:/opt/myapp/\n\nOption B: Building Directly on the Server
\nClone the repository and build the project:
\ncd /opt\ngit clone https://github.com/your-username/your-repository.git myapp\ncd myapp\n./mvnw clean package -DskipTests\n# Or, if Maven is installed globally:\nmvn clean package -DskipTests\n\n3. Configuring Production Configuration and Creating a systemd Service
\n\nTo ensure the application starts automatically on system boot and restarts in case of failure, we will create a systemd unit file.
\n\nCreating a Startup Script
\nCreate a directory for the application (if you haven't already):
\nmkdir -p /opt/myapp\n# Move the JAR file to this directory if it is not already there\n\nCreating a systemd Service
\nCreate the file /etc/systemd/system/myapp.service:
nano /etc/systemd/system/myapp.service\nInsert the following content (replace paths and parameters for your project):
\n[Unit]\nDescription=My Java Spring Boot Application\nAfter=syslog.target network.target\n\n[Service]\nUser=root\nWorkingDirectory=/opt/myapp\nExecStart=/usr/bin/java -Xms256m -Xmx512m -jar /opt/myapp/my-app-0.0.1-SNAPSHOT.jar --server.port=8080\nSuccessExitStatus=143\nRestart=always\nRestartSec=10\n\n[Install]\nWantedBy=multi-user.target\nExplanation of parameters:
\n- \n
-Xms256m -Xmx512m— initial and maximum Java heap size. \n --server.port=8080— the port on which the application will run. \n Restart=always— automatic restart on failure. \n
Starting and Checking the Service
\nsystemctl daemon-reload\nsystemctl start myapp\nsystemctl status myapp\nsystemctl enable myapp # Enable auto-start on boot\nCheck that the application responds locally:
\ncurl http://localhost:8080/actuator/health\nYou should receive a JSON response with the status UP.
4. Setting Up a Reverse Proxy via Nginx
\n\nDirect access to the application via port 8080 is not secure. Let's configure Nginx as a proxy server that will accept requests on