{
    "slug": "cloud_php_deployment",
    "term": "PHP Deployment on Cloud Platforms",
    "category": "cloud",
    "difficulty": "intermediate",
    "short": "Deployment options for PHP on AWS, GCP, and Azure — EC2/VMs, containers (ECS, Cloud Run), serverless (Lambda via Bref), and PaaS (Elastic Beanstalk, App Engine).",
    "long": "PHP cloud deployment options: EC2/VMs (full control, nginx + PHP-FPM, manual scaling), ECS/Fargate (containerised PHP-FPM, auto-scaling, no server management), Cloud Run/GCP (container on demand, scales to zero), Lambda + Bref (serverless PHP, pay per invocation, cold starts), Elastic Beanstalk/App Engine (PaaS, abstracts infrastructure). For most PHP web apps: ECS Fargate or Cloud Run offer the best balance — containers provide reproducibility, auto-scaling handles traffic spikes, and no server management is needed.",
    "aliases": [
        "PHP on AWS",
        "PHP serverless",
        "Bref",
        "ECS PHP"
    ],
    "tags": [
        "cloud",
        "php",
        "devops",
        "deployment"
    ],
    "misconception": "Serverless PHP (Lambda/Bref) is always the cheapest option — cold starts add latency, and consistent traffic is cheaper on containers; serverless excels at spiky, unpredictable workloads.",
    "why_it_matters": "Choosing the right deployment model determines scaling behaviour, cost, and operational complexity — a PHP app deployed on EC2 with manual scaling will go down under traffic spikes that ECS would handle automatically.",
    "common_mistakes": [
        "PHP sessions on multiple EC2 instances without sticky sessions or Redis — session data lost on each request.",
        "Local file storage on containers — containers are ephemeral; use S3 for file storage.",
        "Not setting PHP-FPM worker count for container memory — match pm.max_children to available RAM.",
        "Bref for long-running PHP processes — Lambda has a 15-minute timeout; use ECS for queue workers."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "containerisation",
        "docker_multistage",
        "serverless_functions",
        "cloud_storage_s3"
    ],
    "prerequisites": [
        "containerisation",
        "aws_fundamentals",
        "continuous_deployment"
    ],
    "refs": [
        "https://bref.sh/docs/",
        "https://aws.amazon.com/ecs/"
    ],
    "bad_code": "// PHP on EC2 with local file storage — breaks on scale:\n$uploadPath = '/var/www/html/uploads/' . $filename;\nmove_uploaded_file($_FILES['file']['tmp_name'], $uploadPath);\n// Works on single server\n// Behind load balancer: uploaded file only on one server\n// User request may hit different server — file not found",
    "good_code": "// PHP on ECS Fargate with S3 for files:\n// Dockerfile: FROM php:8.3-fpm-alpine\n// Files go to S3, not local filesystem:\n$s3->putObject([\n    'Bucket' => getenv('S3_BUCKET'),\n    'Key'    => 'uploads/' . $filename,\n    'Body'   => fopen($_FILES['file']['tmp_name'], 'r'),\n]);\n// Sessions in Redis — shared across all containers:\n// session.save_handler = redis\n// session.save_path = tcp://redis.internal:6379",
    "quick_fix": "Use ECS Fargate with CodeDeploy blue/green — push Docker image to ECR, update task definition, CodeDeploy shifts traffic and rolls back automatically on health check failure",
    "severity": "medium",
    "effort": "high",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/cloud_php_deployment",
        "html_url": "https://codeclaritylab.com/glossary/cloud_php_deployment",
        "json_url": "https://codeclaritylab.com/glossary/cloud_php_deployment.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[PHP Deployment on Cloud Platforms](https://codeclaritylab.com/glossary/cloud_php_deployment) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/cloud_php_deployment"
            }
        }
    }
}