1
Fork 0
crypto_bot_training/Session_06/Notebooks/hetzner_setup.ipynb
2025-06-28 16:08:28 +02:00

785 lines
17 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Hetzner VPS Setup Guide - Complete Instructions\n",
"\n",
"## Session 6: Live Trading & Deployment\n",
"\n",
"**Today's Objectives:**\n",
"- Secure API key management & best practices\n",
"- Choose the right deployment platform\n",
"- Set up automated execution on Hetzner Cloud"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Create Account & Choose Server\n",
"\n",
"### 1.1 Create Hetzner Account\n",
"1. Go to [hetzner.com](https://www.hetzner.com/)\n",
"2. Click \"Sign Up\" and create your account\n",
"3. Verify your email address\n",
"4. Add payment method (credit card or PayPal)\n",
"\n",
"### 1.2 Create Your Server\n",
"1. Log into Hetzner Cloud Console\n",
"2. Click \"New Project\" → Name it \"crypto-trading-bot\"\n",
"3. Click \"Add Server\"\n",
"4. **Choose Location**: Nuremberg or Helsinki (closest to major EU exchanges)\n",
"5. **Choose Image**: Ubuntu 22.04\n",
"6. **Choose Type**: CX11 (1 vCPU, 2GB RAM, 20GB storage) - €3.29/month\n",
"7. **SSH Key Setup**:\n",
" - If you have SSH key: Upload your public key\n",
" - If you don't: Select \"Password\" (we'll create SSH key later)\n",
"8. **Server Name**: \"trading-bot-server\"\n",
"9. Click \"Create & Buy Now\"\n",
"\n",
"**Your server will be ready in 30-60 seconds!**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Ubuntu Setup & SSH Access\n",
"\n",
"### 2.1 For Linux/Mac Users\n",
"\n",
"Replace `YOUR_SERVER_IP` with your actual server IP from Hetzner console:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# Connect to your server (run in terminal, not Jupyter)\n",
"# If you used SSH key during setup:\n",
"ssh root@YOUR_SERVER_IP\n",
"\n",
"# If you used password:\n",
"ssh root@YOUR_SERVER_IP\n",
"# Enter the password from Hetzner console"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Create SSH Key (if you didn't have one)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# On your local machine (run in terminal, not Jupyter):\n",
"ssh-keygen -t rsa -b 4096 -C \"your-email@example.com\"\n",
"# Press Enter for default location\n",
"# Press Enter for no passphrase (or set one if you prefer)\n",
"\n",
"# Copy your public key to server:\n",
"ssh-copy-id root@YOUR_SERVER_IP"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.2 For Windows Users\n",
"\n",
"#### Option A: Use Windows Terminal (Recommended)\n",
"1. Install Windows Terminal from Microsoft Store\n",
"2. Open Terminal and use same commands as Linux/Mac above\n",
"\n",
"#### Option B: Use PuTTY\n",
"1. Download PuTTY from [putty.org](https://www.putty.org/)\n",
"2. Open PuTTY\n",
"3. **Host Name**: YOUR_SERVER_IP\n",
"4. **Port**: 22\n",
"5. **Connection Type**: SSH\n",
"6. Click \"Open\"\n",
"7. Username: `root`\n",
"8. Password: (from Hetzner console)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# Create SSH Key for Windows (run in Windows Terminal or PowerShell):\n",
"ssh-keygen -t rsa -b 4096 -C \"your-email@example.com\"\n",
"# Keys will be saved in C:\\Users\\YourName\\.ssh\\"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Install Python & Dependencies\n",
"\n",
"**⚠️ Important: The following commands should be run on your Hetzner server (via SSH), not in this Jupyter notebook!**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.1 Update System"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# Update package list and upgrade system\n",
"sudo apt update && sudo apt upgrade -y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.2 Install Python and Pip"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# Install Python 3 and pip\n",
"sudo apt install python3 python3-pip python3-venv -y\n",
"\n",
"# Check installation\n",
"python3 --version\n",
"pip3 --version"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.3 Install Required Python Libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# Install common trading libraries\n",
"pip3 install pandas numpy ccxt python-dotenv requests\n",
"\n",
"# For backtesting (if using backtesting.py)\n",
"pip3 install backtesting\n",
"\n",
"# For notifications\n",
"pip3 install python-telegram-bot\n",
"\n",
"# Install any other libraries your bot uses"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.4 Install Screen (for running bot persistently)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"sudo apt install screen -y"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Upload Your Bot Code\n",
"\n",
"### 4.1 Method 1: Using SCP (Secure Copy) - Linux/Mac"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# From your local machine, copy your bot files (run in local terminal):\n",
"scp -r /path/to/your/bot/folder root@YOUR_SERVER_IP:/root/trading-bot/\n",
"\n",
"# Example:\n",
"scp -r ~/Desktop/my-crypto-bot root@YOUR_SERVER_IP:/root/trading-bot/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.2 Method 1: Using SCP - Windows"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# In Windows Terminal or PowerShell (run locally):\n",
"scp -r C:\\path\\to\\your\\bot\\folder root@YOUR_SERVER_IP:/root/trading-bot/\n",
"\n",
"# Example:\n",
"scp -r C:\\Users\\YourName\\Desktop\\my-crypto-bot root@YOUR_SERVER_IP:/root/trading-bot/"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.3 Method 2: Using Git (Recommended)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# On your server:\n",
"cd /root/\n",
"git clone https://github.com/yourusername/your-trading-bot.git\n",
"cd your-trading-bot\n",
"\n",
"# Or if you don't have git repo, create directory and use nano:\n",
"mkdir trading-bot\n",
"cd trading-bot\n",
"nano main.py\n",
"# Paste your code, save with Ctrl+X, Y, Enter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.4 Method 3: Using File Transfer (Windows PuTTY users)\n",
"1. Download WinSCP from [winscp.net](https://winscp.net/)\n",
"2. Connect using same credentials as PuTTY\n",
"3. Drag and drop your bot files to `/root/trading-bot/`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Configure Environment Variables\n",
"\n",
"### What are .env files?\n",
"\n",
"**Environment Variables & .env Files Explained**\n",
"\n",
"**What is a .env file?**\n",
"- A simple text file that stores configuration data\n",
"- Contains key-value pairs (API_KEY=your_secret_key)\n",
"- Kept separate from your main code\n",
"\n",
"**Why use .env files?**\n",
"- **Security**: Keeps secrets out of your code\n",
"- **Flexibility**: Easy to change settings without editing code\n",
"- **Version Control**: Never accidentally commit API keys to GitHub\n",
"- **Environment-specific**: Different keys for testing vs production"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.1 Create .env File"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"cd /root/trading-bot\n",
"nano .env"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.2 Add Your API Keys\n",
"\n",
"In the nano editor, add your keys:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# Example .env file content:\n",
"BINANCE_API_KEY=your_actual_api_key_here\n",
"BINANCE_SECRET=your_actual_secret_key_here\n",
"\n",
"# Save: Ctrl+X, then Y, then Enter"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.3 Using .env in Your Python Code"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"from dotenv import load_dotenv\n",
"\n",
"# Load environment variables\n",
"load_dotenv()\n",
"\n",
"# Access your API keys\n",
"api_key = os.getenv('BINANCE_API_KEY')\n",
"secret_key = os.getenv('BINANCE_SECRET')\n",
"\n",
"print(f\"API Key loaded: {api_key[:10]}...\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 5.4 Secure Your .env File"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# Make file readable only by owner\n",
"chmod 600 .env\n",
"\n",
"# Verify permissions\n",
"ls -la .env\n",
"# Should show: -rw------- 1 root root"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Test Your Bot\n",
"\n",
"### 6.1 Run a Test"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"cd /root/trading-bot\n",
"python3 main.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 6.2 Run in Background with Screen"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# Create a new screen session\n",
"screen -S trading-bot\n",
"\n",
"# Run your bot\n",
"python3 main.py\n",
"\n",
"# Detach from screen: Ctrl+A, then D\n",
"# Reattach later: screen -r trading-bot\n",
"# List screens: screen -ls"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 7: Set Up Automatic Restart (Optional)\n",
"\n",
"### 7.1 Create a Systemd Service"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"sudo nano /etc/systemd/system/trading-bot.service"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 7.2 Add Service Configuration\n",
"\n",
"Add this content to the service file:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"[Unit]\n",
"Description=Crypto Trading Bot\n",
"After=network.target\n",
"\n",
"[Service]\n",
"Type=simple\n",
"User=root\n",
"WorkingDirectory=/root/trading-bot\n",
"ExecStart=/usr/bin/python3 /root/trading-bot/main.py\n",
"Restart=always\n",
"RestartSec=10\n",
"\n",
"[Install]\n",
"WantedBy=multi-user.target"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 7.3 Enable and Start Service"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# Reload systemd\n",
"sudo systemctl daemon-reload\n",
"\n",
"# Enable service (start on boot)\n",
"sudo systemctl enable trading-bot\n",
"\n",
"# Start service now\n",
"sudo systemctl start trading-bot\n",
"\n",
"# Check status\n",
"sudo systemctl status trading-bot\n",
"\n",
"# View logs\n",
"sudo journalctl -u trading-bot -f"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Common Commands Reference\n",
"\n",
"### Server Management"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# Check running processes\n",
"ps aux | grep python\n",
"\n",
"# Kill a process\n",
"kill PID_NUMBER\n",
"\n",
"# Check system resources\n",
"htop\n",
"# (install with: sudo apt install htop)\n",
"\n",
"# Check disk space\n",
"df -h\n",
"\n",
"# Check memory usage\n",
"free -h"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Screen Commands"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# Create new screen\n",
"screen -S session_name\n",
"\n",
"# List screens\n",
"screen -ls\n",
"\n",
"# Reattach to screen\n",
"screen -r session_name\n",
"\n",
"# Detach from screen\n",
"# Ctrl+A, then D\n",
"\n",
"# Kill screen session\n",
"screen -X -S session_name quit"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### File Operations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# View file contents\n",
"cat filename.py\n",
"\n",
"# Edit file\n",
"nano filename.py\n",
"\n",
"# Copy file\n",
"cp source.py backup.py\n",
"\n",
"# Move/rename file\n",
"mv oldname.py newname.py\n",
"\n",
"# Delete file\n",
"rm filename.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Troubleshooting\n",
"\n",
"### Connection Issues\n",
"- **Can't connect via SSH**: Check if server is running in Hetzner console\n",
"- **Permission denied**: Make sure you're using correct username (root) and IP\n",
"- **SSH key issues**: Try password authentication first\n",
"\n",
"### Python Issues\n",
"- **ModuleNotFoundError**: Install missing packages with `pip3 install package_name`\n",
"- **Permission errors**: Make sure you're in the correct directory\n",
"- **Python version issues**: Use `python3` instead of `python`\n",
"\n",
"### Bot Issues\n",
"- **API errors**: Double-check your .env file and API key permissions\n",
"- **Network errors**: Check internet connection with `ping google.com`\n",
"- **Log errors**: Check bot output for specific error messages"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Getting Help"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"vscode": {
"languageId": "bat"
}
},
"outputs": [],
"source": [
"# Check system logs\n",
"sudo journalctl -xe\n",
"\n",
"# Check your bot logs if using systemd\n",
"sudo journalctl -u trading-bot -f\n",
"\n",
"# Monitor system resources\n",
"htop"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🎉 Congratulations!\n",
"\n",
"**Your trading bot is now running on Hetzner Cloud!**\n",
"\n",
"**Next Steps:**\n",
"- Monitor your bot's performance\n",
"- Set up alerts/notifications\n",
"- Consider adding more sophisticated error handling\n",
"- Scale up server resources if needed\n",
"\n",
"**Remember:**\n",
"- Start with small position sizes\n",
"- Test thoroughly before going live\n",
"- Always have kill switches ready\n",
"- Monitor your bot regularly"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}