# VidRip Production Deployment Guide This guide covers deploying VidRip in a production environment. ## Quick Start (Simple Deployment) The simplest way to run VidRip in production: ```bash # Make scripts executable (if not already) chmod +x start-production.sh stop-production.sh # Start the service ./start-production.sh # Stop the service ./stop-production.sh ``` The `start-production.sh` script will: - Check system requirements (Node.js 18+, yt-dlp) - Install dependencies - Build backend and frontend - Start the backend server in the background - Create log files in `logs/` directory ## Production Deployment (Recommended) For a robust production setup, use systemd for process management: ### 1. Install System Requirements ```bash # Install Node.js 18+ (if not installed) curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash - sudo apt-get install -y nodejs # Install yt-dlp sudo apt-get install -y python3-pip pip3 install yt-dlp # OR sudo wget https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -O /usr/local/bin/yt-dlp sudo chmod a+rx /usr/local/bin/yt-dlp ``` ### 2. Deploy Application Files ```bash # Create application directory sudo mkdir -p /var/www/vidrip sudo chown $USER:$USER /var/www/vidrip # Copy files to production location cp -r /path/to/vidrip/* /var/www/vidrip/ # Create required directories sudo mkdir -p /var/log/vidrip sudo chown www-data:www-data /var/log/vidrip ``` ### 3. Build the Application ```bash cd /var/www/vidrip # Install dependencies npm run install:all # Build backend cd backend npm run build # Build frontend cd ../frontend npm run build ``` ### 4. Configure Backend (Optional) Create `/var/www/vidrip/backend/.env`: ```env NODE_ENV=production PORT=3001 ``` ### 5. Setup Systemd Service ```bash # Copy service file sudo cp /var/www/vidrip/vidrip.service /etc/systemd/system/ # Edit the service file to match your paths and user sudo nano /etc/systemd/system/vidrip.service # Reload systemd sudo systemctl daemon-reload # Enable service to start on boot sudo systemctl enable vidrip # Start service sudo systemctl start vidrip # Check status sudo systemctl status vidrip ``` ### 6. Setup Nginx Reverse Proxy Create `/etc/nginx/sites-available/vidrip`: ```nginx server { listen 80; server_name your-domain.com; # Serve frontend static files location / { root /var/www/vidrip/frontend/dist; try_files $uri $uri/ /index.html; } # Proxy API requests to backend location /api/ { proxy_pass http://localhost:3001; 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; proxy_set_header X-Forwarded-Proto $scheme; } # Serve downloaded video files location /downloads/ { alias /var/www/vidrip/backend/downloads/; add_header Content-Type video/mp4; } # Increase upload size if needed client_max_body_size 100M; } ``` Enable the site: ```bash sudo ln -s /etc/nginx/sites-available/vidrip /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx ``` ### 7. Setup SSL with Let's Encrypt (Recommended) ```bash sudo apt-get install certbot python3-certbot-nginx sudo certbot --nginx -d your-domain.com ``` ## Systemd Service Management ```bash # Start service sudo systemctl start vidrip # Stop service sudo systemctl stop vidrip # Restart service sudo systemctl restart vidrip # View status sudo systemctl status vidrip # View logs sudo journalctl -u vidrip -f # View application logs tail -f /var/log/vidrip/vidrip.log tail -f /var/log/vidrip/vidrip-error.log ``` ## PM2 Alternative (Node.js Process Manager) If you prefer PM2 over systemd: ```bash # Install PM2 globally sudo npm install -g pm2 # Start application cd /var/www/vidrip/backend pm2 start dist/server.js --name vidrip # Save PM2 configuration pm2 save # Setup PM2 to start on boot pm2 startup # Follow the instructions shown # View logs pm2 logs vidrip # Monitor pm2 monit # Restart pm2 restart vidrip ``` ## Environment Variables Configure in `/var/www/vidrip/backend/.env`: ```env NODE_ENV=production PORT=3001 ``` ## Monitoring and Maintenance ### Check Service Health ```bash # Check if backend is running curl http://localhost:3001/api/config # Check disk space (downloads can fill up) df -h /var/www/vidrip/backend/downloads ``` ### Log Rotation Create `/etc/logrotate.d/vidrip`: ``` /var/log/vidrip/*.log { daily rotate 14 compress delaycompress notifempty create 0640 www-data www-data sharedscripts postrotate systemctl reload vidrip > /dev/null 2>&1 || true endscript } ``` ### Backup Strategy ```bash # Backup database cp /var/www/vidrip/backend/data.db /backups/vidrip-$(date +%Y%m%d).db # Backup downloads (optional, can be large) rsync -av /var/www/vidrip/backend/downloads/ /backups/downloads/ ``` ## Firewall Configuration ```bash # Allow HTTP/HTTPS sudo ufw allow 80/tcp sudo ufw allow 443/tcp # Backend should NOT be exposed directly # Only accessible via nginx proxy ``` ## Troubleshooting ### Service won't start ```bash # Check logs sudo journalctl -u vidrip -n 50 # Check if port is already in use sudo netstat -tlnp | grep 3001 # Check file permissions ls -la /var/www/vidrip/backend/dist/server.js ``` ### Database locked errors ```bash # Check database permissions ls -la /var/www/vidrip/backend/data.db # Ensure only one process is accessing database ps aux | grep vidrip ``` ### yt-dlp issues ```bash # Update yt-dlp pip3 install --upgrade yt-dlp # Check if it's in PATH which yt-dlp # Test manually yt-dlp --version ``` ## Security Checklist - [ ] Run backend as non-root user (www-data) - [ ] Enable firewall (ufw) - [ ] Setup SSL/TLS certificates - [ ] Restrict database file permissions - [ ] Configure nginx security headers - [ ] Enable log rotation - [ ] Regular yt-dlp updates - [ ] Monitor disk space for downloads - [ ] Setup fail2ban (optional) - [ ] Regular security updates ## Performance Tuning ### Download Directory Management Monitor download directory size: ```bash # Check total size du -sh /var/www/vidrip/backend/downloads # Find large files du -h /var/www/vidrip/backend/downloads/* | sort -rh | head -20 ``` ### Database Optimization ```bash # Vacuum database periodically sqlite3 /var/www/vidrip/backend/data.db "VACUUM;" ``` ## Updating the Application ```bash # Pull latest code cd /var/www/vidrip git pull # Install dependencies npm run install:all # Rebuild backend cd backend npm run build # Rebuild frontend cd ../frontend npm run build # Restart service sudo systemctl restart vidrip ```