> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/darkzOGx/youtube-automation-agent/llms.txt
> Use this file to discover all available pages before exploring further.

# VPS Deployment

> Deploy the YouTube Automation Agent to a VPS for 24/7 automated operation

## Overview

Deploying to a Virtual Private Server (VPS) enables true 24/7 automation without keeping your personal computer running. This is the recommended option for:

* Production YouTube channels
* Consistent daily uploads
* Multiple channels management
* Professional operations

<Note>
  VPS deployment costs approximately **\$5-20/month** depending on your provider and resource needs.
</Note>

## VPS Provider Recommendations

<CardGroup cols={2}>
  <Card title="DigitalOcean" icon="water">
    **\$6/month** - 1 GB RAM, 1 vCPU

    * Easy to use
    * Great documentation
    * 1-click deployments
  </Card>

  <Card title="Linode (Akamai)" icon="cloud">
    **\$5/month** - 1 GB RAM, 1 vCPU

    * Excellent performance
    * Strong network
    * Developer-friendly
  </Card>

  <Card title="Vultr" icon="rocket">
    **\$6/month** - 1 GB RAM, 1 vCPU

    * Global locations
    * High-frequency CPU options
    * Competitive pricing
  </Card>

  <Card title="Hetzner" icon="server">
    **€4.15/month** - 2 GB RAM, 1 vCPU

    * Best price/performance
    * European datacenters
    * Excellent value
  </Card>
</CardGroup>

## Server Requirements

### Minimum Specifications

| Component     | Minimum       | Recommended      |
| ------------- | ------------- | ---------------- |
| **CPU**       | 1 vCPU        | 2 vCPUs          |
| **RAM**       | 1 GB          | 2 GB             |
| **Storage**   | 10 GB SSD     | 25 GB SSD        |
| **Bandwidth** | 500 GB/month  | 1 TB/month       |
| **OS**        | Ubuntu 20.04+ | Ubuntu 22.04 LTS |

### Estimated Costs

<Tabs>
  <Tab title="Basic Setup">
    **\$6-8/month total**

    * VPS: \$5-6/month
    * Google Gemini API: Free
    * 1-2 videos per day
    * Perfect for starting out
  </Tab>

  <Tab title="Professional Setup">
    **\$15-25/month total**

    * VPS: \$12-15/month (2GB RAM)
    * OpenAI API: \$10/month
    * 3-5 videos per day
    * Better performance
  </Tab>

  <Tab title="Multi-Channel Setup">
    **\$30-50/month total**

    * VPS: \$20-30/month (4GB RAM)
    * OpenAI API: \$20/month
    * Multiple channels
    * High volume production
  </Tab>
</Tabs>

## Deployment Steps

