optimize-docker-php-fpm

Optimizes PHP-FPM configuration in Docker containers. Tunes process manager, request handling, and resource allocation for production workloads.

59 stars

Best use case

optimize-docker-php-fpm is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Optimizes PHP-FPM configuration in Docker containers. Tunes process manager, request handling, and resource allocation for production workloads.

Teams using optimize-docker-php-fpm should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/optimize-docker-php-fpm/SKILL.md --create-dirs "https://raw.githubusercontent.com/dykyi-roman/awesome-claude-code/main/skills/optimize-docker-php-fpm/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/optimize-docker-php-fpm/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How optimize-docker-php-fpm Compares

Feature / Agentoptimize-docker-php-fpmStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Optimizes PHP-FPM configuration in Docker containers. Tunes process manager, request handling, and resource allocation for production workloads.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# PHP-FPM Configuration Optimization

Provides production-ready PHP-FPM tuning for Docker containers based on workload type and available resources.

## Process Manager Modes

```
┌─────────────────────────────────────────────────────────────────┐
│                  PHP-FPM PROCESS MANAGERS                        │
├─────────────────────────────────────────────────────────────────┤
│  STATIC:    [W][W][W][W][W][W][W][W]   Fixed pool              │
│             Best for: dedicated containers, predictable load    │
│  DYNAMIC:   [W][W][W][W][ ][ ][ ][ ]   Scales up/down         │
│             Best for: variable load, shared resources           │
│  ONDEMAND:  [ ][ ][ ][ ][ ][ ][ ][ ]   Starts on request      │
│             Best for: low-traffic, dev environments             │
└─────────────────────────────────────────────────────────────────┘
```

## Memory Calculation Formula

```
max_children = (available_memory - system_overhead) / avg_worker_memory

Where:
  available_memory = container_memory_limit
  system_overhead  = ~50MB (OS + PHP-FPM master + nginx)
  avg_worker_memory = 30-60MB (depends on application)
```

| Container Memory | System Overhead | Worker Memory | max_children |
|-----------------|-----------------|---------------|-------------|
| 256MB | 50MB | 40MB | 5 |
| 512MB | 50MB | 40MB | 11 |
| 1024MB | 50MB | 40MB | 24 |
| 2048MB | 50MB | 50MB | 39 |

Measure actual worker memory:

```bash
ps aux | grep "php-fpm: pool" | awk '{sum+=$6; n++} END {print sum/n/1024 " MB"}'
```

## Configuration by Workload

### API Workload (Many Fast Requests)

```ini
[www]
pm = static
pm.max_children = 20
request_terminate_timeout = 30s
request_slowlog_timeout = 5s
pm.max_requests = 1000
slowlog = /proc/self/fd/2
```

### Worker Workload (Few Long Requests)

```ini
[www]
pm = dynamic
pm.max_children = 8
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 4
request_terminate_timeout = 300s
request_slowlog_timeout = 30s
pm.max_requests = 200
```

### Mixed Workload

```ini
[www]
pm = dynamic
pm.max_children = 15
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 8
request_terminate_timeout = 60s
request_slowlog_timeout = 10s
pm.max_requests = 500
```

## Dynamic Mode Formulas

```
pm.start_servers     = (min_spare + max_spare) / 2
pm.min_spare_servers = max_children * 0.25
pm.max_spare_servers = max_children * 0.75
```

## pm.max_requests by Application Type

| Application Type | Recommended Value |
|-----------------|-------------------|
| Stateless API | 1000-5000 |
| Framework with ORM | 500-1000 |
| Legacy application | 100-500 |
| Memory-intensive processing | 50-200 |

## Health Check and Monitoring

```ini
pm.status_path = /status
ping.path = /ping
ping.response = pong
```

```dockerfile
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
    CMD php-fpm-healthcheck || exit 1
```

Key status metrics to monitor:
- **active processes:** current load
- **listen queue:** requests waiting (should be 0)
- **max children reached:** need more workers
- **slow requests:** performance issues

## Docker-Specific Settings

