Spaces:
Sleeping
Sleeping
A newer version of the Gradio SDK is available: 6.26.0
Production Deployment Guide
This guide covers deploying the Chatty application in a production environment with proper security configurations.
Prerequisites
- Python 3.8+
- MongoDB Atlas account or MongoDB server
- HTTPS-enabled domain (required for secure cookies)
- Environment with proper SSL/TLS support
Quick Start
Generate Production Configuration
python deploy.py gen-prod-envThis creates
.env.productionwith secure defaults.Configure Environment Variables Edit
.env.productionand update:MONGODB_URL: Your MongoDB connection string- Review all other settings
Validate Configuration
FLASK_ENV=production python deploy.py check-prodDeploy
export FLASK_ENV=production ./start.sh
Environment Variables
Required Variables
| Variable | Description | Example |
|---|---|---|
SECRET_KEY |
Flask secret key (32+ chars) | abc123... |
MONGODB_URL |
MongoDB connection string | mongodb+srv://... |
FLASK_ENV |
Environment mode | production |
Security Variables
| Variable | Default | Production | Description |
|---|---|---|---|
SESSION_COOKIE_SECURE |
false |
true |
Require HTTPS for cookies |
WTF_CSRF_SSL_STRICT |
false |
true |
Strict CSRF over HTTPS |
MAX_LOGIN_ATTEMPTS |
5 |
3 |
Failed login limit |
RATE_LIMIT_WINDOW |
900 |
1800 |
Rate limit window (seconds) |
Optional Variables
| Variable | Default | Description |
|---|---|---|
SESSION_LIFETIME_HOURS |
24 |
Session duration |
API_TIMEOUT |
30 |
External API timeout |
LOG_LEVEL |
INFO |
Logging level |
LOG_FILE |
None | Log file path |
PORT |
7860 |
Server port |
Security Checklist
Before Deployment
- Generate secure
SECRET_KEY(32+ characters) - Configure MongoDB with authentication
- Set up HTTPS/SSL certificates
- Review rate limiting settings
- Configure proper logging
- Test database connectivity
Production Configuration
-
FLASK_ENV=production -
DEBUG=False(automatic in production) -
SESSION_COOKIE_SECURE=true -
WTF_CSRF_SSL_STRICT=true - Strong MongoDB credentials
- Firewall rules configured
Monitoring
- Set up log monitoring
- Configure health checks
- Monitor database connections
- Track authentication failures
Deployment Methods
Hugging Face Spaces
- Create
.envfile with production variables - Ensure
PORT=7860is set - Deploy with:
export FLASK_ENV=production python app.py
Docker Deployment
Create Dockerfile:
FROM python:3.9-slim
WORKDIR /app
COPY ../requirements.txt .
RUN pip install -r requirements.txt
COPY .. .
ENV FLASK_ENV=production
EXPOSE 7860
CMD ["python", "app.py"]
Build and run:
docker build -t chatty .
docker run -p 7860:7860 --env-file .env.production chatty
Traditional Server
- Set up reverse proxy (nginx/Apache)
- Configure SSL certificates
- Use process manager (systemd/supervisor)
- Set environment variables
- Start application
Example systemd service (/etc/systemd/system/chatty.service):
[Unit]
Description=Chatty Application
After=network.target
[Service]
Type=simple
User=chatty
WorkingDirectory=/opt/chatty
Environment=FLASK_ENV=production
EnvironmentFile=/opt/chatty/.env.production
ExecStart=/opt/chatty/atlas_env/bin/python app.py
Restart=always
[Install]
WantedBy=multi-user.target
Database Configuration
MongoDB Atlas
- Create cluster in MongoDB Atlas
- Configure network access (IP whitelist)
- Create database user with appropriate permissions
- Get connection string from Atlas dashboard
- Update
MONGODB_URLin environment
Self-Hosted MongoDB
- Install and configure MongoDB
- Enable authentication
- Create application database and user
- Configure SSL/TLS if needed
- Set connection string in
MONGODB_URL
Troubleshooting
Configuration Issues
# Check configuration
python deploy.py check-prod
# Generate new secret key
python deploy.py gen-secret
# Test database connection
python -c "from database import test_connection; test_connection()"
Common Issues
Secret Key Errors
- Generate new key:
python deploy.py gen-secret - Ensure key is 32+ characters
- Generate new key:
Database Connection Failures
- Check MongoDB URI format
- Verify network connectivity
- Check authentication credentials
Session/Cookie Issues
- Ensure HTTPS is configured
- Check
SESSION_COOKIE_SECUREsetting - Verify domain configuration
CSRF Token Errors
- Check
WTF_CSRF_SSL_STRICTsetting - Ensure forms include CSRF tokens
- Verify HTTPS configuration
- Check
Logs and Monitoring
- Application logs: Check
LOG_FILEor console output - Database logs: MongoDB logs for connection issues
- Web server logs: nginx/Apache access and error logs
- System logs: systemd journal for service issues
Security Best Practices
Environment Variables
- Never commit
.envfiles to version control - Use secure secret management in production
- Rotate secrets regularly
- Never commit
Database Security
- Use strong passwords
- Enable MongoDB authentication
- Configure network restrictions
- Regular backups
Application Security
- Keep dependencies updated
- Monitor for security vulnerabilities
- Use HTTPS everywhere
- Implement proper logging
Infrastructure Security
- Keep OS updated
- Configure firewall rules
- Use fail2ban for brute force protection
- Regular security audits
Performance Optimization
Database Optimization
- Proper indexing (handled automatically)
- Connection pooling (configured)
- Query optimization
Application Optimization
- Enable gzip compression
- Use CDN for static assets
- Implement caching where appropriate
Infrastructure Optimization
- Use reverse proxy (nginx)
- Load balancing for high traffic
- Monitor resource usage
Backup and Recovery
Database Backups
- Regular MongoDB backups
- Test restore procedures
- Store backups securely
Application Backups
- Code repository backups
- Configuration backups
- Log file archival
Disaster Recovery
- Document recovery procedures
- Test disaster recovery
- Maintain backup infrastructure