PHP and Serverless: An Unexpected Alliance
\n\nWhen we hear "Serverless," the first associations are JavaScript, Python, or Go. PHP in this context seems like a "dinosaur" stuck in the era of shared hosting and monolithic WordPress. But let's face the truth: PHP is alive, well, and more flexible than you think.
\n\nToday we'll break down how to run PHP code in AWS Lambda and its alternatives. You'll learn why serverless PHP is not magic, but a working tool for APIs, webhook handlers, and microservices. Spoiler: you won't have to give up your favorite language.
\n\nWhat is Serverless Really?
\n\nLet's dispel the myth right away. "Serverless" does not mean "without a server." It means you don't think about the server. The platform (AWS, Google Cloud, Vercel) manages the infrastructure itself: scaling, updating, fixing. You just upload your code and pay for its execution time.
\n\nFor PHP, this sounds like a challenge. PHP traditionally requires a web server (Apache, Nginx) and the PHP-FPM module. But technology doesn't stand still.
\n\nHow Does PHP Work in a Serverless Environment?
\n\nThe key problem: Lambda functions (and their equivalents) are stateless containers that live for a few seconds or minutes. They don't "hold" a session in memory or run Apache. Therefore, a standard PHP script that waits for $_GET or writes to $_SESSION needs to be adapted.
The solution came in the form of special runtimes. They transform a Lambda event (e.g., an HTTP request from API Gateway) into a familiar PHP context. The most popular tool for this is Bref.
\n\nBref: PHP Engine for AWS Lambda
\n\nBref is an open-source project that allows you to run PHP on AWS Lambda "out of the box." It comes with ready-made layers that include PHP 8.x and popular extensions (PDO, Redis, gd, mbstring).
\n\nInstallation and First Run
\n\nTo get started, you'll need:
\n- \n
- An AWS account \n
- Installed
composerandaws-cli\n - Node.js (for Serverless Framework) \n
Let's create a simple function that returns a JSON with the current time:
\n\n// index.php\n<?php\nrequire __DIR__ . '/vendor/autoload.php';\n\nuse Bref\\Context\\Context;\nuse Bref\\Event\\Http\\HttpHandler;\n\nclass TimeHandler extends HttpHandler\n{\n public function handleRequest($request, Context $context): array\n {\n return [\n 'statusCode' => 200,\n 'headers' => ['Content-Type' => 'application/json'],\n 'body' => json_encode([\n 'server_time' => date('Y-m-d H:i:s'),\n 'php_version' => PHP_VERSION,\n 'message' => 'Hello from Serverless PHP!'\n ]),\n ];\n }\n}\n\nreturn new TimeHandler();\n\nNow let's configure the deployment. Create a file serverless.yml:
# serverless.yml\nservice: php-time-api\n\nprovider:\n name: aws\n region: eu-central-1\n runtime: provided.al2\n\nplugins:\n - ./vendor/bref/bref\n\nfunctions:\n api:\n handler: index.php\n timeout: 28\n layers:\n - ${bref:layer.php-81}\n events:\n - httpApi: 'GET /time'\n\nRun the deployment:
\ncomposer require bref/bref\nserverless deploy\n\nIn a minute, you'll get a URL like: https://xxx.execute-api.eu-central-1.amazonaws.com/time. Open it in your browser — you'll see JSON. Congratulations, you've just run a serverless PHP function on AWS Lambda!
Alternatives to AWS Lambda for PHP
\n\nDon't want to be tied to AWS? There are alternatives that also work well with PHP.
\n\nGoogle Cloud Functions (with Adaptation)
\n\nGoogle doesn't natively support PHP, but you can