<Steps>
  <Step title="Create VPS Instance">
    Choose your provider and create a new droplet/instance:

    **DigitalOcean Example:**

    1. Sign up at [digitalocean.com](https://www.digitalocean.com/)
    2. Click "Create" → "Droplets"
    3. Choose Ubuntu 22.04 LTS
    4. Select Basic plan (\$6/month)
    5. Choose datacenter region (closest to your target audience)
    6. Add SSH key for secure access
    7. Click "Create Droplet"

    <Note>
      Save your server's IP address - you'll need it for SSH access
    </Note>
  </Step>

  <Step title="Connect via SSH">
    Connect to your VPS:

    ```bash theme={null}
    ssh root@your-server-ip
    ```

    For Windows users, use [PuTTY](https://www.putty.org/) or Windows Terminal.
  </Step>

  <Step title="Update System Packages">
    ```bash theme={null}
    apt update && apt upgrade -y
    ```
  </Step>

  <Step title="Install Node.js 18+">
    Install Node.js using NodeSource:

    ```bash theme={null}
    curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
    apt install -y nodejs
    ```

    Verify installation:

    ```bash theme={null}
    node --version  # Should show v18.x.x or higher
    npm --version
    ```
  </Step>

  <Step title="Install Git">
    ```bash theme={null}
    apt install -y git
    ```
  </Step>

  <Step title="Install PM2 Process Manager">
    PM2 keeps your application running and restarts it if it crashes:

    ```bash theme={null}
    npm install -g pm2
    ```
  </Step>

  <Step title="Create Application User">
    For security, don't run as root:

    ```bash theme={null}
    adduser youtube-agent
    usermod -aG sudo youtube-agent
    su - youtube-agent
    ```
  </Step>

  <Step title="Clone Repository">
    ```bash theme={null}
    cd ~
    git clone https://github.com/darkzOGx/youtube-automation-agent.git
    cd youtube-automation-agent
    ```
  </Step>

  <Step title="Install Dependencies">
    ```bash theme={null}
    npm install --production
    ```

    <Note>
      The `--production` flag skips dev dependencies, saving disk space.
    </Note>
  </Step>

  <Step title="Configure Environment">
    Create and edit the environment file:

    ```bash theme={null}
    cp .env.example .env
    nano .env
    ```

    Update with your credentials:

    ```env theme={null}
    NODE_ENV=production
    PORT=3456
    LOG_LEVEL=info

    # AI Provider
    OPENAI_API_KEY=your-openai-key
    # OR
    GEMINI_API_KEY=your-gemini-key

    # Channel Settings
    CHANNEL_NAME=Your Channel Name
    DEFAULT_AUTHOR=Your Name
    TARGET_AUDIENCE=Your target audience

    # YouTube Settings
    YOUTUBE_REGION=US
    DEFAULT_PRIVACY_STATUS=public

    # Security
    JWT_SECRET=generate-secure-random-string-here
    ```

    Save with `Ctrl+X`, then `Y`, then `Enter`
  </Step>

  <Step title="Upload YouTube Credentials">
    Transfer your `credentials.json` file from local machine to VPS:

    **From your local machine:**

    ```bash theme={null}
    scp config/credentials.json youtube-agent@your-server-ip:~/youtube-automation-agent/config/
    ```

    **Or create it directly on the VPS:**

    ```bash theme={null}
    nano config/credentials.json
    # Paste your credentials JSON
    # Save with Ctrl+X, Y, Enter
    ```
  </Step>

  <Step title="Run Initial Setup">
    ```bash theme={null}
    npm run setup
    ```

    This initializes the database and verifies your configuration.
  </Step>

  <Step title="Start with PM2">
    Start the application with PM2:

    ```bash theme={null}
    pm2 start index.js --name youtube-agent
    ```

    Also start the scheduler:

    ```bash theme={null}
    pm2 start schedules/daily-automation.js --name youtube-scheduler
    ```

    Save the PM2 configuration:

    ```bash theme={null}
    pm2 save
    ```
  </Step>

  <Step title="Configure Auto-Start on Boot">
    Make PM2 restart on server reboot:

    ```bash theme={null}
    pm2 startup
    ```

    Copy and run the command that PM2 outputs.
  </Step>

  <Step title="Configure Firewall">
    Allow necessary ports:

    ```bash theme={null}
    # Switch back to root
    exit

    # Configure UFW firewall
    ufw allow 22/tcp      # SSH
    ufw allow 3456/tcp    # Application port
    ufw enable
    ```

    <Warning>
      Make sure to allow SSH (port 22) before enabling the firewall, or you'll lock yourself out!
    </Warning>
  </Step>
</Steps>

## Setting Up Nginx Reverse Proxy (Optional)

For production deployments, use Nginx as a reverse proxy:

<Steps>
  <Step title="Install Nginx">
    ```bash theme={null}
    apt install -y nginx
    ```
  </Step>

  <Step title="Configure Nginx">
    Create a new site configuration:

    ```bash theme={null}
    nano /etc/nginx/sites-available/youtube-agent
    ```

    Add this configuration:

    ```nginx theme={null}
    server {
        listen 80;
        server_name your-domain.com;  # Or use server IP
        
        location / {
            proxy_pass http://localhost:3456;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    ```
  </Step>

  <Step title="Enable Site">
    ```bash theme={null}
    ln -s /etc/nginx/sites-available/youtube-agent /etc/nginx/sites-enabled/
    nginx -t  # Test configuration
    systemctl restart nginx
    ```
  </Step>

  <Step title="Update Firewall">
    ```bash theme={null}
    ufw allow 'Nginx Full'
    ufw delete allow 3456/tcp  # No longer needed
    ```
  </Step>
</Steps>

## SSL/HTTPS Setup with Let's Encrypt (Optional)

Secure your dashboard with HTTPS:

```bash theme={null}
# Install Certbot
apt install -y certbot python3-certbot-nginx

# Obtain certificate
certbot --nginx -d your-domain.com

# Auto-renewal is configured automatically
```

## Management Commands

### PM2 Commands

```bash theme={null}
# View status
pm2 status

# View logs
pm2 logs youtube-agent
pm2 logs youtube-scheduler

# Restart application
pm2 restart youtube-agent

# Stop application
pm2 stop youtube-agent

# Monitor resources
pm2 monit

# View detailed info
pm2 info youtube-agent
```

### System Monitoring

```bash theme={null}
# Check disk usage
df -h

# Check memory usage
free -h

# Check CPU and processes
top
# Or use htop (install with: apt install htop)
htop

# Check network connections
netstat -tulpn | grep 3456
```

## Database Backups

Automate database backups:

<Steps>
  <Step title="Create Backup Script">
    ```bash theme={null}
    nano ~/backup-youtube-agent.sh
    ```

    Add:

    ```bash theme={null}
    #!/bin/bash
    BACKUP_DIR="$HOME/backups"
    DATE=$(date +%Y%m%d_%H%M%S)

    mkdir -p $BACKUP_DIR

    # Backup database and config
    cd ~/youtube-automation-agent
    tar -czf $BACKUP_DIR/youtube-agent-$DATE.tar.gz \
      data/ config/ .env

    # Keep only last 7 days of backups
    find $BACKUP_DIR -name "youtube-agent-*.tar.gz" -mtime +7 -delete

    echo "Backup completed: youtube-agent-$DATE.tar.gz"
    ```

    Make executable:

    ```bash theme={null}
    chmod +x ~/backup-youtube-agent.sh
    ```
  </Step>

  <Step title="Schedule with Cron">
    ```bash theme={null}
    crontab -e
    ```

    Add daily backup at 3 AM:

    ```cron theme={null}
    0 3 * * * /home/youtube-agent/backup-youtube-agent.sh >> /home/youtube-agent/backup.log 2>&1
    ```
  </Step>
</Steps>

## Updating the Application

```bash theme={null}
# Navigate to app directory
cd ~/youtube-automation-agent

# Stop the application
pm2 stop all

# Backup current version
tar -czf ~/youtube-agent-backup-pre-update.tar.gz .

# Pull latest changes
git pull origin main

# Install any new dependencies
npm install --production

# Restart application
pm2 restart all

# Check logs for errors
pm2 logs
```

## Monitoring and Alerts

### Set Up Email Alerts

Install and configure system mail:

```bash theme={null}
apt install -y mailutils

# Test email
echo "Test from YouTube Agent VPS" | mail -s "Test" your@email.com
```

### PM2 Monitoring

PM2 can send alerts on crashes:

```bash theme={null}
# Install PM2 notification module
pm2 install pm2-slack  # For Slack notifications
# Or
pm2 install pm2-discord  # For Discord notifications
```

## Troubleshooting

### Application Won't Start

```bash theme={null}
# Check PM2 logs
pm2 logs youtube-agent --lines 100

# Check system logs
journalctl -u youtube-agent -n 50

# Verify Node.js version
node --version

# Check for port conflicts
sudo netstat -tulpn | grep 3456
```

### High Memory Usage

```bash theme={null}
# Check memory
free -h

# Restart application to clear memory
pm2 restart youtube-agent

# Consider upgrading to 2GB RAM if consistently high
```

### Database Locked Errors

```bash theme={null}
# Stop all processes
pm2 stop all

# Remove lock files
rm -f data/*.db-shm data/*.db-wal

# Restart
pm2 restart all
```

### YouTube API Authentication Issues

```bash theme={null}
# Remove old tokens
rm -f data/youtube-oauth-token.json

# Re-authenticate
npm run credentials:setup

# Restart application
pm2 restart youtube-agent
```

## Security Best Practices

<Steps>
  <Step title="Keep System Updated">
    ```bash theme={null}
    # Set up automatic security updates
    apt install -y unattended-upgrades
    dpkg-reconfigure --priority=low unattended-upgrades
    ```
  </Step>

  <Step title="Use SSH Keys Only">
    Disable password authentication:

    ```bash theme={null}
    nano /etc/ssh/sshd_config
    ```

    Set:

    ```
    PasswordAuthentication no
    PermitRootLogin no
    ```

    Restart SSH:

    ```bash theme={null}
    systemctl restart sshd
    ```
  </Step>

  <Step title="Install Fail2Ban">
    Protect against brute force attacks:

    ```bash theme={null}
    apt install -y fail2ban
    systemctl enable fail2ban
    systemctl start fail2ban
    ```
  </Step>

  <Step title="Restrict File Permissions">
    ```bash theme={null}
    chmod 600 .env
    chmod 600 config/credentials.json
    chmod 700 data/
    ```
  </Step>
</Steps>

## Performance Optimization

### Enable Node.js Production Mode

Already set in `.env`:

```env theme={null}
NODE_ENV=production
```

### Optimize PM2 Settings

```bash theme={null}
pm2 start index.js --name youtube-agent \
  --max-memory-restart 500M \
  --log-date-format="YYYY-MM-DD HH:mm:ss Z"
```

### Database Optimization

```bash theme={null}
# Run VACUUM on SQLite database monthly
sqlite3 data/youtube-automation.db "VACUUM;"
```

## Cost Optimization Tips

<CardGroup cols={2}>
  <Card title="Use Gemini API" icon="google">
    Free tier handles most workloads
  </Card>

  <Card title="Smaller VPS" icon="server">
    1GB RAM sufficient for 1-2 videos/day
  </Card>

  <Card title="Reserved Instances" icon="money-bill">
    Some providers offer discounts for annual payment
  </Card>

  <Card title="Optimize Schedules" icon="clock">
    Reduce frequency if hitting API limits
  </Card>
</CardGroup>

## Next Steps

<CardGroup cols={2}>
  <Card title="Cloud Deployment" icon="cloud" href="/deployment/cloud">
    Scale to cloud platforms for enterprise needs
  </Card>

  <Card title="Monitoring" icon="chart-line" href="/guides/monitoring">
    Set up advanced monitoring and analytics
  </Card>

  <Card title="Configuration" icon="gear" href="/configuration/environment">
    Fine-tune your deployment
  </Card>

  <Card title="API Reference" icon="code" href="/api/endpoints">
    Integrate with your custom tools
  </Card>
</CardGroup>
