How to Customize FrankenPHP

How to Customize FrankenPHP

Posted By

kamlesh paul

on

Dec 18, 2024

In my previous article, Boost Laravel Performance: Running Octane with FrankenPHP in Production (Zero Downtime), I demonstrated a great solution, but some key details were missing. Specifically:

  • How to add php extensions
  • Which extensions are supported
  • How to modify php.ini settings
  • How to change the PHP version and which versions are supported

If you’re looking for answers to these questions, let’s cover them in this article.

Table of contents

How to add php extensions

  • By default, most popular PHP extensions are compiled. To reduce the size of the binary and to reduce the attack surface, you can choose the list of extensions to build using the PHP_EXTENSIONS Docker ARG.

For instance, run the following command to only build the redis extension:

docker buildx bake \
  --load \
  --set static-builder.args.PHP_EXTENSIONS=redis \
  static-builder

To add libraries enabling additional functionality to the extensions you’ve enabled, you can pass use the PHP_EXTENSION_LIBS Docker ARG:

docker buildx bake \
  --load \
  --set static-builder.args.PHP_EXTENSIONS=redis \
  --set static-builder.args.PHP_EXTENSION_LIBS=libjpeg,libwebp \
  static-builder

Which extensions are supported

How to modify php.ini settings

  • You can achieve this using the ini_set() and set_time_limit() functions. For example:
ini_set('memory_limit', '256M');
set_time_limit(60); // Limit script execution to 60 seconds

This approach lets you adjust settings such as memory limits and execution time without affecting the global configuration, providing flexibility in how your PHP scripts run.

How to change the PHP version and which versions are supported

docker buildx bake \
  --load \
  --set static-builder.args.PHP_EXTENSIONS=redis \
  --set static-builder.args.PHP_EXTENSION_LIBS=libjpeg,libwebp \
  --set static-builder.args. PHP_VERSION=8.0 \
  static-builder

Copy FrankenPHP binary

docker cp $(docker create --name static-builder dunglas/frankenphp:static-builder):/go/src/app/dist/frankenphp-linux-$(uname -m) frankenphp ; docker rm static-builder

Conclusion

Customize FrankenPHP allows you to optimize your Laravel applications effectively. By adjusting PHP extensions, changing settings, and managing PHP versions, you can create a tailored environment that meets your application’s needs. These enhancements can significantly improve performance and flexibility, ensuring a smooth deployment process.

For more information about FrankenPHP, check out the FrankenPHP documentation.

FAQ

Can I add custom PHP extensions that are not listed?

Yes, you can add custom extensions by compiling them yourself. Refer to the static-php documentation for guidance on adding unsupported extensions.


How do I know which PHP version to use?

The best PHP version depends on your application requirements and compatibility with Laravel. It’s generally recommended to use the latest stable version supported by your Laravel version.


Can I change PHP settings on a live server?

Yes, you can change PHP settings dynamically using ini_set() in your PHP scripts. However, be cautious when adjusting critical settings on a live server.

Share this article

22 views