Full Java JAR Deployment Guide

Build JAR from IntelliJ

Package your application into a runnable .jar file.

# Step 1: Open project in IntelliJ IDEA
# Step 2: Go to File → Project Structure → Artifacts
# Step 3: Click "+" → JAR → From modules with dependencies

# Select main class (your SpringBoot main or main method)
# Choose "Extract to target JAR"

# Step 4: Build JAR
Build → Build Artifacts → Build

# Output location
out/artifacts/yourapp.jar

Update Server

# Update package list
sudo apt update

# Upgrade system
sudo apt upgrade -y

Install Java (OpenJDK)

# Install Java (recommended version 17)
sudo apt install openjdk-17-jdk -y

# Verify installation
java -version

# Check Java path
which java

Upload Your JAR

# Copy JAR to server
scp yourapp.jar user@server:/home/user/app/

# Move into directory
cd /home/user/app

Test Run Application

# Run app normally
java -jar yourapp.jar

# Run on custom port
java -jar yourapp.jar --server.port=8080

Install MySQL

# Install MySQL server
sudo apt install mysql-server -y

# Secure installation
sudo mysql_secure_installation

Create Database

# Open MySQL
sudo mysql

# Create database
CREATE DATABASE myapp;

# Create user
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'StrongPassword';

# Grant privileges
GRANT ALL PRIVILEGES ON myapp.* TO 'myuser'@'localhost';

# Apply changes
FLUSH PRIVILEGES;
EXIT;

Create Systemd Service

# Create service file
sudo nano /etc/systemd/system/myapp.service

[Unit]
Description=Java App
After=network.target

[Service]
User=ubuntu
WorkingDirectory=/home/ubuntu/app
ExecStart=/usr/bin/java -jar /home/ubuntu/app/yourapp.jar
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Start Service

# Reload systemd
sudo systemctl daemon-reload

# Enable service on boot
sudo systemctl enable myapp

# Start service
sudo systemctl start myapp

# Check status
sudo systemctl status myapp

Install Apache

# Install Apache
sudo apt install apache2 -y

# Enable proxy modules
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod rewrite

Reverse Proxy (Port 80 → 8080)

# Create virtual host
sudo nano /etc/apache2/sites-available/myapp.conf

<VirtualHost *:80>
ServerName yourdomain.com

# Forward traffic to Java app
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/

</VirtualHost>
# Enable site
sudo a2ensite myapp.conf

# Restart Apache
sudo systemctl restart apache2

Why Use Nginx Instead?

:contentReference[oaicite:4]{index=4} is often preferred over :contentReference[oaicite:5]{index=5} because:

  • Better performance under high traffic
  • Handles many concurrent connections efficiently
  • Lower memory usage
  • Very fast as reverse proxy

Apache is easier for beginners, but Nginx is better for scaling.

Firewall Setup

# Allow SSH
sudo ufw allow 22

# Allow HTTP/HTTPS
sudo ufw allow 80
sudo ufw allow 443

# Enable firewall
sudo ufw enable

Check Logs

# App logs
journalctl -u myapp -f

# Apache logs
tail -f /var/log/apache2/error.log

# MySQL logs
tail -f /var/log/mysql/error.log