Part 1: Initial Setup in Coolify Dashboard
Step 1: Accessing Coolify
- Open your browser and navigate to your Coolify instance (e.g.,
https://coolify.yourdomain.com) - Log in with your credentials
- You'll see the main dashboard with "Projects" and "Resources" sections
Step 2: Creating a New Project
- Click the "New Project" button in the top right corner
- Fill in the form:
- Name: "Django-High-Performance"
- Description: "High-performance Django application with PostgreSQL"
- Click "Create Project"
Step 3: Setting up PostgreSQL Database
- In your project, click "New Resource"
- Select "Database" from the resource types
- Choose "PostgreSQL"
- Fill in the configuration:
Name: postgres-main Version: 15 Username: django_user Password: [generate a secure password] Database Name: django_db Port: 5432
- Under "Advanced Settings":
Memory Limit: 12288 (12GB in MB) CPU Limit: 4
- Click "Create Database"
- After creation, click on the database resource
- Go to "Settings" tab
- Under "Configuration", add these settings:
max_connections = 200 shared_buffers = 4GB effective_cache_size = 12GB maintenance_work_mem = 1GB
- Click "Save Configuration" and "Restart Database"
Step 4: Setting up PgBouncer
- Click "New Resource"
- Select "Service" → "Docker"
- Configuration:
Name: pgbouncer Image: edoburu/pgbouncer:1.18.0 Port: 6432
- Click "Add Volume" and add:
Host Path: /data/pgbouncer/pgbouncer.ini Container Path: /etc/pgbouncer/pgbouncer.ini
- Add another volume:
Host Path: /data/pgbouncer/userlist.txt Container Path: /etc/pgbouncer/userlist.txt
- Under "Environment Variables", add:
DB_HOST=postgres-main DB_PORT=5432 DB_USER=django_user DB_PASSWORD=[your-postgres-password]
- Click "Create Service"
Step 5: Setting up Redis
- Click "New Resource"
- Select "Database" → "Redis"
- Configure:
Name: redis-cache Version: 7 Password: [generate secure password] Memory Limit: 4096 (4GB in MB) CPU Limit: 2
- Click "Create Redis"
Part 2: Preparing Your Application
Step 1: Project Files Setup
Create these files in your Django project:
Dockerfile:
FROM python:3.11-slim
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
WORKDIR /app
RUN apt-get update && apt-get install -y \
build-essential \
postgresql-client \
libpq-dev \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt \
gunicorn[gevent] \
redis \
hiredis \
django-redis \
psycopg2-binary
COPY . .
RUN python manage.py collectstatic --noinput
RUN useradd -m myuser
RUN chown -R myuser:myuser /app
USER myuser
CMD ["gunicorn", "--config", "gunicorn.conf.py", "core.wsgi:application"]
gunicorn.conf.py:
import multiprocessing workers = multiprocessing.cpu_count() * 2 + 1 worker_class = 'gevent' worker_connections = 1000 timeout = 30 keepalive = 2 max_requests = 1000 max_requests_jitter = 200 preload_app = True bind = "0.0.0.0:8000" # Logging accesslog = '-' errorlog = '-' loglevel = 'info'
Step 2: Update Django Settings
Update settings.py:
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = os.getenv('DJANGO_SECRET_KEY')
DEBUG = os.getenv('DJANGO_DEBUG', 'False') == 'True'
ALLOWED_HOSTS = os.getenv('DJANGO_ALLOWED_HOSTS', '').split(',')
# Database
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.getenv('POSTGRES_DB'),
'USER': os.getenv('POSTGRES_USER'),
'PASSWORD': os.getenv('POSTGRES_PASSWORD'),
'HOST': os.getenv('POSTGRES_HOST'),
'PORT': os.getenv('POSTGRES_PORT'),
'CONN_MAX_AGE': 0,
}
}
# Cache
CACHES = {
'default': {
'BACKEND': 'django_redis.cache.RedisCache',
'LOCATION': os.getenv('REDIS_URL'),
'OPTIONS': {
'CLIENT_CLASS': 'django_redis.client.DefaultClient',
'PARSER_CLASS': 'redis.connection.HiredisParser',
'CONNECTION_POOL_CLASS_KWARGS': {
'max_connections': 50,
'timeout': 20,
}
}
}
}
# Session
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
SESSION_CACHE_ALIAS = 'default'
Part 3: Deploying the Application
Step 1: Creating the Application Service
- In Coolify dashboard, click "New Resource"
- Select "Application" → "Docker"
- Select your Git repository
- Configure Build:
Branch: main (or your preferred branch) Dockerfile Path: ./Dockerfile Port: 8000
- Under "Advanced Settings":
Memory Limit: 8192 (8GB in MB) CPU Limit: 8 Instance Count: 2
Step 2: Environment Variables
- Click on "Environment Variables"
- Add the following (click "Add" after each):
# Django DJANGO_SECRET_KEY=[generate a secure key] DJANGO_DEBUG=False DJANGO_ALLOWED_HOSTS=your-domain.com DJANGO_CSRF_TRUSTED_ORIGINS=https://your-domain.com # Database POSTGRES_DB=django_db POSTGRES_USER=django_user POSTGRES_PASSWORD=[your-postgres-password] POSTGRES_HOST=pgbouncer POSTGRES_PORT=6432 # Redis REDIS_URL=redis://:[redis-password]@redis-cache:6379/0 # Port PORT=8000
Step 3: Network Configuration
- Go to "Network" tab
- Click "Add to Network"
- Select the network containing your database and Redis
- Click "Add"
Step 4: Initial Deployment
- Click "Deploy" button
- Watch the build logs (click "View Logs")
- Wait for the build to complete
Step 5: Database Migration
- After successful deployment, click "Terminal"
- Run:
python manage.py migrate python manage.py createsuperuser
Part 4: Post-Deployment Configuration
Step 1: Domain Setup
- Go to "Settings" tab
- Under "Domains", click "Add Domain"
- Enter your domain
- Enable "HTTPS/SSL"
- Choose "Let's Encrypt"
- Click "Add Domain"
Step 2: Monitoring Setup
- Go to "Monitoring" tab
- Enable "Resource Monitoring"
- Configure alerts:
- Click "Add Alert"
- Set CPU Usage > 80%
- Add email notification
- Repeat for:
- Memory Usage > 80%
- Error Rate > 1%
Common Issues and Solutions
Problem 1: Database Connection Failed
If you see "could not connect to database" errors:
- Check network connectivity:
# In application terminal nc -zv pgbouncer 6432
- Verify environment variables:
# In application terminal env | grep POSTGRES
- Check PgBouncer logs:
- Go to PgBouncer service
- Click "Logs"
Problem 2: Redis Connection Issues
If cache errors occur:
- Verify Redis connection:
# In Django shell from django.core.cache import cache cache.set('test', 'working') cache.get('test') - Check Redis URL format
- Verify network connectivity
Problem 3: Static Files Not Loading
- Check STATIC_ROOT setting
- Run collectstatic again:
python manage.py collectstatic --clear --noinput
- Verify whitenoise configuration
Performance Monitoring
Step 1: Database Monitoring
Run these queries in PostgreSQL:
-- Current connections SELECT count(*) FROM pg_stat_activity; -- Connection states SELECT state, count(*) FROM pg_stat_activity GROUP BY state; -- Slow queries SELECT pid, age(clock_timestamp(), query_start), usename, query FROM pg_stat_activity WHERE state != 'idle' AND query NOT ILIKE '%pg_stat_activity%' ORDER BY query_start desc;
Step 2: Application Monitoring
- Check Gunicorn workers:
# In application terminal ps aux | grep gunicorn
- Monitor request latency:
- Go to "Metrics" tab
- Look for "Request Duration"
- Check "95th percentile" latency
No comments:
Post a Comment