Laravel Vapor enables you to develop fast, scalable and serverless PHP applications with AWS Lambda. It eliminates the DevOps management headache with its powerful serverless deployment platform designed specifically for Laravel applications.
It allows you to deploy your PHP applications on AWS Lambda with zero-server maintenance, high availability and automatic scaling. Here’s a complete guide on how to work with Laravel Vapor and create a serverless PHP setup.
What is Laravel Vapor?
It is an auto-scaling, serverless Laravel deployment platform that enables users to deploy applications. It manages infrastructure, scaling issues, or server patches.
Step-by-Step Guide: How to Deploy a Laravel App on Vapor
1. Prerequisites
- A Laravel application.
- An AWS account.
- A Laravel Vapor account.
- The Vapor CLI installed via Composer.
Bash:
composer global require laravel/vapor-cli
2. Login to Vapor
Authenticate your Vapor account:
Bash:
vapor login
3. Initialize Vapor in Your Project
Run this command in the root of your Laravel project:
Bash:
vapor init
This command creates a vapor.yml configuration file, where you define environments, databases, storage, queue workers, and more.
4. Configure vapor.yml
Here’s an example of a minimal vapor.yml file:
yaml
id: 1234
name: my-laravel-app
environments: production: memory: 1024 cli-memory: 512 runtime: php-8.3 database: my-database storage: my-storage queue: sqs
5. Deploy to Vapor
Once your config is ready, deploy your application:
Bash:
vapor deploy production
Vapor will package your Laravel app, upload it to AWS, and deploy it on Lambda.
6. Managing Assets and Queues
- Assets: You can use Laravel Mix and Vapor’s asset upload feature to push static assets to CloudFront.
Bash:
vapor assets
- Queues: Vapor supports Laravel queues via Amazon SQS or Lambda. Define queues in config/queue.php and configure workers in vapor.yml.
7. Use Serverless Features
With Vapor, your Laravel app benefits from:
- Automatic scaling with Lambda.
- Stateless request handling.
- On-demand queue workers.
- Built-in Redis and RDS support.
- Seamless CI/CD integrations.
How to Build a Serverless Contact Form with Laravel Vapor?
Let’s walk through a quick use case — deploying a contact form API endpoint serverlessly.
Step 1: Create a route and controller
PHP:
// routes/api.php
Route::post('/contact', 'ContactController@send');
// app/Http/Controllers/ContactController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
class ContactController extends Controller
{ public function send(Request $request) { Mail::raw("Message from: {$request->email}", function($msg) use ($request) { $msg->to('admin@example.com')->subject('New Contact Form Submission'); }); return response()->json(['message' => 'Email sent!']); }
}
Step 2: Deploy with Vapor
Ensure mail settings are configured in vapor.yml, then deploy:
Bash:
vapor deploy production
You now have a fully serverless contact form API deployed on AWS Lambda using Laravel Vapor.
Why Use Laravel Vapor for Serverless PHP?
- Zero Maintenance: No need to manage servers or infrastructure.
- Scalability: Instantly scales with traffic via AWS Lambda.
- Security: Managed AWS services with automatic updates.
- Efficiency: Faster deployments and simplified workflows.
Ready to Go Serverless with Laravel?
Hire PHP Laravel developers to develop scalable, secure and serverless applications. For startups or scaling enterprises, Laravel Vapor is a game-changer for modern PHP development.