{"id":415,"date":"2024-06-13T13:00:53","date_gmt":"2024-06-13T13:00:53","guid":{"rendered":"https:\/\/upprofits.net\/?p=415"},"modified":"2024-08-30T11:26:42","modified_gmt":"2024-08-30T11:26:42","slug":"introduction-to-frankenphp","status":"publish","type":"post","link":"https:\/\/upprofits.net\/index.php\/2024\/06\/13\/introduction-to-frankenphp\/","title":{"rendered":"Introduction to FrankenPHP"},"content":{"rendered":"

FrankenPHP<\/strong> is a new PHP runtime designed to modernize PHP architecture. It is built on top of Caddy<\/a>, and it includes Caddy\u2019s built-in features such as automatic SSL, native support for HTTP3, and Early Hints. It also supports compression methods like Gzip, Brotli, and Zstd<\/a>. Additionally, it features a built-in Mercure<\/a> hub, enabling real-time push events without the need for additional libraries or SDKs.<\/p>\n

With all these features, FrankenPHP promises faster performance out-of-the-box, simpler configuration, and an improved Developer Experience (DX) compared to traditional PHP setups like PHP-FPM.<\/p>\n

Here is how it compares:<\/p>\n

\n\n\n\n\n\n\n\n
Feature<\/th>\nFrankenPHP<\/th>\nPHP-FPM<\/th>\n<\/tr>\n<\/thead>\n
Performance<\/strong><\/td>\nGreat performance with worker mode and direct communication with the web server, reducing latency.<\/td>\nGood performance, but it requires FastCGI communication which may introduce some overhead.<\/td>\n<\/tr>\n
Scalability<\/strong><\/td>\nExcellent scalability. It can be compiled into a single binary, making it easy to run on serverless architectures.<\/td>\nGood scalability, but often requires manual tuning of process pools on both the PHP-FPM and the web server side (usually Nginx<\/a>).<\/td>\n<\/tr>\n
Complexity<\/strong><\/td>\nGenerally simple with minimal configuration overhead, thanks to Caddy\u2019s configuration<\/a>.<\/td>\nSomewhat complex, involving separate configurations for PHP-FPM and the web server (usually Nginx<\/a>).<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n

Getting Started<\/h4>\n

Running FrankenPHP requires Docker. It is pre-packaged in a single Docker image, so you don\u2019t need to install PHP binaries and modules yourself. Once you have Docker installed, you need to create the index.php<\/strong> file, which will serve as the homepage.<\/p>\n

In this example, let\u2019s keep it simple. I will just print the PHP info table.<\/p>\n

\r\n<?php \r\n    phpinfo();\r\n<\/pre>\n

You can now run the following Docker command to start the site:<\/p>\n

\r\ndocker run -v $PWD:\/app\/public -p 80:80 -p 443:443 -p 443:443\/udp dunglas\/frankenphp\r\n<\/pre>\n

This command mounts the current directory to the \/app\/public<\/code> directory in the Docker container and maps ports 80<\/strong> and 443<\/strong> from the container to the host machine, as well as 443\/udp<\/strong> to enable HTTP3.<\/p>\n

Caddy also generates SSL certificates and loads the site over HTTPS. However, your browser won\u2019t recognize the SSL certificate for localhost, so you will receive an error when you load the site.<\/p>\n

\"SSL<\/figure>\n

There are a few ways we can create a workaround to let HTTPS load on localhost. The easiest and quickest method that works in different scenarios is to enable the flag in Chrome at chrome:\/\/flags\/#allow-insecure-localhost<\/code>. Restart Chrome, and then reload the page.<\/p>\n

\"Loaded<\/figure>\n

Now, your localhost runs on the FrankenPHP server API. If you inspect the response in Chrome, you\u2019ll see that it\u2019s compressed with zstd and served through HTTP3.<\/p>\n

It\u2019s impressive that we can have it running with just a single command.<\/p>\n

Running a Framework<\/h4>\n

FrankenPHP is not limited to just running a simple PHP file. It is compatible and can run a full-fledged PHP framework such as Symfony, Laravel, or any other PHP framework. The only thing you need to do is mount the framework directory to the \/app\/public<\/code> directory in the Docker container. For example, to run a Laravel application, you can run the following command:<\/p>\n

\r\ndocker run -v $PWD:\/app -p 80:80 -p 443:443 -p 443:443\/udp dunglas\/frankenphp\r\n<\/pre>\n

It\u2019s that simple. FrankenPHP will automatically detect the files and serve the Laravel application.<\/p>\n

\"Laravel<\/figure>\n

Wrapping Up<\/h4>\n

All these features like automatic SSL, HTTP3, and modern compression make developing and running PHP applications much easier and faster. If you\u2019re coming from nginx or Apache, like me, the only thing you need to get used to is the Caddyfile configuration. But once you get accustomed to it, you\u2019ll find it much simpler and more powerful than traditional web server configurations.<\/p>\n

I think FrankenPHP is a great choice for modern PHP applications. It\u2019s fast, scalable, and easy to use. I highly recommend it for any PHP developers looking to give it a try.<\/p>\n

The post Introduction to FrankenPHP<\/a> appeared first on Hongkiat<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"

FrankenPHP is a new PHP runtime designed to modernize PHP architecture. It is built on top of Caddy, and it includes Caddy\u2019s built-in features such as automatic SSL, native support for HTTP3, and Early Hints. It also supports compression methods like Gzip, Brotli, and Zstd. Additionally, it features a built-in Mercure hub, enabling real-time push […]<\/p>\n","protected":false},"author":1,"featured_media":421,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-415","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts\/415"}],"collection":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/comments?post=415"}],"version-history":[{"count":3,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts\/415\/revisions"}],"predecessor-version":[{"id":427,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/posts\/415\/revisions\/427"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/media\/421"}],"wp:attachment":[{"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/media?parent=415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/categories?post=415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/upprofits.net\/index.php\/wp-json\/wp\/v2\/tags?post=415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}