Deploy Spring Boot Application to Lightsail Server

AWS Lightsail Deployment Course

2026-04-08

Step 4: Deploy Spring Boot Application

Deploy your Spring Boot application to AWS Lightsail server

  • Install Java Runtime Environment
  • Transfer application files
  • Configure production environment
  • Set up system service
  • Monitor and troubleshoot

Understanding the Deployment Process

Key Components

  • Java Runtime Environment (JRE)
  • Compiled JAR file transfer
  • Production environment variables
  • System service configuration

Best Practice

Always test your application locally with production profiles before deploying

Installing Java on Lightsail

Connect and Install OpenJDK 17

# SSH to your instance
ssh -i your-key.pem ubuntu@your-lightsail-ip

# Update and install Java
sudo apt update
sudo apt install openjdk-17-jdk -y

# Verify installation
java -version

Preparing Application Structure

Create Directory Structure

# Create application directory
sudo mkdir -p /opt/myapp

# Create logs directory
sudo mkdir -p /var/log/myapp

# Create application user
sudo useradd -r -s /bin/false myapp

# Set ownership
sudo chown -R myapp:myapp /opt/myapp /var/log/myapp

Building and Transferring Application

Build JAR File Locally

# Using Maven
./mvnw clean package -DskipTests

# Using Gradle
./gradlew build -x test

Transfer to Server

# Transfer JAR file
scp -i your-key.pem target/myapp-0.0.1-SNAPSHOT.jar \
  ubuntu@your-lightsail-ip:/home/ubuntu/

# Move to application directory
sudo mv /home/ubuntu/myapp-0.0.1-SNAPSHOT.jar /opt/myapp/myapp.jar

Configuring Production Environment

Create Environment File

# Create environment configuration
sudo nano /opt/myapp/application.env

Environment Variables

  • SPRING_PROFILES_ACTIVE=prod
  • Database connection settings
  • Server configuration
  • JVM memory options
  • Logging levels

Environment Configuration Example

# /opt/myapp/application.env
SPRING_PROFILES_ACTIVE=prod

# Database Configuration
DB_HOST=your-lightsail-db-endpoint
DB_PORT=3306
DB_NAME=myapp_prod
DB_USERNAME=myappuser
DB_PASSWORD=your-secure-password

# JVM Options
JAVA_OPTS=-Xms512m -Xmx1024m -Dspring.profiles.active=prod

Set Secure Permissions

sudo chown myapp:myapp /opt/myapp/application.env
sudo chmod 600 /opt/myapp/application.env

Creating System Service

Systemd Service File

sudo nano /etc/systemd/system/myapp.service
[Unit]
Description=My Spring Boot Application
After=network.target

[Service]
Type=exec
User=myapp
Group=myapp
ExecStart=/usr/bin/java -jar /opt/myapp/myapp.jar
EnvironmentFile=/opt/myapp/application.env
WorkingDirectory=/opt/myapp
Restart=always

Enable and Start Service

Service Management Commands

# Reload systemd configuration
sudo systemctl daemon-reload

# Enable service to start on boot
sudo systemctl enable myapp.service

# Start the service
sudo systemctl start myapp.service

# Check service status
sudo systemctl status myapp.service

Production Application Properties

Database Configuration

# application-prod.properties
spring.datasource.url=jdbc:mysql://${DB_HOST}:${DB_PORT}/${DB_NAME}
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}

# Production optimizations
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=false

# Logging
logging.file.name=/var/log/myapp/application.log

Testing Your Deployment

Verification Steps

  • Check service status: sudo systemctl status myapp.service
  • View logs: sudo tail -f /var/log/myapp/application.log
  • Test health endpoint: curl http://localhost:8080/actuator/health
  • Verify port listening: sudo netstat -tlnp | grep :8080

Configure Lightsail Firewall

Open port 8080 in Lightsail console → Networking tab

Monitoring and Log Management

Log Rotation Configuration

# /etc/logrotate.d/myapp
/var/log/myapp/*.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    create 0644 myapp myapp
}

Essential Monitoring Commands

  • sudo systemctl status myapp.service
  • sudo journalctl -u myapp.service -f
  • free -h (memory usage)
  • df -h (disk usage)

Troubleshooting Common Issues

Application Won’t Start

  • Check Java installation
  • Verify JAR permissions
  • Review systemd logs
  • Check environment file syntax

Database Connection Issues

  • Test database connectivity
  • Verify connection strings
  • Check firewall rules
  • Validate credentials

Memory Issues

  • Monitor usage with free -h
  • Adjust JVM heap settings
  • Consider instance upgrade

Summary: What You’ve Accomplished

Successfully Deployed ✅

  • Installed Java 17 on Lightsail server
  • Created secure directory structure
  • Transferred application JAR file
  • Configured production environment variables
  • Set up systemd service for automatic startup
  • Implemented logging with rotation
  • Configured basic monitoring tools

Next Step Preview

Configure Nginx reverse proxy for HTTP/HTTPS traffic on standard ports