Deployment¶
This guide covers deployment strategies and procedures for CitizenAI in different environments.
Overview¶
CitizenAI supports multiple deployment scenarios:
- Local Development: For testing and development
- Production Server: Full-scale deployment
- Docker Containers: Containerized deployment
- Cloud Platforms: AWS, Azure, Google Cloud deployment
Prerequisites¶
Before deploying CitizenAI, ensure you have:
- Python 3.8 or higher
- Required dependencies installed
- Database configured
- Environment variables set
- SSL certificates (for production)
Local Development Deployment¶
Quick Start¶
-
Clone the repository:
-
Install dependencies:
-
Configure environment:
-
Run the application:
The application will be available at http://localhost:5000
.
Production Deployment¶
Server Requirements¶
- CPU: 2+ cores recommended
- RAM: 4GB minimum, 8GB recommended
- Storage: 20GB minimum
- OS: Ubuntu 20.04+ or CentOS 7+
Installation Steps¶
-
Prepare the server:
-
Clone and setup application:
-
Configure environment:
-
Setup systemd service:
Service file content:
[Unit]
Description=CitizenAI Flask Application
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/path/to/citizen-ai
Environment="PATH=/path/to/citizen-ai/venv/bin"
ExecStart=/path/to/citizen-ai/venv/bin/python app.py
Restart=always
[Install]
WantedBy=multi-user.target
- Start the service:
Nginx Configuration¶
Create nginx configuration:
Configuration content:
server {
listen 80;
server_name your-domain.com;
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;
}
}
Enable the site:
sudo ln -s /etc/nginx/sites-available/citizen-ai /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Docker Deployment¶
Dockerfile¶
Create a Dockerfile
:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python", "app.py"]
Docker Compose¶
Create docker-compose.yml
:
version: '3.8'
services:
citizen-ai:
build: .
ports:
- "5000:5000"
environment:
- FLASK_ENV=production
- DATABASE_URL=postgresql://user:pass@db:5432/citizenai
depends_on:
- db
db:
image: postgres:13
environment:
- POSTGRES_DB=citizenai
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
Deploy with Docker¶
# Build and run
docker-compose up -d
# View logs
docker-compose logs -f
# Stop services
docker-compose down
Cloud Platform Deployment¶
AWS Deployment¶
Using EC2¶
- Launch EC2 instance (Ubuntu 20.04)
- Configure security groups (ports 22, 80, 443)
- Follow production deployment steps
- Configure Load Balancer if needed
Using Elastic Beanstalk¶
- Create
application.py
(entry point) - Package application
- Deploy through EB console
- Configure environment variables
Azure Deployment¶
Using App Service¶
- Create Azure App Service
- Configure Python runtime
- Deploy via Git or Azure CLI
- Set application settings
Google Cloud Deployment¶
Using App Engine¶
Create app.yaml
:
runtime: python39
env_variables:
FLASK_ENV: production
DATABASE_URL: your-database-url
automatic_scaling:
min_instances: 1
max_instances: 10
Deploy:
Environment Configuration¶
Environment Variables¶
Required environment variables:
# Application
FLASK_ENV=production
SECRET_KEY=your-secret-key
# Database
DATABASE_URL=postgresql://user:pass@host:port/db
# AI Services
OPENAI_API_KEY=your-openai-key
GEMINI_API_KEY=your-gemini-key
# Security
JWT_SECRET_KEY=your-jwt-secret
CORS_ORIGINS=https://your-domain.com
Configuration Management¶
Use different configuration files for different environments:
config/development.py
config/production.py
config/testing.py
Database Setup¶
PostgreSQL¶
-
Install PostgreSQL:
-
Create database and user:
-
Run migrations:
SSL/TLS Configuration¶
Using Let's Encrypt¶
-
Install Certbot:
-
Obtain certificate:
-
Auto-renewal:
Monitoring and Logging¶
Application Logging¶
Configure logging in your application:
import logging
from logging.handlers import RotatingFileHandler
if not app.debug:
file_handler = RotatingFileHandler('logs/citizenai.log', maxBytes=10240, backupCount=10)
file_handler.setFormatter(logging.Formatter(
'%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]'
))
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
System Monitoring¶
Use tools like: - htop for system resources - nginx access logs for web traffic - systemd journal for service logs
Backup and Recovery¶
Database Backup¶
# Create backup
pg_dump citizenai > backup_$(date +%Y%m%d).sql
# Restore backup
psql citizenai < backup_20231201.sql
Application Backup¶
Performance Optimization¶
Application Level¶
- Use caching (Redis/Memcached)
- Optimize database queries
- Implement connection pooling
- Use CDN for static assets
Server Level¶
-
Configure Gunicorn for production:
-
Optimize Nginx:
Security Considerations¶
Application Security¶
- Use HTTPS in production
- Implement rate limiting
- Validate all inputs
- Use secure session management
- Regular security updates
Server Security¶
- Configure firewall (ufw)
- Disable root SSH access
- Use SSH key authentication
- Regular system updates
- Monitor access logs
Troubleshooting¶
Common Issues¶
- Port conflicts: Check if port 5000 is available
- Permission errors: Verify file permissions
- Database connection: Check database credentials
- Environment variables: Verify all required vars are set
Debug Commands¶
# Check service status
sudo systemctl status citizen-ai
# View logs
sudo journalctl -u citizen-ai -f
# Check ports
sudo netstat -tlnp | grep :5000
# Test database connection
psql -h localhost -U citizenai_user -d citizenai
Maintenance¶
Regular Tasks¶
-
Update dependencies:
-
Database maintenance:
-
Log rotation:
-
Security updates:
Scaling¶
Horizontal Scaling¶
- Use load balancers (nginx, HAProxy)
- Deploy multiple application instances
- Implement session storage (Redis)
- Use database replication
Vertical Scaling¶
- Increase server resources
- Optimize database configuration
- Use application profiling
- Implement caching strategies
Support¶
For deployment issues:
- Check the Troubleshooting Guide
- Review FAQ
- Contact support team
- File issues on GitHub
Next Steps¶
After successful deployment:
- Set up monitoring and alerts
- Configure automated backups
- Implement CI/CD pipeline
- Plan disaster recovery
- Document operational procedures