Tutorial 12 min read

How to Deploy a Trading Bot on a VPS (24/7 Automated)

Step-by-step guide to deploying your Python trading bot on a VPS with systemd, monitoring, logging, and automatic restarts for 24/7 operation.

9Sections
6Code samples
12Min read
TutorialCategory

Why a VPS?

Running a trading bot on your laptop means it stops when you close the lid, when your WiFi drops, or when Windows decides to update. A VPS (Virtual Private Server) runs 24/7 in a data center with redundant power and network. A basic VPS costs $5-10/month and is the minimum viable infrastructure for automated trading.

99.9%

Uptime guarantee

$5/mo

Starting cost

24/7

Always-on monitoring

Step 1: Server Setup

Shell Step 1: Server Setup
# SSH into your VPS
ssh root@your-server-ip

# Create a dedicated user (never run trading bots as root)
adduser trader
usermod -aG sudo trader
su - trader

# Update system
sudo apt update && sudo apt upgrade -y

# Install Python and dependencies
sudo apt install python3 python3-pip python3-venv -y

# Create project directory
mkdir -p ~/trading-bot
cd ~/trading-bot

# Create virtual environment
python3 -m venv venv
source venv/bin/activate

# Install dependencies
pip install requests python-dotenv

Step 2: Deploy Your Bot

Python Step 2: Deploy Your Bot
# Upload your bot code (from your local machine)
scp -r ./bot/* trader@your-server-ip:~/trading-bot/

# Or use git
cd ~/trading-bot
git clone https://github.com/your-repo/trading-bot.git .

# Create a .env file for secrets (never commit this)
cat > .env << 'EOF'
CLAW_API_KEY=your_tickatlas_api_key
TELEGRAM_TOKEN=your_bot_token
TELEGRAM_CHAT_ID=your_chat_id
EOF

chmod 600 .env  # Only owner can read

Step 3: Create a Systemd Service

Systemd keeps your bot running, automatically restarts it after crashes, and starts it on boot.

Shell Step 3: Create a Systemd Service
sudo tee /etc/systemd/system/trading-bot.service > /dev/null << 'EOF'
[Unit]
Description=Trading Bot
After=network.target
Wants=network-online.target

[Service]
Type=simple
User=trader
WorkingDirectory=/home/trader/trading-bot
ExecStart=/home/trader/trading-bot/venv/bin/python bot.py
Restart=always
RestartSec=30
StandardOutput=journal
StandardError=journal
EnvironmentFile=/home/trader/trading-bot/.env

# Security hardening
NoNewPrivileges=true
ProtectSystem=strict
ReadWritePaths=/home/trader/trading-bot/logs

[Install]
WantedBy=multi-user.target
EOF

# Enable and start
sudo systemctl daemon-reload
sudo systemctl enable trading-bot
sudo systemctl start trading-bot

# Check status
sudo systemctl status trading-bot

Step 4: Logging

Python Step 4: Logging
# In your bot code, configure file-based logging:
import logging
from logging.handlers import RotatingFileHandler

def setup_logging():
    logger = logging.getLogger("bot")
    logger.setLevel(logging.INFO)

    # Rotate logs at 10MB, keep 5 backups
    handler = RotatingFileHandler(
        "logs/bot.log", maxBytes=10_000_000, backupCount=5
    )
    handler.setFormatter(logging.Formatter(
        "%(asctime)s %(levelname)s %(message)s"
    ))
    logger.addHandler(handler)

    # Also log to stdout for journalctl
    logger.addHandler(logging.StreamHandler())
    return logger

# View logs in real-time
# journalctl -u trading-bot -f
# tail -f ~/trading-bot/logs/bot.log

Step 5: Health Monitoring

Shell Step 5: Health Monitoring
# Add a health check cron job that alerts you if the bot stops
# Create /home/trader/check_bot.sh

#!/bin/bash
if ! systemctl is-active --quiet trading-bot; then
    curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage" \
        -d "chat_id=$TELEGRAM_CHAT_ID" \
        -d "text=ALERT: Trading bot is DOWN on $(hostname)"
fi

# Add to crontab
crontab -e
# Add: */5 * * * * /home/trader/check_bot.sh

Step 6: Updates and Deployment

Shell Step 6: Updates and Deployment
# Deploy updates safely:
cd ~/trading-bot
git pull origin main
source venv/bin/activate
pip install -r requirements.txt

# Restart the bot
sudo systemctl restart trading-bot

# Verify it started correctly
sudo systemctl status trading-bot
journalctl -u trading-bot --since "1 minute ago"

Security Checklist

Never run as root

Create a dedicated user with minimal permissions. The bot only needs read access to its code and write access to its log directory.

Protect your API keys

Store keys in a .env file with 600 permissions. Never hardcode them in your bot code or commit them to git.

Enable SSH key auth, disable password auth

SSH keys are significantly more secure than passwords. Disable password authentication in sshd_config after setting up key-based access.

Set up a firewall

Use ufw to allow only SSH (port 22) and deny everything else. Your bot makes outbound API calls -- it does not need any inbound ports open.

Related Reading

Run this against live data.

Every account starts pay-as-you-go with $2.50 of credit and no card. Paste the key into the samples above and the requests work unchanged.

Prefer the API to the article

Build it instead of reading about it.

Create a key, run the first request, then extend one layer at a time. Every account starts pay-as-you-go with $2.50 of credit — no card required.