Boost PHP Performance: Migrate Laravel to FrankenPHP Worker Mode
This article explains how replacing the traditional Apache/nginx stack with FrankenPHP’s worker mode can dramatically improve PHP application performance, outlines the drawbacks of the classic request cycle, and provides a step‑by‑step guide for migrating a Laravel project.
Introduction
Many developers overlook how code becomes interactive, and changing the HTTP server can have a huge impact on performance. Traditional PHP deployments rely on Apache or nginx, while FrankenPHP offers a simpler, faster alternative.
Problems with Traditional PHP Request Cycle
Each request starts from scratch with no shared state, avoiding cross‑request contamination but repeating costly operations such as loading class files, reading configuration, initializing the framework container, and loading routes, middleware, and service providers. This results in higher server load, slower responses, and increased hosting costs.
FrankenPHP – A Modern Application Server
FrankenPHP is built on the Caddy web server, written in Go, and embeds the PHP runtime directly into the server process. It is officially supported by the PHP Foundation, ensuring stability and long‑term backing.
Key features include automatic HTTPS certificate issuance, native HTTP/2 and HTTP/3 support, Early Hints for pre‑loading resources, graceful reload for zero‑downtime deployments, and, most importantly, the worker mode .
Worker Mode
In worker mode the application is started once and remains resident in memory; subsequent requests reuse the already loaded state, eliminating the framework bootstrap overhead and delivering dramatic speed gains.
Migrating a Laravel Application to FrankenPHP
Step‑by‑step example for Laravel:
composer require laravel/octane
php artisan octane:install
# Choose "frankenphp" as the server when prompted
php artisan octane:start --server=frankenphpThe migration requires no Docker or complex configuration, making the cost very low.
Flexible Deployment Options
Production: run the official standalone binary or use the official Docker image.
Local development: start directly with the Laravel Octane command for seamless workflow.
The standalone binary bundles the PHP runtime, Caddy, and the application into a single executable, ensuring version consistency and easy distribution.
Pitfalls
Because workers are long‑lived, static variables or static properties persist across requests, which can cause unexpected behavior. The following example demonstrates a static counter that increments across requests:
<?php
class RequestCounter {
public static int $count = 0;
public static function increment(): int {
return ++self::$count;
}
}To avoid issues, reset state on application startup or configure max_requests so workers restart after a set number of requests, similar to traditional PHP‑FPM behavior.
Conclusion
Traditional PHP restarts the entire application on every request, wasting CPU cycles.
FrankenPHP worker mode starts once, stays in memory, and yields huge performance improvements.
Laravel users can adopt it via the Octane package with almost zero configuration.
Be aware that static properties and global state persist between requests.
Set max_requests to restart workers periodically and prevent memory leaks.
FrankenPHP’s worker mode solves many long‑standing PHP performance pain points and is strongly recommended for projects that can adopt it.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
