Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)
Posted By
kamlesh paulon
Dec 9, 2024Are you looking to make your Laravel application blazing fast? With Laravel Octane and FrankenPHP, you can significantly speed up your app by running everything in memory, avoiding the overhead of bootstrapping the app repeatedly.
Laravel Octane makes your application stateful, meaning your app doesn’t need to rebuild everything for every request. Combined with FrankenPHP, a PHP-based web server built on Caddy, you can eliminate reverse proxies and serve PHP applications with unparalleled performance.
Table of contents
- Why FrankenPHP?
- Step 1: Install Laravel Octane and FrankenPHP
- Step 2: Test Locally
- Step 3: Run in Production
- Step 4: Customize FrankenPHP with a Caddyfile
- Step 5: Keep Octane Running with Supervisor
- Step 6: Extend PHP Extensions
- Step 7: Create a Deployment Script
- Conclusion
Why FrankenPHP?
FrankenPHP is a custom-built Caddy server designed for PHP applications. It lets you run your Laravel application efficiently without a reverse proxy, giving you direct control over performance.
Step 1: Install Laravel Octane and FrankenPHP
- Start by installing Laravel Octane via Composer:
- Next, install Octane with the FrankenPHP server option:
This will download and configure the necessary files for FrankenPHP.
Step 2: Test Locally
- To test the setup on your local environment, run:
This starts the server on localhost
. But in production, you’ll need to run it with your domain.
Step 3: Run in Production
- For production environments, run the following command to start the server with your domain:
- However, this will start without HTTPS. To enable HTTPS and run on port 443, use:
This will run the server with your domain name and HTTPS, eliminating the need for a reverse proxy, making your app faster.
Step 4: Customize FrankenPHP with a Caddyfile
For optimal performance, you can configure FrankenPHP using a custom Caddyfile
. Place your Caddyfile
in the specified path, which should match the --caddyfile
flag used in the Supervisor configuration.
Here’s Caddyfile
with necessary headers and configurations:
This configuration ensures that your Laravel application is secure, fast, and efficient, with caching headers and security policies.
Ref link https://caddy.community/t/tweaking-frakenphp-caddyfile-for-laravel-octane/23926
Step 5: Keep Octane Running with Supervisor
In production, you’ll need to ensure Laravel Octane keeps running even after closing the terminal. This is where Supervisor comes in.
- Install Supervisor:
- Create a new configuration file for Laravel Octane:
Add the following configuration to keep Octane running:
To optimize performance, check your server’s
CPU count
and adjust the--workers
parameter accordingly. A common practice is to set the number of workers to the number of CPU cores available, allowing your application to handle multiple requests simultaneously without overloading the server.
- Next, reload the Supervisor configurations:
Step 6: Extend PHP Extensions
If you need additional PHP extensions that are not included by default, you can create the FrankenPHP binary from the FrankenPHP documentation. Follow the instructions provided to compile your custom binary, which you can place in the root of your project and use to enable any extra extensions your application may require.
Step 7: Create a Deployment Script
To streamline your deployment process, you can create a script that ensures everything is built for production and restarts Laravel Octane every time you make changes to your live server.
- To run this script:
Make it executable and then execute it:
This script ensures that your application is updated with the latest changes, optimized for production, and that Laravel Octane is restarted.
Conclusion
By leveraging Laravel Octane with FrankenPHP, you’re taking full advantage of memory-resident processes and eliminating unnecessary reverse proxies. This results in a highly optimized and scalable application.
Enjoy your blazing-fast Laravel app.