PHP was designed for the request-response cycle: process a request, return a response, clean up. This makes it a natural fit for serverless computing, where functions spin up per request and shut down when idle. The challenge has been runtime support—AWS Lambda and other serverless platforms did not natively support PHP.
In 2026, multiple solutions exist for running PHP serverlessly, each with different trade-offs.
AWS Lambda with Bref
Bref is the most mature solution for PHP on Lambda. It provides custom Lambda runtimes for PHP and integrates with the Serverless Framework.
Setup
1 | # Install Bref |
Configuration
1 | # serverless.yml |
How Bref works
Bref creates Lambda layers containing the PHP runtime (PHP binary, extensions, php-fpm). When a request arrives:
- Lambda starts the PHP-FPM process (cold start: ~400ms)
- Nginx-like layer proxies the HTTP request to PHP-FPM
- PHP processes the request normally (your existing code works)
- Response is returned through API Gateway
- The Lambda instance stays warm for ~5-15 minutes
Cold start optimization
1 | # Reduce cold starts by minimizing deployment package size |
1 | // Optimize the autoloader for Lambda |
| Optimization | Cold start impact |
|---|---|
| Optimized autoloader | -50ms |
| Remove dev dependencies | -100ms |
| Smaller deployment package | -50-200ms |
| Provisioned concurrency | Eliminates cold start |
| More memory (1024MB → 2048MB) | -100ms (more CPU) |
What works on Lambda
- Laravel (with
bref/laravel-bridge) - Symfony (with
bref/symfony-bridge) - Any framework using PSR-7/PSR-15
- Stateless PHP applications
What does not work
- File-based sessions (use Redis, DynamoDB, or cookies)
- File uploads to local disk (use S3)
- Persistent connections (WebSockets, long-polling)
- Background processes or cron (use SQS + Lambda or EventBridge)
Laravel Vapor
Vapor is Laravel’s first-party serverless platform, built on AWS:
1 | # Install Vapor CLI |
Vapor handles infrastructure automatically: Lambda functions, API Gateway, SQS queues, S3 storage, CloudFront CDN, RDS databases, and ElastiCache.
Vapor vs Bref
| Aspect | Vapor | Bref |
|---|---|---|
| Setup complexity | Minimal (managed) | Moderate (self-configured) |
| Framework support | Laravel only | Any PHP framework |
| Infrastructure | Managed by Vapor | You manage via serverless.yml |
| Cost | $39/month + AWS | AWS only |
| Customization | Limited | Full control |
| Queue handling | Built-in SQS bridge | Manual SQS configuration |
Edge computing with PHP
Edge computing runs code at CDN nodes closest to the user. Response times are 5-50ms instead of 100-500ms from a central server.
Cloudflare Workers (experimental)
PHP does not run natively on Cloudflare Workers (which use V8/JavaScript). However, experimental projects compile PHP to WebAssembly (WASM) for edge execution:
1 | // worker.js using php-wasm |
This is experimental. PHP-WASM has limitations: no filesystem, no network extensions, no database connections. It works for computation and template rendering, not for full application hosting.
Vercel Serverless Functions
Vercel supports PHP through community runtimes:
1 | // vercel.json |
1 | // api/hello.php |
Vercel PHP functions are limited compared to Lambda/Bref: fewer extensions, smaller payload limits, and shorter timeouts.
When serverless makes sense for PHP
Good use cases
- Variable traffic: Sites with unpredictable traffic (news sites, marketing pages) benefit from auto-scaling to zero.
- API endpoints: Stateless REST/GraphQL APIs map cleanly to Lambda functions.
- Scheduled tasks: Cron jobs that run for seconds, not minutes, are cheaper on Lambda than on a persistent server.
- Cost optimization at low traffic: A site with 1,000 requests/day costs pennies on Lambda vs $5-20/month for a server.
Bad use cases
- High-traffic sites: Above ~1M requests/month, a $20 VPS is cheaper than Lambda.
- WebSocket applications: Lambda has a 29-second timeout and no persistent connections.
- File-heavy applications: Local filesystem is ephemeral on Lambda.
- Legacy PHP applications: Code that assumes persistent state, file sessions, or specific server configuration is hard to port.
Cost comparison
Monthly cost for a PHP application handling 500K requests/month, averaging 200ms per request:
| Platform | Monthly cost | Notes |
|---|---|---|
| Lambda (Bref) | $4-8 | Free tier: 1M requests/month |
| Laravel Vapor | $39 + $4-8 AWS | Managed platform fee |
| DigitalOcean droplet | $6 | 1GB RAM, self-managed |
| Laravel Forge + DO | $12 + $6 | Managed server |
| AWS Fargate (container) | $15-30 | Auto-scaling containers |
At low traffic (<100K requests/month), serverless is cheapest. At high traffic (>2M requests/month), a persistent server wins.
Migration path
Step 1: Verify statelessness
1 | // Check for state that won't survive Lambda invocations |
Step 2: Test locally
1 | # Bref provides a local Docker environment |
Step 3: Deploy to staging
1 | serverless deploy --stage staging |
Step 4: Load test
1 | # Use k6, Artillery, or wrk to simulate traffic |
Step 5: Monitor cold starts
Watch CloudWatch metrics for Duration and Init Duration. If init times are too high, consider provisioned concurrency.
Common mistakes
Assuming serverless is always cheaper: At moderate-to-high traffic, serverless can cost 5-10x more than a VPS. Calculate costs at your expected traffic.
Ignoring cold starts: A 600ms cold start is unacceptable for an API powering a mobile app. Measure real cold starts, not best-case scenarios.
Using serverless for long-running processes: Lambda has a 15-minute timeout. Batch jobs and queue workers need different solutions.
Not reducing package size: A 100MB deployment package has 2x the cold start of a 20MB package. Remove dev dependencies, test files, and documentation.
FAQ
Can I run WordPress on Lambda?
Technically yes (with significant modification), but it is not practical. WordPress relies heavily on the filesystem and database queries that do not work well with Lambda’s ephemeral nature. Use traditional hosting for WordPress.
Is PHP-WASM production-ready?
No. It is an active research area with promising results but significant limitations. Use Lambda/Bref for production PHP serverless today.
Should I use Vapor or Bref?
If you use Laravel and want a managed experience, Vapor. If you want more control or use a different framework, Bref. Both use Lambda underneath.
Next steps
Start by running composer require bref/bref and deploying a simple PHP function to Lambda. Measure the cold start time with your actual dependencies. Then calculate the monthly cost at your expected traffic and compare it with traditional hosting.
For containerized PHP deployment (the alternative to serverless), the Docker guide covers production-ready Docker images. For understanding PHP performance in any deployment model, the performance optimization guide covers profiling and tuning.