# Ubuntu Server Deployment Guide This guide covers deploying the RSC Revolution Market Analytics system on Ubuntu Server. ## Prerequisites - Ubuntu Server 18.04+ (tested on 20.04/22.04) - Python 3.8+ (comes with Ubuntu 20.04+) - MySQL access to RSC Revolution database - SSH access to Ubuntu server ## Quick Start ```bash # 1. Install dependencies sudo apt update sudo apt install python3 python3-pip git # 2. Clone repository git clone market_analytics cd market_analytics # 3. Install Python packages pip3 install -r requirements.txt # 4. Configure environment cp .env.example .env nano .env # Edit database credentials # 5. Test installation python3 run_simple_tests.py # 6. Setup automated collection chmod +x setup_cron_ubuntu.sh ./setup_cron_ubuntu.sh ``` ## Detailed Installation ### 1. System Setup ```bash # Update system sudo apt update && sudo apt upgrade -y # Install required packages sudo apt install -y python3 python3-pip python3-venv # Create dedicated user (optional but recommended) sudo useradd -m -s /bin/bash analytics sudo su - analytics ``` ### 2. Application Setup ```bash # Clone repository cd /home/analytics git clone market_analytics cd market_analytics # Create virtual environment (recommended) python3 -m venv venv source venv/bin/activate # Install dependencies pip3 install -r requirements.txt ``` ### 3. Configuration ```bash # Copy and edit environment file cp .env.example .env nano .env ``` Update `.env` with your database credentials: ```env DB_HOST=your-mysql-host DB_USER=your-mysql-user DB_PASSWORD=your-mysql-password DB_NAME=rscs_main ``` ### 4. Verify Installation ```bash # Test core functionality python3 run_simple_tests.py # Expected: 6/7 tests passing # Health check may show warnings until data collection starts ``` ### 5. Setup Automated Collection Create Ubuntu-specific cron setup: ```bash # Create Ubuntu cron script cat > setup_cron_ubuntu.sh << 'EOF' #!/bin/bash # RSC Revolution Market Analytics - Ubuntu Cron Setup ANALYTICS_DIR="/home/analytics/market_analytics" PYTHON_PATH="/usr/bin/python3" # If using virtual environment, update this path: # PYTHON_PATH="/home/analytics/market_analytics/venv/bin/python" echo "Setting up cron jobs for market analytics..." # Remove existing analytics cron jobs crontab -l 2>/dev/null | grep -v "market_analytics" | crontab - # Add new cron jobs (crontab -l 2>/dev/null; cat << EOL # RSC Revolution Market Analytics # Current state - every 5 minutes */5 * * * * cd ${ANALYTICS_DIR} && ${PYTHON_PATH} collectors/current_state.py >> logs/cron.log 2>&1 # Hourly snapshot - at minute 1 of every hour 1 * * * * cd ${ANALYTICS_DIR} && ${PYTHON_PATH} collectors/hourly_snapshot.py >> logs/cron.log 2>&1 # Notable trades - every 10 minutes */10 * * * * cd ${ANALYTICS_DIR} && ${PYTHON_PATH} collectors/notable_trades.py >> logs/cron.log 2>&1 # Health check - every 30 minutes */30 * * * * cd ${ANALYTICS_DIR} && ${PYTHON_PATH} monitoring/health_check.py >> logs/health.log 2>&1 # Weekly cleanup - Sundays at 2 AM 0 2 * * 0 cd ${ANALYTICS_DIR} && ${PYTHON_PATH} maintenance/cleanup_old_data.py >> logs/maintenance.log 2>&1 EOL ) | crontab - echo "Cron jobs installed successfully!" echo "View with: crontab -l" echo "Monitor logs: tail -f ${ANALYTICS_DIR}/logs/cron.log" EOF chmod +x setup_cron_ubuntu.sh ``` Run the setup: ```bash ./setup_cron_ubuntu.sh ``` ### 6. Create Log Directory ```bash mkdir -p logs touch logs/cron.log logs/health.log logs/maintenance.log ``` ## System Service Setup (Optional) For more robust deployment, create a systemd service: ```bash # Create service file sudo tee /etc/systemd/system/market-analytics.service > /dev/null << EOF [Unit] Description=RSC Revolution Market Analytics After=network.target mysql.service [Service] Type=oneshot User=analytics WorkingDirectory=/home/analytics/market_analytics ExecStart=/usr/bin/python3 collectors/current_state.py StandardOutput=append:/home/analytics/market_analytics/logs/service.log StandardError=append:/home/analytics/market_analytics/logs/service.log [Install] WantedBy=multi-user.target EOF # Create timer for regular execution sudo tee /etc/systemd/system/market-analytics.timer > /dev/null << EOF [Unit] Description=Run Market Analytics every 5 minutes Requires=market-analytics.service [Timer] OnCalendar=*:0/5 Persistent=true [Install] WantedBy=timers.target EOF # Enable and start sudo systemctl daemon-reload sudo systemctl enable market-analytics.timer sudo systemctl start market-analytics.timer ``` ## Monitoring and Maintenance ### Check System Status ```bash # View active cron jobs crontab -l # Monitor real-time logs tail -f logs/cron.log # Check health status python3 monitoring/health_check.py # View recent database activity python3 -c " from config.database import get_db_connection with get_db_connection() as conn: cursor = conn.cursor() cursor.execute('SELECT COUNT(*) as count FROM market_current_state') print(f'Current items tracked: {cursor.fetchone()[\"count\"]}') cursor.execute('SELECT MAX(last_updated) as latest FROM market_current_state') print(f'Last update: {cursor.fetchone()[\"latest\"]}') " ``` ### Log Rotation Set up automatic log rotation: ```bash sudo tee /etc/logrotate.d/market-analytics > /dev/null << EOF /home/analytics/market_analytics/logs/*.log { daily rotate 30 compress delaycompress missingok notifempty create 644 analytics analytics } EOF ``` ### Firewall (if needed) ```bash # If connecting to remote MySQL server sudo ufw allow out 3306/tcp ``` ## Troubleshooting ### Common Issues 1. **Permission denied on cron jobs** ```bash chmod +x collectors/*.py monitoring/*.py maintenance/*.py ``` 2. **Python module not found** ```bash # Check Python path in cron jobs which python3 # Update PYTHON_PATH in setup_cron_ubuntu.sh if needed ``` 3. **Database connection failed** ```bash # Test MySQL connectivity mysql -h $DB_HOST -u $DB_USER -p$DB_PASSWORD $DB_NAME -e "SELECT 1" ``` 4. **Virtual environment issues** ```bash # If using venv, update cron script Python path: PYTHON_PATH="/home/analytics/market_analytics/venv/bin/python" ``` ### Log Locations - **Cron activities**: `logs/cron.log` - **Health checks**: `logs/health.log` - **Maintenance tasks**: `logs/maintenance.log` - **Application logs**: `logs/` (auto-rotated daily) ## Performance Optimization ### Database Optimization ```sql -- Ensure indexes exist (run once) CREATE INDEX IF NOT EXISTS idx_market_sales_time ON pk_market_sales(time); CREATE INDEX IF NOT EXISTS idx_market_active_deadman ON pk_market_active(deadman); CREATE INDEX IF NOT EXISTS idx_prices_hourly_time ON market_prices_hourly(snapshot_time); CREATE INDEX IF NOT EXISTS idx_current_state_updated ON market_current_state(last_updated); ``` ### System Resources - **Memory**: 512MB minimum, 1GB recommended - **Storage**: 5GB minimum for logs and data - **CPU**: Single core sufficient for most RSC servers ## Security Notes - Use dedicated MySQL user with minimal permissions (SELECT only on game tables) - Consider running analytics in isolated user account - Regularly rotate database passwords - Monitor log files for unusual activity ## Backup Strategy ```bash # Weekly backup script (add to cron) #!/bin/bash BACKUP_DIR="/home/analytics/backups" DATE=$(date +%Y%m%d) mkdir -p $BACKUP_DIR # Backup analytics data mysqldump -h $DB_HOST -u $DB_USER -p$DB_PASSWORD $DB_NAME \ market_prices_hourly market_current_state market_notable_trades \ > $BACKUP_DIR/market_analytics_$DATE.sql # Cleanup old backups (keep 4 weeks) find $BACKUP_DIR -name "market_analytics_*.sql" -mtime +28 -delete ``` --- **System is now ready for production use on Ubuntu Server!** For support or issues, check the troubleshooting section above or review application logs.