phpsymfonysymfony4preloadopcache

Opcache preloading and missing App_KernelProdContainer.preload.php


After following the documentation, my new OPCache settings are like this:

opcache.preload_user=www-data
opcache.preload=/var/www/vhosts/.../httpdocs/.../var/cache/prod/App_KernelProdContainer.preload.php
opcache.memory_consumption=1024
opcache.interned_strings_buffer=256
opcache.max_accelerated_files=20000
opcache.validate_timestamps=0

By default, inside var/cache/prod folder there is a file named srcApp_KernelProdContainer.preload.php.

After deleting /cache folder & restarting PHP-FPM, App_KernelProdContainer.preload.php is not created.

Should I rename App_KernelProdContainer.preload.php to srcApp_KernelProdContainer.preload.php or am I missing something else?


Solution

  • No, you shouldn't add this file directly to your PHP settings, since as you discovered it makes the whole thing very brittle. You should also not rename it or anything, since it's generated automatically.

    What Symfony does in latter versions is create a "preload" file.

    The Symfony 4 flex recipe includes this one.

    <?php
    
    if (file_exists(dirname(__DIR__).'/var/cache/prod/srcApp_KernelProdContainer.preload.php')) {
        require dirname(__DIR__).'/var/cache/prod/srcApp_KernelProdContainer.preload.php';
    }
    
    if (file_exists(dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php')) {
        require dirname(__DIR__).'/var/cache/prod/App_KernelProdContainer.preload.php';
    }
    

    On Symfony 5.1+ you have something like this.

    Notice that what this file requires the preload script conditionally. This way, if it doesn't exist, it won't just kill the PHP interpreter.

    If you do not currently have this file; you can create it manually, or just copy it from the recipe.

    Add this file to your opcache.preload settings on php.ini, not the generated preloading script directly.