Introduction to FrankenPHP

FrankenPHP introduces a modernized approach to PHP architecture.

FrankenPHP leverages Caddy, incorporating its built-in features such as automatic SSL, native support for HTTP3, and Early Hints. It further supports compression methods like Gzip, Brotli, and Zstd. A built-in Mercure hub is also included for real-time push events without the need for additional libraries or SDKs.

This runtime promises enhanced performance right out of the box, streamlined configuration, and an improved Developer Experience (DX) when compared to traditional PHP environments such as PHP-FPM.

Here’s a comparison of their features:

FeatureFrankenPHPPHP-FPM
PerformanceOffers excellent performance with worker mode and direct communication with the web server, reducing latency.Delivers good performance, though it requires FastCGI communication which can introduce some overhead.
ScalabilityHighly scalable. It can be compiled into a single binary, facilitating operation on serverless architectures.Offers decent scalability, but typically necessitates manual tuning of process pools on both the PHP-FPM and web server sides (often Nginx).
ComplexityGenerally simpler with minimal configuration effort, thanks to Caddy’s configuration.More complex, requiring separate configurations for PHP-FPM and the web server (typically Nginx).

Getting Started with FrankenPHP

To begin using FrankenPHP, you’ll need Docker, as it is packaged within a single Docker image. This means there is no need to manually install PHP binaries or modules. Once Docker is set up, create an index.php file to serve as the homepage.

Let’s start with a simple example where we display the PHP information table:

<?php 
    phpinfo();

Use the following Docker command to launch your site:

docker run -v $PWD:/app/public -p 80:80 -p 443:443 -p 443:443/udp dunglas/frankenphp

This command links the current directory to the /app/public directory inside the Docker container and maps ports 80 and 443 from the container to your host machine. The additional mapping of 443/udp facilitates HTTP3 support.

Caddy automatically handles SSL certification and loads your site via HTTPS. However, you might encounter an SSL certificate error in your browser because it does not recognize certificates for localhost.

SSL Error on LocalhostSSL Error on Localhost

To bypass the HTTPS error on localhost, you can enable a specific flag in Chrome by visiting chrome://flags/#allow-insecure-localhost. After restarting Chrome and reloading the page, the issue should be resolved.

Localhost Loaded SuccessfullyLocalhost Loaded Successfully

With FrankenPHP, your localhost now operates using the server API, with responses compressed using zstd and delivered over HTTP3. It’s remarkable how this setup is achievable with just a single command.

Running a Framework with FrankenPHP

FrankenPHP extends beyond simple PHP files; it fully supports major PHP frameworks such as Symfony, Laravel, and others. To use it, simply mount your framework directory to the /app/public directory in the Docker container. For instance, to run a Laravel application, execute this command:

docker run -v $PWD:/app -p 80:80 -p 443:443 -p 443:443/udp dunglas/frankenphp

This simple setup allows FrankenPHP to automatically recognize and serve the Laravel application.

Laravel Application Running on LocalhostLaravel Application Running on Localhost

Concluding Thoughts

The integration of features like automatic SSL, HTTP3, and advanced compression technologies simplifies and accelerates the development and deployment of PHP applications. For those accustomed to nginx or Apache, adapting to the Caddyfile configuration is the biggest change. However, it proves to be simpler and more robust than traditional web server setups once mastered.

I find FrankenPHP to be an excellent solution for modern PHP applications due to its speed, scalability, and ease of use. It comes highly recommended for any PHP developers eager to enhance their setup.

The post Introduction to FrankenPHP appeared first on Hongkiat.

Leave a Reply

Your email address will not be published.