The Easiest Approach to Increase Laravel Performance: The FrankenPHP
Keep developing with PHP, trust me.
As we know, Laravel is the most popular PHP Framework (behind WordPress, actually) as per StackOverflow Developer Survey 2024:
PHP is often considered slow compared to other languages like Go or JS (Node.js) because it was originally designed as a synchronous, interpreted language. Each PHP request starts a new process, which reads, parses, and executes the script, then shuts down, resulting in significant overhead on every request. This “start from scratch” model leads to longer execution times, especially when handling larger applications or complex logic.
Additionally, PHP’s synchronous nature means that each request must wait for tasks like database connection or file I/O to complete before continuing, causing bottlenecks in high-concurrency environments. While tools like PHP-FPM help mitigate some of these issues by handling multiple processes, the way PHP-FPM uses worker processes for each request still affects performance, making PHP less efficient for certain high-performance tasks compared to asynchronous alternatives. That is the usual way of running the Laravel App.
So let me tell you that there’s an alternative for running it faster, known as FrankenPHP:
FrankenPHP is a modern application server for PHP built on top of the Caddy web server, written in Go. On the other hand, Laravel officially supports FrankenPHP via Laravel Octane, which will supercharge the Laravel application’s performance by serving our application using high-powered application servers, including FrankenPHP. The Laravel Octane works by booting the Laravel application once, keeping it in memory, and then feeding it requests at supersonic speeds. There are more other than FrankenPHP that are supported by Laravel Octane, but the easiest setup falls to FrankenPHP for me.
How to Install
What we have to do is install the Laravel Octane package with the following:
composer require laravel/octane
And then install it to Laravel using:
php artisan octane:install --server=frankenphp
After installing it, we will be asked to install FrankenPHP if FrankenPHP is not found. Accept it, and FrankenPHP will be installed:
After that, we could just run the Laravel Octane to serve our application using:
php artisan octane:frankenphp --port=8080 --host=0.0.0.0
That’s all. Now, we can access the Laravel App from the defined port. Wait, A PORT? Right, port for the app. In this phase, you would have 2 options for exposing our app publicly.
- Our domain/IP has to be forwarded to that port (whether using port 80 or the FrankenPHP port) or
- Using Nginx to proxy_pass it to the FrankenPHP Port.
Now, I will show you the difference in performance
Testing Performance
This will use my real Laravel Project, using a valid Sanctum Token, MySql in the same server, and the API responses are the process of gzuncompress, json_decode, and implode processes in sequence. The test will run wrk for 30 seconds, using 16 threads and keeping 100 HTTP connections open. Here are the results:
Nginx + PHP-FPM: Total requests are 2326 with the Requests/sec is 77.
FrankenPHP: Total requests are 9382 with the Requests/sec is 311.
It shows that FrankenPHP would be faster by around 4x if it were just changed into it! But now let’s see the result of the top command:
When testing using Nginx + PHP, php-fpm commands show 5 processes with only around 30–40% CPU Usage.
On the other hand, FrankenPHP will only use 1 single process, but the CPU Usage goes up to 138%, meaning it utilizes more than 1 Core.
So, are you interested in trying FrankenPHP with Laravel Octane?
Another thing to note is that there are a few considerations to keep in mind, which can be found in the Laravel Docs. Everything has its pros and cons, so carefully consider what we can handle.