PHP has been powering the web for decades. From WordPress to Laravel, it remains one of the most popular backend languages on the planet. However, as serverless computing gained popularity, PHP was frequently overlooked in favor of Node.js, Python, and Go.
That story has altered.
PHP has become a first-class citizen of the serverless world thanks to AWS Lambda and Bref. You can run modern PHP applications without managing servers, scale automatically, and pay just for what you need—all while maintaining your existing PHP skills and frameworks.
In this post, we’ll explore:
- What serverless PHP really means
- How Bref makes it possible
- A practical setup walkthrough
- When serverless PHP does and doesn’t make sense
- Performance, cold starts, and best practices
Let’s dive in.
What Is Serverless (Really)?
Before touching PHP, let’s clear a common misconception.
Serverless does not mean “no servers.”
It means you don’t manage them.
With AWS Lambda:
- No provisioning EC2 instances
- No patching OS updates
- No capacity planning
- Automatic scaling
- Pay-per-request pricing
You do not install servers, but functions. The rest is handled by Amazon Web Services.
This was not previously possible for PHP developers until Bref.
Why PHP Was Late to Serverless
AWS Lambda initially supported:
- Node.js
- Python
- Java
- Go
- Ruby
PHP was missing because:
- Lambda requires a custom runtime
- PHP traditionally assumes long-running processes
- The ecosystem lacked official tooling
Bref solved all of this by:
- Providing production-ready PHP runtimes
- Handling Lambda’s runtime interface
- Integrating seamlessly with modern PHP frameworks
PHP is still highly relevant for building scalable, dynamic apps—see why PHP is a top choice for e-commerce development
What Is Bref?
Bref is an open-source framework that allows you to run PHP applications on AWS Lambda.
It provides:
- Custom PHP runtimes (PHP 8.x supported)
- Native Lambda integrations
- HTTP handling via API Gateway
- CLI tooling and deployment helpers
Think of Bref as the bridge between PHP and AWS Lambda.
Architecture Overview
A typical serverless PHP setup looks like this:
Client
↓
API Gateway
↓
AWS Lambda (PHP via Bref)
↓
RDS / DynamoDB / S3 / External APIs
Each request spins up (or reuses) a Lambda container that runs your PHP code.
Setting Up a Serverless PHP Project with Bref
Let’s walk through a real setup.
1. Prerequisites
You’ll need:
- PHP 8.1+
- Composer
- AWS account
- AWS CLI configured
- Docker (for local builds)
2. Create a New Project
composer create-project bref/bref my-serverless-app
cd my-serverless-app
This gives you:
- A basic Lambda handler
- Serverless Framework config
- PHP runtime configuration
3. Install Bref Runtime
composer require bref/bref
This pulls in:
- PHP runtime layers
- Lambda integrations
- Deployment helpers
4. Configure serverless.yml
This file defines your entire infrastructure.
service: php-serverless-app
provider:
name: aws
region: ap-south-1
runtime: provided.al2
plugins:
- ./vendor/bref/bref
functions:
api:
handler: public/index.php
layers:
- ${bref:layer.php-81}
events:
- httpApi: '*'
Key points:
provided.al2 tells Lambda we’re using a custom runtime
Bref injects the PHP binary via Lambda Layers
httpApi connects API Gateway to PHP
5. Your PHP Entry Point
In public/index.php:
<?php
echo json_encode([
'message' => 'Hello from Serverless PHP!',
'time' => date('c'),
]);
That’s it. No Apache. No Nginx. No FPM.
6. Deploy to AWS
vendor/bin/serverless deploy
After deployment, you’ll get:
- An HTTPS endpoint
- Auto-scaling Lambda function
- Pay-per-request billing
Learn more about PHP & It's Trending Frameworks
Using Frameworks Like Laravel or Symfony
Yes, you can run full frameworks.
Laravel on Lambda
- Use Laravel Octane-style optimizations
- Cache config and routes
- Avoid runtime file writes
- Use S3 for storage
Symfony on Lambda
- Use HTTP kernel as handler
- Precompile container
- Disable debug mode
Bref officially supports both and provides documentation for best practices.
Performance and Cold Starts
Let’s address the elephant in the room.
Cold Starts Explained
A cold start happens when:
- No Lambda container is available
- AWS spins up a new one
- PHP runtime initializes
For PHP:
- Cold starts are usually 100–500ms
- Warm requests are very fast
How to Reduce Cold Starts
- Use smaller dependencies
- Enable OPcache
- Avoid heavy bootstrap logic
- Use provisioned concurrency (for critical APIs)
For most APIs, cold starts are a non-issue.
When Serverless PHP Is a Great Choice
Serverless PHP shines when:
✅ You have spiky or unpredictable traffic
✅ You want zero server maintenance
✅ You already know PHP
✅ You’re building APIs, microservices, or web backends
✅ You want cost efficiency at low-to-medium scale
When You Should NOT Use It
Serverless PHP may not be ideal if:
❌ You need long-running processes
❌ You rely heavily on local filesystem writes
❌ You require ultra-low latency at all times
❌ You have legacy apps tightly coupled to servers
Serverless is a tool—not a silver bullet.
Cost Considerations
Lambda pricing is based on:
- Number of requests
- Execution duration
- Memory allocation
For many PHP apps:
- Costs are significantly lower than EC2
- Especially for low or bursty traffic
Real-world examples often run on single-digit USD/month.
Best Practices for Production
- Cache aggressively (Redis, DynamoDB, or HTTP caching)
- Use environment variables, not .env files
- Monitor with CloudWatch
- Log structured JSON
- Keep functions small and focused
Conclusion
Serverless PHP is no longer experimental.
With AWS Lambda and Bref:
- PHP joins the modern cloud-native world
- You get scalability without complexity
- You write PHP, not infrastructure code
If you’re a PHP developer who’s been ignoring serverless because “PHP isn’t supported,” it’s time to revisit that assumption.
Serverless PHP is not the future, it’s already here.
Top comments (0)