|
| 1 | +# Deployment Guide |
| 2 | + |
| 3 | +This guide covers how to deploy myfy applications to production environments. |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## Quick Start |
| 8 | + |
| 9 | +For a basic production deployment: |
| 10 | + |
| 11 | +```bash |
| 12 | +# 1. Build frontend assets (if using FrontendModule) |
| 13 | +myfy frontend build |
| 14 | + |
| 15 | +# 2. Start production server |
| 16 | +myfy start --host 0.0.0.0 --port 8000 --workers 4 |
| 17 | +``` |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## Production Server |
| 22 | + |
| 23 | +### myfy start |
| 24 | + |
| 25 | +The `myfy start` command is optimized for production deployments: |
| 26 | + |
| 27 | +- **Automatic production mode**: Sets `MYFY_FRONTEND_ENVIRONMENT=production` |
| 28 | +- **No auto-reload**: Better performance and stability |
| 29 | +- **Asset verification**: Ensures frontend assets are built before starting |
| 30 | +- **Multi-worker support**: Uses gunicorn for horizontal scaling |
| 31 | + |
| 32 | +### Single Worker |
| 33 | + |
| 34 | +For simple deployments or development-like production: |
| 35 | + |
| 36 | +```bash |
| 37 | +myfy start --host 0.0.0.0 --port 8000 |
| 38 | +``` |
| 39 | + |
| 40 | +This uses uvicorn with a single worker process. |
| 41 | + |
| 42 | +### Multiple Workers |
| 43 | + |
| 44 | +For better performance and concurrency: |
| 45 | + |
| 46 | +```bash |
| 47 | +myfy start --host 0.0.0.0 --port 8000 --workers 4 |
| 48 | +``` |
| 49 | + |
| 50 | +This uses gunicorn with multiple uvicorn workers. Install gunicorn first: |
| 51 | + |
| 52 | +```bash |
| 53 | +pip install gunicorn |
| 54 | +# or |
| 55 | +uv add gunicorn |
| 56 | +``` |
| 57 | + |
| 58 | +**Worker count recommendations:** |
| 59 | +- CPU-bound apps: `(2 × CPU cores) + 1` |
| 60 | +- I/O-bound apps: `(4 × CPU cores) + 1` |
| 61 | +- Start with 4 workers and adjust based on monitoring |
| 62 | + |
| 63 | +--- |
| 64 | + |
| 65 | +## Frontend Assets |
| 66 | + |
| 67 | +If your application uses `FrontendModule`, you must build assets before deploying: |
| 68 | + |
| 69 | +```bash |
| 70 | +myfy frontend build |
| 71 | +``` |
| 72 | + |
| 73 | +This generates: |
| 74 | +- Minified JavaScript bundles |
| 75 | +- Minified CSS files |
| 76 | +- Asset manifest with content hashes |
| 77 | +- Files in `frontend/static/dist/` |
| 78 | + |
| 79 | +**Important:** The `myfy start` command will verify that assets are built and fail if missing. |
| 80 | + |
| 81 | +--- |
| 82 | + |
| 83 | +## Environment Variables |
| 84 | + |
| 85 | +### Core Settings |
| 86 | + |
| 87 | +```bash |
| 88 | +# Frontend environment (auto-set by myfy start) |
| 89 | +MYFY_FRONTEND_ENVIRONMENT=production |
| 90 | + |
| 91 | +# Web server settings |
| 92 | +MYFY_WEB_HOST=0.0.0.0 |
| 93 | +MYFY_WEB_PORT=8000 |
| 94 | +``` |
| 95 | + |
| 96 | +### Frontend Settings |
| 97 | + |
| 98 | +```bash |
| 99 | +# Static file serving |
| 100 | +MYFY_FRONTEND_STATIC_URL_PREFIX=/static |
| 101 | + |
| 102 | +# Cache control |
| 103 | +MYFY_FRONTEND_CACHE_MAX_AGE=31536000 # 1 year default |
| 104 | +``` |
| 105 | + |
| 106 | +See [Configuration Reference](./api-reference/core.md) for all available settings. |
| 107 | + |
| 108 | +--- |
| 109 | + |
| 110 | +## Deployment Options |
| 111 | + |
| 112 | +### Docker |
| 113 | + |
| 114 | +Create a `Dockerfile`: |
| 115 | + |
| 116 | +```dockerfile |
| 117 | +FROM python:3.12-slim |
| 118 | + |
| 119 | +WORKDIR /app |
| 120 | + |
| 121 | +# Install system dependencies |
| 122 | +RUN apt-get update && apt-get install -y \ |
| 123 | + nodejs npm \ |
| 124 | + && rm -rf /var/lib/apt/lists/* |
| 125 | + |
| 126 | +# Copy application code |
| 127 | +COPY . . |
| 128 | + |
| 129 | +# Install Python dependencies |
| 130 | +RUN pip install -e . |
| 131 | +RUN pip install gunicorn |
| 132 | + |
| 133 | +# Build frontend assets |
| 134 | +RUN myfy frontend build |
| 135 | + |
| 136 | +# Expose port |
| 137 | +EXPOSE 8000 |
| 138 | + |
| 139 | +# Start production server |
| 140 | +CMD ["myfy", "start", "--host", "0.0.0.0", "--port", "8000", "--workers", "4"] |
| 141 | +``` |
| 142 | + |
| 143 | +Build and run: |
| 144 | + |
| 145 | +```bash |
| 146 | +docker build -t myfy-app . |
| 147 | +docker run -p 8000:8000 myfy-app |
| 148 | +``` |
| 149 | + |
| 150 | +### Docker Compose |
| 151 | + |
| 152 | +Create a `docker-compose.yml`: |
| 153 | + |
| 154 | +```yaml |
| 155 | +version: '3.8' |
| 156 | + |
| 157 | +services: |
| 158 | + web: |
| 159 | + build: . |
| 160 | + ports: |
| 161 | + - "8000:8000" |
| 162 | + environment: |
| 163 | + - MYFY_FRONTEND_ENVIRONMENT=production |
| 164 | + restart: unless-stopped |
| 165 | +``` |
| 166 | +
|
| 167 | +Run: |
| 168 | +
|
| 169 | +```bash |
| 170 | +docker-compose up -d |
| 171 | +``` |
| 172 | + |
| 173 | + |
| 174 | +## Health Checks |
| 175 | + |
| 176 | +Add a health check endpoint to your application: |
| 177 | + |
| 178 | +```python |
| 179 | +from myfy.web import route |
| 180 | + |
| 181 | +@route.get("/health") |
| 182 | +async def health_check(): |
| 183 | + return {"status": "healthy"} |
| 184 | +``` |
| 185 | + |
| 186 | +Configure your load balancer or orchestrator to check this endpoint. |
| 187 | + |
| 188 | +--- |
| 189 | + |
| 190 | +## Monitoring |
| 191 | + |
| 192 | +### Logging |
| 193 | + |
| 194 | +myfy uses Python's standard logging. Configure it in your application: |
| 195 | + |
| 196 | +```python |
| 197 | +import logging |
| 198 | + |
| 199 | +logging.basicConfig( |
| 200 | + level=logging.INFO, |
| 201 | + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s' |
| 202 | +) |
| 203 | +``` |
| 204 | +--- |
| 205 | + |
| 206 | +## Troubleshooting |
| 207 | + |
| 208 | +### Frontend assets not found |
| 209 | + |
| 210 | +**Error:** "Frontend assets not built" |
| 211 | + |
| 212 | +**Solution:** Run `myfy frontend build` before starting the server. |
| 213 | + |
| 214 | +### Gunicorn not found |
| 215 | + |
| 216 | +**Error:** "gunicorn not installed" |
| 217 | + |
| 218 | +**Solution:** Install gunicorn: |
| 219 | +```bash |
| 220 | +pip install gunicorn |
| 221 | +``` |
| 222 | + |
| 223 | +### Port already in use |
| 224 | + |
| 225 | +**Error:** "Address already in use" |
| 226 | + |
| 227 | +**Solution:** Change the port or stop the conflicting service: |
| 228 | +```bash |
| 229 | +myfy start --port 8080 |
| 230 | +``` |
| 231 | + |
| 232 | +### Worker timeout |
| 233 | + |
| 234 | +If workers are timing out, increase the timeout: |
| 235 | +```bash |
| 236 | +gunicorn app:app \ |
| 237 | + --worker-class uvicorn.workers.UvicornWorker \ |
| 238 | + --timeout 120 |
| 239 | +``` |
| 240 | + |
| 241 | +--- |
| 242 | + |
| 243 | +## Next Steps |
| 244 | + |
| 245 | +- [CLI Reference](./api-reference/cli.md) - Full CLI command reference |
| 246 | +- [Configuration](./api-reference/core.md) - All configuration options |
| 247 | +- [Frontend Module](./modules/frontend.md) - Frontend integration guide |
0 commit comments