Skip to content

Latest commit

 

History

History
489 lines (392 loc) · 9.88 KB

File metadata and controls

489 lines (392 loc) · 9.88 KB

Productivity Tracking System - Setup & Deployment Guide

Table of Contents

  1. Prerequisites
  2. Backend Setup
  3. Frontend Setup
  4. Database Configuration
  5. Running Locally
  6. Docker Deployment
  7. Production Deployment
  8. Troubleshooting

Prerequisites

  • Python 3.9+
  • Node.js 16+
  • PostgreSQL 12+
  • Redis 6+
  • Git

Backend Setup

1. Clone Repository

cd backend
git clone <repository-url>

2. Create Virtual Environment

python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

3. Install Dependencies

pip install -r requirements.txt

4. Configure Environment Variables

cp .env.example .env

Edit .env file with your configuration:

SECRET_KEY=your-secure-secret-key-here
DEBUG=False
ALLOWED_HOSTS=localhost,127.0.0.1,yourdomain.com

DB_ENGINE=django.db.backends.postgresql
DB_NAME=productivity_db
DB_USER=postgres
DB_PASSWORD=your_secure_password
DB_HOST=localhost
DB_PORT=5432

REDIS_URL=redis://localhost:6379/0
CORS_ALLOWED_ORIGINS=http://localhost:3000

5. Create Database

# Using PostgreSQL psql
createdb productivity_db
createuser postgres_user

6. Run Migrations

python manage.py makemigrations
python manage.py migrate

7. Create Superuser

python manage.py createsuperuser

8. Load Seed Data (Optional)

python manage.py shell < seed_data.py

9. Collect Static Files

python manage.py collectstatic --noinput

Frontend Setup

1. Navigate to Frontend Directory

cd ../frontend

2. Install Dependencies

npm install

3. Configure Environment Variables

cp .env.example .env

Edit .env:

REACT_APP_API_URL=http://localhost:8000/api

Running Locally

Backend

Terminal 1: Start Django Development Server

cd backend
source venv/bin/activate
python manage.py runserver 0.0.0.0:8000

Terminal 2: Start Celery Worker

cd backend
source venv/bin/activate
celery -A config worker -l info

Terminal 3: Start Daphne (WebSocket Server)

cd backend
source venv/bin/activate
daphne -b 0.0.0.0 -p 8001 config.asgi:application

Frontend

Terminal 4: Start React Development Server

cd frontend
npm start

Access the application at http://localhost:3000

Redis

redis-server

Docker Deployment

Build Docker Images

Backend

cd backend
docker build -t productivity-tracker-backend .

Frontend

cd frontend
docker build -t productivity-tracker-frontend .

Docker Compose

docker-compose up -d

Run Migrations in Docker

docker exec productivity-tracker-backend python manage.py migrate
docker exec productivity-tracker-backend python manage.py createsuperuser

Production Deployment

AWS EC2 Deployment

1. Launch EC2 Instance

  • OS: Ubuntu 20.04 LTS
  • Instance Type: t3.medium or larger
  • Storage: 30GB SSD

2. Install Dependencies

sudo apt update
sudo apt install -y python3.9 python3-pip postgresql postgresql-contrib redis-server nodejs npm nginx supervisor

# Create Python virtual environment
python3 -m venv /home/ubuntu/venv
source /home/ubuntu/venv/bin/activate

3. Clone Repository

cd /home/ubuntu
git clone <repository-url>
cd Productivity-Tracking-System

4. Setup Backend

cd backend
pip install -r requirements.txt
python manage.py migrate
python manage.py collectstatic --noinput

5. Configure Supervisor

Create /etc/supervisor/conf.d/productivity-tracker.conf:

[program:django]
directory=/home/ubuntu/Productivity-Tracking-System/backend
command=/home/ubuntu/venv/bin/gunicorn config.wsgi:application --bind 0.0.0.0:8000 --workers 4
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/django.log

[program:celery]
directory=/home/ubuntu/Productivity-Tracking-System/backend
command=/home/ubuntu/venv/bin/celery -A config worker -l info
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/celery.log

[program:daphne]
directory=/home/ubuntu/Productivity-Tracking-System/backend
command=/home/ubuntu/venv/bin/daphne -b 0.0.0.0 -p 8001 config.asgi:application
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/daphne.log

6. Configure Nginx

Create /etc/nginx/sites-available/productivity-tracker:

upstream django {
    server 127.0.0.1:8000;
}

