BysMax
Back to course

Traccar Compilation and Deployment Guide

I will show you how to compile and deploy Traccar on a server like DigitalOcean, AWS, etc.

Emmanuel Díaz Leal Hernández

May 25, 2024
Intermediate

Quick Commands

Copy and paste these commands for instant deployment

Backend Deployment

git pull && ./gradlew assemble && sudo systemctl stop traccar && sudo cp -r ./target/* /opt/traccar/ && sudo cp -r ./schema/* /opt/traccar/schema && sudo systemctl start traccar

Frontend Deployment

git pull && npm i && npm run build && sudo cp -r ./build/* /opt/traccar/web/

Live Monitoring

tail -f /opt/traccar/logs/tracker-server.log

Environment Configuration

Before starting, verify your tools:

java -version    # Java 11+ required
node -v         # Node.js 18+ required  
npm -v          # Latest compatible version

Pro-tip: Use nvm to manage Node.js versions in production environments


Backend Compilation Process

Initial Repository Setup

git clone --recursive https://github.com/traccar/traccar.git
cd traccar

Compilation and Deployment Workflow

# Compile application
./gradlew assemble

# Controlled shutdown
sudo systemctl stop traccar

# Deploy artifacts
sudo cp -r ./target/* /opt/traccar/
sudo cp -r ./schema/* /opt/traccar/schema

# Restart service
sudo systemctl start traccar

Warning: Always stop the service before replacing JARs to avoid file locks


Frontend Compilation Process

Repository Setup

git clone https://github.com/traccar/traccar-web.git
cd traccar-web

Compilation and Deployment Pipeline

# Resolve dependencies
npm install

# Build for production
npm run build

# Hot deployment (no downtime)
sudo cp -r build/* /opt/traccar/web/

Feature: Frontend deployments do not require a service restart - just refresh your browser


Troubleshooting Arsenal

Service Diagnostics

# Deep analysis of service logs
sudo journalctl -u traccar

# Live log streaming
journalctl -u traccar -f

# Check service status
sudo systemctl status traccar

Permission Recovery

# Fix ownership issues
sudo chown -R traccar:traccar /opt/traccar/

# Verify file permissions
ls -la /opt/traccar/

# Reset permissions if necessary
sudo chmod -R 755 /opt/traccar/

Advanced Deployment Strategies

Automated Deployment Script

Build your deployment arsenal with deploy.sh:

#!/bin/bash
set -e

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'

echo -e "${YELLOW}Compiling backend...${NC}"
./gradlew assemble

echo -e "${YELLOW}Stopping Traccar service...${NC}"
sudo systemctl stop traccar

echo -e "${YELLOW}Deploying artifacts...${NC}"
sudo cp -r ./target/* /opt/traccar/
sudo cp -r ./schema/* /opt/traccar/schema

echo -e "${YELLOW}Starting Traccar service...${NC}"
sudo systemctl start traccar

echo -e "${GREEN}Deployment completed!${NC}"

# Health check with retry logic
sleep 5
RETRY_COUNT=3
for i in $(seq 1 $RETRY_COUNT); do
    if curl -f http://localhost:8082 > /dev/null 2>&1; then
        echo -e "${GREEN}Service is running correctly!${NC}"
        exit 0
    else
        echo -e "${YELLOW}Health check attempt $i/$RETRY_COUNT failed, retrying...${NC}"
        sleep 3
    fi
done

echo -e "${RED}Health check failed after $RETRY_COUNT attempts${NC}"
exit 1

Make it executable:

chmod +x deploy.sh && ./deploy.sh

Development Flow Optimization

# Live reload for frontend development
npm run dev

# Filter system logs in background
tail -f /opt/traccar/logs/tracker-server.log | grep -E "(ERROR|WARN|Exception)"

# Watch for file changes during development
find /opt/traccar -name "*.jar" | entr -r sudo systemctl restart traccar

Production Health Monitoring

# Comprehensive service check
curl -f http://localhost:8082 && echo "Service running" || echo "Service down"

# Port check
netstat -tlnp | grep :8082

# Memory usage monitoring
ps aux | grep traccar | awk '{print $2, $4, $11}' | head -1

# Disk space check for logs
df -h /opt/traccar/logs/

Rollback Strategy

# Backup current deployment
sudo cp -r /opt/traccar/target /opt/traccar/target.backup.$(date +%Y%m%d_%H%M%S)

# Quick rollback procedure
sudo systemctl stop traccar
sudo cp -r /opt/traccar/target.backup.* /opt/traccar/target/
sudo systemctl start traccar

Production Best Practices

Environment Variable Configuration

export TRACCAR_HOME=/opt/traccar
export JAVA_OPTS="-Xms1g -Xmx2g -server"

Log Rotation Configuration

# Add to /etc/logrotate.d/traccar
/opt/traccar/logs/*.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    create 644 traccar traccar
    postrotate
        systemctl reload traccar
    endscript
}

Systemd Service Hardening

# Enhanced service security
sudo systemctl edit traccar

Add override configuration:

[Service]
NoNewPrivileges=true
PrivateTmp=true
ProtectHome=true
ProtectSystem=strict
ReadWritePaths=/opt/traccar

Quick Verification Commands

System Status

# Verify all components are running
systemctl is-active traccar && echo "Service active"
curl -s http://localhost:8082 > /dev/null && echo "API responding"

Log Cleanup

# Manually clean old logs
find /opt/traccar/logs -name "*.log.*" -mtime +30 -delete

# Check current log size
du -sh /opt/traccar/logs/

Performance Monitoring

# CPU and memory for Traccar process
top -p $(pgrep -f traccar)

# Active network connections
ss -tuln | grep :8082

Comentarios (0)