heroku-buildpack-php icon indicating copy to clipboard operation
heroku-buildpack-php copied to clipboard

ability to use php 7.4 preloading

Open rbaarsma opened this issue 5 years ago • 12 comments

I cannot find any information on using the new php 7.4 preloading feature with Heroku. Since we need to input a path, I'm not sure if I can do it manually

For example in Symfony this can normally be enabled with a simple one-liner in the php.ini: https://symfony.com/doc/current/performance.html#use-the-opcache-class-preloading

It's supposed to add about 75% performance. That's quite a lot, so I'd like to use it - and I'm probably not the only one :)

rbaarsma avatar May 17 '20 14:05 rbaarsma

For what it's worth: documentation on this functionality: https://www.php.net/manual/en/opcache.preloading.php had been published by the end of July 2020: https://github.com/php/doc-en/commit/83c41a8f69bacceef42fb99b2188bb8b24b2fe58

holtkamp avatar Aug 24 '20 21:08 holtkamp

For me (on dokku/herokuish), putting this in user.ini seems to work (at least, doesn't give errors during deploy):

opcache.preload=/app/var/cache/prod/App_KernelProdContainer.preload.php
opcache.memory_consumption=256
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0

Make sure that user.ini is mentioned in your Procfile:

web: $(composer config bin-dir)/heroku-php-nginx -C nginx.conf -F fpm_custom.conf -i user.ini public/

bjornpost avatar Aug 25 '20 08:08 bjornpost

@bjornpost thanks for the insight, did you observe any (significant) improvements?

holtkamp avatar Aug 25 '20 09:08 holtkamp

@holtkamp I'm not sure how and where to measure these improvements, so I wouldn't dare to burn my fingers on that. I'm not noticing big improvements in ttfb on individual requests though.

bjornpost avatar Aug 25 '20 10:08 bjornpost

@bjornpost maybe you see a drop in response time on something like NewRelic or the Heroku metrics 🤓. Will try to get it working as well and hopefully report back

holtkamp avatar Aug 25 '20 11:08 holtkamp

@holtkamp did you manage to get it working?

bjornpost avatar Oct 06 '20 19:10 bjornpost

Did not try yet... Hopefully "this year"...

holtkamp avatar Oct 06 '20 20:10 holtkamp

Is this something that should work out of the box or does heroku need to do something to support the usage of opcache.preload in a .user.ini file?

jwage avatar Dec 19 '20 01:12 jwage

@bjornpost .user.ini should be automatically picked up by heroku/dokku - so do you need to specifically specify that in Procfile using -i for opcode preloading to work. did anyone enable opcode preloading and can share metrics or tests prior and after

nileio avatar Aug 16 '21 06:08 nileio

After looking in to this, not all ini settings can be set in a .user.ini file or in a custom phpfpm.conf

See this here https://www.php.net/manual/en/configuration.file.per-user.php

Only INI settings with the modes PHP_INI_PERDIR and PHP_INI_USER will be recognized in .user.ini-style INI files.

And you can see on this page https://www.php.net/manual/en/ini.list.php that the setting opcache.preload is PHP_INI_SYSTEM so I believe this setting has to be set another way in heroku.

This does not work either:

Procfile

web: heroku-php-nginx -C nginx.conf -F phpfpm.conf public/

phpfpm.conf

php_value[opcache.preload]=/app/var/cache/prod/App_KernelProdContainer.preload.php
php_value[opcache.memory_consumption]=256
php_value[opcache.max_accelerated_files]=20000
php_value[opcache.validate_timestamps]=0

Is it is possible to set these settings in the php.ini that is used when php-fpm starts?

jwage avatar Jul 06 '22 22:07 jwage

The answer to @jwage's question is... heroku-php-{apache2,nginx} -i custom.ini, but keep in mind that you're completely replacing the production INI config this way.

Anyway... what do y'all think about PHP_OPCACHE__PRELOAD or something like that as an env var?

dzuelke avatar Jul 12 '22 20:07 dzuelke

Did anyone figure this out in a way that doesn't replace the production ini?

Edit: I've been running some experiments and come up with this: .heroku/compile.sh

#!/usr/bin/env bash
set -e
set -o pipefail

cat .heroku/php/etc/php/php.ini - <<'EOT' > config/php.ini
opcache.preload=/app/config/preload.php
opcache.memory_consumption=256
opcache.max_accelerated_files=20000
EOT

exit 0

composer.json

{
    "scripts": {
        "compile": [
            "@php bin/console cache:clear --env=prod --no-optional-warmers --no-debug"
            ".heroku/compile.sh"
        ]
}

Procfile

web: vendor/bin/heroku-php-apache2 -C .heroku/apache.conf -i config/php.ini public/

nicholasruunu avatar May 10 '23 08:05 nicholasruunu