Skip to content
Advertisement

How to precompile php opcache using command line?

I’m trying to warm up opcache via opcache.file_cache feature using console command.

My opcache.ini:

opcache.enable=true
opcache.enable_cli=true
opcache.memory_consumption=128
opcache.interned_strings_buffer=32
opcache.use_cwd=false
opcache.max_file_size=0
opcache.max_accelerated_files=32531
opcache.validate_timestamps=false
opcache.revalidate_freq=0
opcache.enable_file_override=true
opcache.optimization_level='0xFFFFFFFF'
opcache.file_update_protection=0
opcache.save_comments=false
opcache.file_cache='/tmp/.opcache'
opcache.file_cache_consistency_checks=false
opcache.log_verbosity_level=0

before I start my php-fpm process I compile files by executing scrpt from clommand line:

$getDirs = static function () use ($argv): array {
    $dirs = $argv;
    unset($dirs[0]);
    return $dirs;
};

$files = SymfonyComponentFinderFinder::create()->in($getDirs())
    ->name('*.php')
    ->ignoreUnreadableDirs()
    ->notContains('#!/usr/bin/env php')
    ->notPath(get_included_files())
    ->files()
    ->followLinks();

$compiled = 0;
foreach ($files as $file) {
    try {
        if (!opcache_is_script_cached($file->getRealPath())) {
            opcache_compile_file($file->getRealPath());
        }

        $compiled++;
    } catch (Throwable $e) {
    }
}

echo 'Cached ' . $compiled . ' files' . PHP_EOL;

This script generates compiled files to specified (/tmp/.opcache) directory. Then I start php-fpm process, but no precompiled cache is not used and opcache compiles cache every time php-fpm process being restarted.

Is there a way to precompile opcache without using http requests?

Advertisement

Answer

May be someone will find this useful Precompiling opcache for php-fpm via CLI does work For some reason it takes too much time (compared with in-memory stored Opcodes) to load cached files into memory, but no recompilation of PHP files being executed.

P.S. This trick works even for different opcache confgurations.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement