I understand what we could compile our PHP code to OpCache using preloader in 7.4. As I understand, it is only meant for CGI/FastCGI (PHP-FPM). Is it possible to do the same thing for just a CLI script that is not meant to be executed on HTTP request but console run?
Here I created a small repository example.
Technically preloading works for CLI.
I didn't get your example to use it, though. I am not familiar with the PHP image you used but here are some pointers:
You should run your container as non-root (or configure a user for preloading), see https://www.php.net/manual/de/opcache.preloading.php ("Running preloading as root is not allowed.")
You could add
user: <someUserId>:<someUsergroupId> to your service. Depending on how user switching is implemented in PHP it might need an entry in
opcode cache for CLI is opt-in, see https://www.php.net/manual/en/opcache.configuration.php#ini.opcache.enable-cli
There is a page on Preloading in the manual which explicitly mentions this scenario:
Additionally, preloading is only useful when there is a persistent process from one request to another. That means while it can work in a CLI script if the opcache is enabled, it's generally pointless.
To enable it, you would set something like this in your php.ini:
opcache.enable_cli=1 opcache.file_cache=/some/dir/somewhere opcache.file_cache_only=1 opcache.preload=/path/to/preload.php
If you configure this and run a CLI script, the preload script in
/path/to/preload.php will be executed, and any use of include, include_once, require, require_once, or opcache_compile_file() will cause compiled opcodes to be stored in
When you run a second CLI script, it will be able to use those compiled opcodes, but it won't know that preloading has already happened. As such, everything that you put in preload.php will run on every script load anyway. You would need to handle this manually, e.g. by calling opcache_is_script_cached.
The complexity of using it all this way probably exceeds the gain for most use cases.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.