Run Sulu via FrankenPHP with custom PHP extensions
FrankenPHP is a new experimental application server created by Kévin Dunglas, a well-known Symfony core team member famous for projects like API Platform and Mercure Hub. In this blog post we will look at how to use FrankenPHP to run our Sulu Demo application. (Note: it does require additional PHP extensions).
What is FrankenPHP?
FrankenPHP is a PHP Application Server written in Golang. It is very similar to how Roadrunner works, another PHP Application Server written in Golang. FrankenPHP servers try to keep most of the application (in Symfony, the Kernel) in the memory. This allows the application servers to respond a lot faster since they don't need to boot the PHP application and wait for the container and kernel to be initialized.
The difference to Roadrunner is that FrankenPHP is not running in the PHP CLI context. Where Roadrunner uses PSR-7 request/response object, FrankenPHP creates its own SAPI to communicate and uses the normal $_SERVER variables to provide the PHP information.
FrankenPHP is still experimental. It was released on 14 October 2022 at the AFUP PHP Forum as an easy-to-Dockerize Application Server for PHP. From what we can read on the FrankenPHP Website, it is the first PHP Server which also supports 103 Early Hints. This is a new HTTP response object that supports sending several responses to the client to preload resources (like CSS) which drastically improves website loading times. FrankenPHP is built on top of Caddy Webserver which also provides automatic HTTPS support.
How to use FrankenPHP with Sulu?
The first requirement for using FrankenPHP is to use PHP 8.2 - which is not yet officially released - but if you are using "brew" you can install it via:
brew install firstname.lastname@example.org
For this demonstration we are using the sulu-demo as our test application. We are installing it via:
git clone email@example.com:sulu/sulu-demo.git
We need to change the
composer.json to PHP 8.2 by replacing PHP and extra platform to 8.2.0. After that, we need to install the Composer dependencies via the ignore platform reqs flag (because not all dependencies of Sulu support PHP 8.2 yet):
composer update --ignore-platform-reqs="php"
After this, we need to make the sulu-demo able to run via the symfony/runtime package. As Sulu is a DualKernel Setup there is an additional step required for us. For this, we will create a new
src/DualKernel.php which moves the logic of our index.php into this new runtime supported Kernel class:
Next we will edit the public/index.php file to use this new DualKernel class:
Now we will install the new FrankenPHP runtime by running:
composer require runtime/frankenphp-symfony --ignore-platform-req="php"
To use the new runtime we will also configure it in the "composer.json" extra section:
Thx to withinboredom Pull Request and the latest changes in the FrankenPHP docker image we not longer need to use a fork of FrankenPHP. To create our own docker image with additional php extensions, we only need to create a the following
Dockerfile and build it.
To build our custom image we need to run:
docker build -t custom-franken-php .
This can take some time so grab a coffee or a new drink until it is finished.
After successfully compiling our custom FrankenPHP image, we are now able to start our application. For this we go back into the root directory of our sulu-demo and run the following command to start FrankenPHP:
docker run -e FRANKENPHP_CONFIG="worker ./public/index.php" -v $PWD:/app -p 80:80 -p 443:443 custom-franken-php
The demo application is now available under https://localhost. You maybe need to accept unsafe SSL certification to see the page in your browser.
If it is not allowed to connect to your database you'll need to edit the
.env.local file to the following to connect via the host IP address:
This way you should be able to test any Symfony application with the new FrankenPHP application server, even if the application has a Multi-Kernel setup or requires additional PHP extensions.