Opcache preloader in php 7.4 on BitrixVM
Rus
Eng
Opcache preloader в php 7.4 на BitrixVM

All English-language materials have been translated fully automatically using the Google service

Preload files and classes using opcache.preloader in php 7.4+

In php, starting from version 7.4, it became possible to preload project files into memory. This can potentially significantly speed up access to them from project files. It works like this:

1. You write a file in which you list all the necessary files for preloading
2. Add the opcache.preload option to the php.ini file
3. Restart the httpd service
4. You work with files and classes preloaded into memory
5. Take into account that after making changes to files and classes, to update them in memory - the httpd service must be restarted.

In its simplest form, the listing of the preloader.php file will look like this:

<?
require_once __DIR__.'/local/PATH_TO_VENDOR/vendor/autoload.php';
require_once __DIR__.'/local/PATH_TO_CUSTOM_CLASSES/include/autoload.php';

// File list
$files = [
	//'/var/www/data/site/html/file_to_preload.php',
];

// Preload all root project files
foreach ($files as $file) {
	require_once $file;
	
	//Or
	//opcache_compile_file($file);
}

Assuming that opcache is already installed on your server and you have root access to it, then go to the file /etc/php.d/10-opcache.ini and add commands:

; Root User name. On BitrixVM it is usually root
opcache.preload_user=root
; Full path to your preloader file
opcache.preload=/home/bitrix/ext_www/site/preloader.php

Full listing opcache.ini

zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=512
opcache.interned_strings_buffer=88
opcache.max_accelerated_files=200000
opcache.max_wasted_percentage=1
opcache.validate_timestamps=1
opcache.revalidate_freq=0
opcache.fast_shutdown=1
opcache.save_comments=1
opcache.load_comments=1
opcache.blacklist_filename=/etc/php.d/opcache*.blacklist
opcache.preload_user=root
opcache.preload=/home/bitrix/ext_www/site/preloader.php

Restart httpd service with command

service httpd restart

And remember it - you will need it to apply changes to the preloaded classes

If you did everything correctly, then you can see that preloading works with the command:

print_r(opcache_get_status()['preload_statistics'])

It will print an array of all files and classes that were preloaded

Comments

There are no comments yet, you can be the first to leave it

Leave a comment

The site uses a comment pre-moderation system, so your message will be published only after approval by the moderator

You are replying to a user's comment

Send

FEEDBACK

Email me

Are you developing a new service, making improvements to the existing one and want to be better than your competitors? You have come to the right place. I offer you a comprehensive studio-level website development. From me you can order design, layout, programming, development of non-traditional functionality, implementation of communication between CMS, CRM and Data Analitics, as well as everything else related to sites, except for promotion.

Contact, I will always advise on all questions and help you find the most effective solution for your business. I am engaged in the creation of sites in Novosibirsk and in other regions of Russia, I also work with the CIS countries. You will be satisfied with our cooperation

An error occurred while sending, please try again after a while
Message sent successfully

Phones

+7(993) 007-18-96

Email

info@tichiy.ru

Address

Россия, г. Москва

By submitting the form, you automatically confirm that you have read and accept the Privacy Policy site

Contact with me
Send message
By submitting the form, you automatically confirm that you have read and accept Privacy policy of site
Sending successful!
Thank you for contacting :) I will contact you as soon as possible
Sending failed
An error occurred while sending the request. Please wait and try again after a while or call my phone number