PHP on Servers Without Servers: AWS Lambda for Web Developers

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

PHP and Serverless: An Unexpected Alliance

\n\n

When 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\n

Today 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\n

What is Serverless Really?

\n\n

Let'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\n

For 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\n

How Does PHP Work in a Serverless Environment?

\n\n

The 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.

\n\n

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\n

Bref: PHP Engine for AWS Lambda

\n\n

Bref 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\n

Installation and First Run

\n\n

To get started, you'll need:

\n
    \n
  • An AWS account
  • \n
  • Installed composer and aws-cli
  • \n
  • Node.js (for Serverless Framework)
  • \n
\n\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\n

Now let's configure the deployment. Create a file serverless.yml:

\n\n
# 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\n

Run the deployment:

\n
composer require bref/bref\nserverless deploy
\n\n

In 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!

\n\n

Alternatives to AWS Lambda for PHP

\n\n

Don't want to be tied to AWS? There are alternatives that also work well with PHP.

\n\n

Google Cloud Functions (with Adaptation)

\n\n

Google doesn't natively support PHP, but you can

Blogs

Book Recommendations