```ini
; Logging to stdout/stderr
error_log = /proc/self/fd/2
access.log = /proc/self/fd/2
clear_env = yes
catch_workers_output = yes
decorate_workers_output = no
```

## Before/After Comparison

| Metric | Default | Optimized (static) | Improvement |
|--------|---------|---------------------|-------------|
| max_children | 5 | 20 | +300% capacity |
| Idle memory (512MB) | 200MB wasted | ~50MB overhead | -75% waste |
| Request latency (p99) | 500ms | 150ms | -70% |
| Throughput (req/s) | 50 | 200 | +300% |
| Memory leak recovery | Never | Every 1000 req | Predictable |

## Complete Production Configuration

```ini
[global]
error_log = /proc/self/fd/2
log_level = warning
daemonize = no

[www]
user = www-data
group = www-data
listen = 0.0.0.0:9000
pm = static
pm.max_children = 20
pm.max_requests = 1000
request_terminate_timeout = 30s
request_slowlog_timeout = 5s
slowlog = /proc/self/fd/2
pm.status_path = /status
ping.path = /ping
ping.response = pong
clear_env = yes
catch_workers_output = yes
decorate_workers_output = no
access.log = /proc/self/fd/2
access.format = "%R - %u %t \"%m %r\" %s %{mili}dms %{mega}MMB %C%%"
```

## Generation Instructions

1. **Determine workload type:** API, Worker, or Mixed
2. **Calculate max_children:** Based on container memory and worker memory
3. **Select PM mode:** Static for production, dynamic for variable load
4. **Configure timeouts:** Based on expected request duration
5. **Set max_requests:** Based on application memory behavior
6. **Enable monitoring:** Status page, health check, slow log

Related Skills

optimize-docker-startup

59
from dykyi-roman/awesome-claude-code

Optimizes Docker container startup time for PHP applications. Reduces initialization overhead through preloading, caching, and entrypoint optimization.

optimize-docker-opcache

59
from dykyi-roman/awesome-claude-code

Optimizes OPcache configuration for PHP Docker containers. Configures memory, file limits, JIT, and validation for production and development.

optimize-docker-layers

59
from dykyi-roman/awesome-claude-code

Analyzes and optimizes Docker layer caching for PHP projects. Identifies layer ordering issues, cache invalidation problems, and provides recommendations for faster builds.

optimize-docker-image-size

59
from dykyi-roman/awesome-claude-code

Optimizes Docker image size for PHP projects. Reduces image footprint through Alpine, multi-stage builds, layer cleanup, and dependency minimization.

optimize-docker-compose-resources

59
from dykyi-roman/awesome-claude-code

Optimizes Docker Compose resource allocation for PHP stacks. Configures memory limits, CPU constraints, and service scaling.

optimize-docker-build-time

59
from dykyi-roman/awesome-claude-code

Optimizes Docker build time for PHP projects. Analyzes layer caching, BuildKit features, parallel builds, and dependency installation.

docker-troubleshooting-knowledge

59
from dykyi-roman/awesome-claude-code

Docker troubleshooting knowledge base. Provides debugging patterns, common error solutions, and diagnostic commands for PHP containers.

docker-security-knowledge

59
from dykyi-roman/awesome-claude-code

Docker security knowledge base for PHP. Provides hardening patterns, vulnerability scanning, secrets management, and OWASP container guidelines.

docker-scanning-knowledge

59
from dykyi-roman/awesome-claude-code

Docker image scanning knowledge base. Provides vulnerability detection, compliance checking, and SBOM generation for PHP container images.

docker-production-knowledge

59
from dykyi-roman/awesome-claude-code

Docker production knowledge base for PHP. Provides deployment patterns, health checks, graceful shutdown, logging, and monitoring.

docker-php-extensions-knowledge

59
from dykyi-roman/awesome-claude-code

Docker PHP extensions knowledge base. Provides installation patterns for common extensions, build dependency management, and PECL usage.

docker-orchestration-knowledge

59
from dykyi-roman/awesome-claude-code

Docker orchestration knowledge base. Provides patterns for Swarm, Kubernetes basics, service scaling, and load balancing for PHP.