# Video Downloader - Deployment Instructions

This guide will help you deploy the Video Downloader application on your own server.

## Prerequisites

- Python 3.8 or higher
- pip (Python package manager)
- A web server (e.g., Nginx, Apache)
- A server with at least 1GB RAM

## Installation Steps

### 1. Prepare the server environment

```bash
# Update package lists
sudo apt update
sudo apt upgrade -y

# Install required system packages
sudo apt install -y python3 python3-pip python3-venv nginx
```

### 2. Set up the application

```bash
# Create a directory for the application
mkdir -p /var/www/video-downloader
cd /var/www/video-downloader

# Extract the zip file contents to this directory
# (Assuming you've uploaded the zip file to the server)
unzip /path/to/video-downloader.zip -d .

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

# Install required packages
pip install -r requirements.txt
```

### 3. Configure Gunicorn

Create a systemd service file to run the application with Gunicorn:

```bash
sudo nano /etc/systemd/system/video-downloader.service
```

Add the following content to the file:

```
[Unit]
Description=Video Downloader Gunicorn daemon
After=network.target

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/video-downloader
ExecStart=/var/www/video-downloader/venv/bin/gunicorn --workers 3 --bind 0.0.0.0:5000 --timeout 120 main:app
Restart=on-failure

[Install]
WantedBy=multi-user.target
```

Save and close the file, then enable and start the service:

```bash
sudo systemctl daemon-reload
sudo systemctl enable video-downloader
sudo systemctl start video-downloader
```

### 4. Configure Nginx as a reverse proxy

Create an Nginx configuration file:

```bash
sudo nano /etc/nginx/sites-available/video-downloader
```

Add the following content:

```
server {
    listen 80;
    server_name your-domain.com;  # Replace with your domain

    location / {
        proxy_pass http://127.0.0.1:5000;
        proxy_set_header Host $host;
        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;
        
        # These settings are necessary for big file downloads
        proxy_read_timeout 300;
        proxy_connect_timeout 300;
        proxy_send_timeout 300;
        proxy_buffering off;
    }
    
    # Configure download directory for direct access if needed
    location /downloads {
        alias /var/www/video-downloader/downloads;
        autoindex off;
        sendfile on;
        sendfile_max_chunk 1m;
        tcp_nopush on;
    }
}
```

Enable the site and restart Nginx:

```bash
sudo ln -s /etc/nginx/sites-available/video-downloader /etc/nginx/sites-enabled/
sudo nginx -t  # Test the configuration
sudo systemctl restart nginx
```

### 5. Set up SSL with Let's Encrypt

```bash
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com
```

Follow the prompts to complete the SSL setup.

### 6. Set proper permissions

```bash
# Make sure the downloads directory is writable
sudo mkdir -p /var/www/video-downloader/downloads
sudo chown -R www-data:www-data /var/www/video-downloader/downloads
sudo chmod -R 755 /var/www/video-downloader
```

## Maintenance and Updates

### Updating the application

When you want to update the application with a new version:

1. Back up the current version
2. Extract the new zip file
3. Restart the services

```bash
# Stop the service
sudo systemctl stop video-downloader

# Back up the current version
sudo cp -r /var/www/video-downloader /var/www/video-downloader-backup-$(date +%Y%m%d)

# Extract new files (skip the venv and downloads directories)
sudo unzip -o /path/to/new-video-downloader.zip -d /var/www/video-downloader -x "venv/*" "downloads/*"

# Update permissions
sudo chown -R www-data:www-data /var/www/video-downloader
sudo chmod -R 755 /var/www/video-downloader

# Restart the service
sudo systemctl start video-downloader
```

### Monitoring logs

To monitor application logs:

```bash
sudo journalctl -u video-downloader -f
```

To view Nginx logs:

```bash
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/nginx/access.log
```

## Troubleshooting

### The application doesn't start

Check the logs:

```bash
sudo journalctl -u video-downloader -n 50
```

### Downloads fail or are slow

1. Make sure the `downloads` directory has proper permissions
2. Check if disk space is sufficient
3. Make sure the proxy timeout settings in Nginx are adequate

### High server load

Adjust the number of Gunicorn workers based on your server's CPU:

```bash
# Edit the service file
sudo nano /etc/systemd/system/video-downloader.service

# Change the --workers parameter (generally 2-4 workers per CPU core)
# Then reload and restart
sudo systemctl daemon-reload
sudo systemctl restart video-downloader
```

## Security Considerations

1. Consider setting up a firewall (e.g., ufw or firewalld)
2. Regularly update the system and application dependencies
3. Monitor the server for unusual activities
4. Consider adding rate limiting to prevent abuse

For additional support, contact support@yourdomain.com.