server {
    listen 80;
    server_name yourdomain.com;

    client_max_body_size 20M;

    location /static/ {
        alias /home/ubuntu/Productivity-Tracking-System/backend/staticfiles/;
    }

    location /media/ {
        alias /home/ubuntu/Productivity-Tracking-System/backend/media/;
    }

    location / {
        proxy_pass http://django;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /ws/ {
        proxy_pass http://127.0.0.1:8001;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Enable site:

sudo ln -s /etc/nginx/sites-available/productivity-tracker /etc/nginx/sites-enabled/
sudo systemctl restart nginx

7. Setup SSL with Let's Encrypt

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com

8. Setup Frontend

cd ../frontend
npm install
npm run build

Serve with Nginx or deploy to CDN.

Azure Deployment

1. Create App Service

az group create --name productivity-tracker-rg --location eastus
az appservice plan create --name productivity-plan --resource-group productivity-tracker-rg --sku B2 --is-linux
az webapp create --resource-group productivity-tracker-rg --plan productivity-plan --name productivity-tracker --runtime "PYTHON|3.9"

2. Configure App Settings

az webapp config appsettings set --resource-group productivity-tracker-rg --name productivity-tracker \
  --settings \
  DJANGO_SETTINGS_MODULE=config.settings \
  SECRET_KEY="your-secret-key" \
  DEBUG=False \
  ALLOWED_HOSTS="yourdomain.azurewebsites.net"

3. Deploy Code

az webapp deployment source config-zip --resource-group productivity-tracker-rg --name productivity-tracker --src path/to/code.zip

Environment Variables for Production

SECRET_KEY=<generate-with-django>
DEBUG=False
ALLOWED_HOSTS=yourdomain.com,www.yourdomain.com

DB_ENGINE=django.db.backends.postgresql
DB_NAME=productivity_prod
DB_USER=prod_user
DB_PASSWORD=<strong-password>
DB_HOST=<database-host>
DB_PORT=5432

REDIS_URL=redis://<redis-host>:6379/0

CORS_ALLOWED_ORIGINS=https://yourdomain.com,https://www.yourdomain.com

# Email Configuration
EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
EMAIL_HOST=smtp.gmail.com
EMAIL_PORT=587
EMAIL_HOST_USER=<your-email>
EMAIL_HOST_PASSWORD=<app-password>
EMAIL_USE_TLS=True

# AWS S3 (for media files)
AWS_ACCESS_KEY_ID=<your-key>
AWS_SECRET_ACCESS_KEY=<your-secret>
AWS_STORAGE_BUCKET_NAME=<bucket-name>
AWS_S3_REGION_NAME=us-east-1

Troubleshooting

Issue: Database Connection Error

# Check PostgreSQL is running
sudo systemctl status postgresql

# Check connection
psql -U postgres -h localhost -d productivity_db

Issue: Redis Connection Error

# Check Redis is running
redis-cli ping

# Should return: PONG

Issue: Static Files Not Loading

python manage.py collectstatic --noinput --clear

Issue: WebSocket Connection Failed

  • Ensure Daphne is running on port 8001
  • Check CORS configuration
  • Verify firewall allows WebSocket connections

Issue: CORS Errors

Update .env:

CORS_ALLOWED_ORIGINS=http://localhost:3000,https://yourdomain.com

Issue: Out of Memory

Reduce number of Gunicorn workers:

gunicorn config.wsgi:application --bind 0.0.0.0:8000 --workers 2

Useful Commands

# View logs
tail -f /var/log/django.log
tail -f /var/log/celery.log

# Restart services
sudo supervisorctl restart django celery daphne

# Check migrations
python manage.py showmigrations

# Create backup
pg_dump productivity_db > backup.sql

# Restore backup
psql productivity_db < backup.sql

Performance Optimization

Database Optimization

# Analyze and optimize tables
ANALYZE;
VACUUM ANALYZE;

# Check indexes
SELECT * FROM pg_stat_user_indexes;

Caching

Enable Redis caching in settings:

CACHES = {
    'default': {
        'BACKEND': 'django_redis.cache.RedisCache',
        'LOCATION': config('REDIS_URL'),
        'OPTIONS': {
            'CLIENT_CLASS': 'django_redis.client.DefaultClient',
        }
    }
}

Load Balancing

Use AWS ALB or Nginx to distribute traffic across multiple instances.

Monitoring & Logging

Setup ELK Stack (Elasticsearch, Logstash, Kibana)

  1. Deploy ELK stack
  2. Configure Django logging to send to Logstash
  3. Create dashboards in Kibana

AWS CloudWatch

LOGGING = {
    'handlers': {
        'watchtower': {
            'level': 'INFO',
            'class': 'watchtower.CloudWatchLogHandler',
            'log_group': 'productivity-tracker',
        },
    },
}

Backup Strategy

  1. Daily Database Backups

    0 2 * * * pg_dump productivity_db | gzip > /backups/db-$(date +\%Y\%m\%d).sql.gz
  2. Media Files Backup

    • Use AWS S3 with versioning enabled
    • Daily sync to secondary bucket
  3. Automated Backups

    • AWS RDS automated backups
    • Point-in-time recovery enabled

Support

For issues and questions:

  1. Check logs in /var/log/
  2. Review database schema
  3. Test API endpoints with Postman
  4. Check WebSocket connectivity