Installeer PHP OPcache op Rocky/Alma/CentOS 9|8
De OPcache is bedoeld om de prestaties van PHP te verbeteren door vooraf gecompileerde script-bytecode in gedeeld geheugen op te slaan. Door PHP-scripts in het geheugen te cachen, wordt de noodzaak voor PHP om scripts bij elke ontvangen aanvraag te laden en te parseren, geëlimineerd. Bij het hosten van websites of webapplicaties die in PHP zijn gebouwd, zal het gebruik van OPcache de reactietijd en de algemene laadtijden aanzienlijk verbeteren.
Dit is hoe PHP OPcache werkt:
- Scriptcompilatie: OPcache compileert een opgevraagd script naar bytecode.
- Bytecode caching: Zodra het is gecompileerd, wordt de bytecode opgeslagen in gedeeld geheugen voor snellere toegang.
- Uitvoering van volgende verzoeken: De volgende keer dat een verzoek voor hetzelfde script wordt ontvangen, wordt de gecachte bytecode gebruikt, waardoor de compilatiestap wordt overgeslagen en de uitvoering sneller verloopt.
Samengevat, door OPcache te gebruiken zult u een snellere uitvoeringstijd van PHP-scripts ervaren, een verminderde belasting van CPU en geheugen, en een betere schaalbaarheid van het systeem aangezien het meer gelijktijdige verzoeken aankan.
Hoe kun je dan PHP OPcache installeren en configureren op een Rocky Linux/AlmaLinux en CentOS Stream 9|8 Linux-systeem? Volg deze handleiding om te zien hoe het moet.
Overschakelen naar de PHP-editie van REMI
We kunnen OPcache alleen inschakelen op een systeem met PHP geïnstalleerd. De genoemde systemen verspreiden meerdere versies van PHP in AppStream-repositories. Maar Opcache wordt geleverd in REMI-repositories.
Om te controleren of PHP is geïnstalleerd, voer het commando uit:
$ php --version
PHP 8.2.13 (cli) (built: Nov 21 2023 09:55:59) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.2.13, Copyright (c) Zend TechnologiesUit mijn systeem kunnen we bevestigen dat de versie 8.2 is. Dus, we kunnen de PHP-module resetten.
sudo dnf module reset php -yVervolgens installeren we de RPM-repository van Remi:
- Enterprise Linux 9 – (Rocky/Alma/CentoS Stream 9)
sudo dnf -y install https://rpms.remirepo.net/enterprise/remi-release-9.rpm- Enterprise Linux 8 – (Rocky/Alma/CentoS Stream 8)
sudo dnf -y install https://rpms.remirepo.net/enterprise/remi-release-8.rpmDe installer zal automatisch de EPEL-repository instellen als afhankelijkheid. Om te controleren of REMI werkt, voer het volgende uit:
$ sudo dnf -y repolist
repo id repo name
appstream Rocky Linux 8 - AppStream
baseos Rocky Linux 8 - BaseOS
epel Extra Packages for Enterprise Linux 8 - x86_64
extras Rocky Linux 8 - Extras
remi-modular Remi's Modular repository for Enterprise Linux 8 - x86_64
remi-safe Safe Remi's RPM repository for Enterprise Linux 8 - x86_64Alle PHP-modules kunnen worden gecontroleerd door het volgende commando uit te voeren:
dnf module list phpHier is de voorbeeldoutput:
Rocky Linux 8 - AppStream
Name Stream Profiles Summary
php 7.2 [d][x] common [d], devel, minimal PHP scripting language
php 7.3 [x] common [d], devel, minimal PHP scripting language
php 7.4 [x] common [d], devel, minimal PHP scripting language
php 8.0 [x] common [d], devel, minimal PHP scripting language
php 8.2 [x] common [d], devel, minimal PHP scripting language
Remi's Modular repository for Enterprise Linux 8 - x86_64
Name Stream Profiles Summary
php remi-7.2 [x] common [d], devel, minimal PHP scripting language
php remi-7.3 [x] common [d], devel, minimal PHP scripting language
php remi-7.4 [x] common [d], devel, minimal PHP scripting language
php remi-8.0 [x] common [d], devel, minimal PHP scripting language
php remi-8.1 [x] common [d], devel, minimal PHP scripting language
php remi-8.2 [x] common [d], devel, minimal PHP scripting language
php remi-8.3 [x] common [d], devel, minimal PHP scripting language
Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalledActiveer nu een specifiek PHP-module van REMI. Uit de eerdere uitvoer bleek dat PHP 8.2 was geïnstalleerd, dit betekent dat we de module php:remi-8.2 moeten activeren.
sudo dnf module install php:remi-8.2 -yIn de meeste gevallen zal het je geïnstalleerde PHP-versie upgraden en de pakketten niet verwijderen.
Dependencies resolved.
==============================================================================================================================================================================================================================
Package Architecture Version Repository Size
==============================================================================================================================================================================================================================
Upgrading:
php-cli x86_64 8.2.21-1.el8.remi remi-modular 5.5 M
php-common x86_64 8.2.21-1.el8.remi remi-modular 1.3 M
php-fpm x86_64 8.2.21-1.el8.remi remi-modular 1.9 M
php-mbstring x86_64 8.2.21-1.el8.remi remi-modular 583 k
php-xml x86_64 8.2.21-1.el8.remi remi-modular 264 k
Installing dependencies:
oniguruma5php x86_64 6.9.9-1.el8.remi remi-safe 213 k
Installing module profiles:
php/common
Enabling module streams:
php remi-8.2
Transaction Summary
==============================================================================================================================================================================================================================
Install 1 Package
Upgrade 5 Packages
Total download size: 9.7 M
Is this ok [y/N]: yPHP OPcache installeren
Met de RPM-repository van REMI geconfigureerd, kunnen we zoeken naar beschikbare versies van OPcache.
$ sudo dnf search opcache
Last metadata expiration check: 0:07:55 ago on Sun 28 Jul 2024 10:12:13 PM UTC.
============================================================================================== Name & Summary Matched: opcache ===============================================================================================
php-opcache.x86_64 : The Zend OPcache
php56-php-opcache.x86_64 : The Zend OPcache
php70-php-opcache.x86_64 : The Zend OPcache
php71-php-opcache.x86_64 : The Zend OPcache
php72-php-opcache.x86_64 : The Zend OPcache
php73-php-opcache.x86_64 : The Zend OPcache
php74-php-opcache.x86_64 : The Zend OPcache
php80-php-opcache.x86_64 : The Zend OPcache
php81-php-opcache.x86_64 : The Zend OPcache
php82-php-opcache.x86_64 : The Zend OPcache
php83-php-opcache.x86_64 : The Zend OPcache
php84-php-opcache.x86_64 : The Zend OPcache
In dit voorbeeld installeren we php82-php-opcache, dat specifiek is voor PHP versie 8.2. Pas dit aan naar een versie die geschikt is voor jouw geïnstalleerde PHP.
sudo dnf install php82-php-opcacheMeer informatie over het geïnstalleerde pakket kan worden opgevraagd met de rpm -qi-commando's:
$ rpm -qi php82-php-opcache
Name : php82-php-opcache
Version : 8.2.21
Release : 1.el8.remi
Architecture: x86_64
Install Date: Sun 28 Jul 2024 10:21:52 PM UTC
Group : Unspecified
Size : 1024035
License : PHP-3.01
Signature : RSA/SHA256, Tue 02 Jul 2024 02:07:23 PM UTC, Key ID 555097595f11735a
Source RPM : php82-php-8.2.21-1.el8.remi.src.rpm
Build Date : Tue 02 Jul 2024 01:33:41 PM UTC
Build Host : builder.remirepo.net
Relocations : (not relocatable)
Packager : Remi Collet
Vendor : Remi's RPM repository <https://rpms.remirepo.net/> #StandWithUkraine
URL : http://www.php.net/
Bug URL : https://forum.remirepo.net/
Summary : The Zend OPcache
...PHP OPcache configureren
Gebruik het find-commando om het pad van de OPcache-module te vinden.
$ sudo find / -name opcache.so
/usr/lib64/php/modules/opcache.so
/usr/lib64/php-zts/modules/opcache.soDe module moet automatisch worden geladen bij installatie.
$ php -m | grep -i opcache
Zend OPcache
Zend OPcacheDe installer zal ook een configuratiesjabloon aanmaken in /etc/php.d, dat je kunt aanpassen.
$ ls /etc/php.d/|grep opcache
10-opcache.ini
opcache-default.blacklistOpen het bestand om te bewerken.
sudo vi /etc/php.d/10-opcache.iniOp onze server gebruiken we de volgende instellingen om een goede prestatie te leveren:
; Enable Zend OPcache extension module
zend_extension=opcache
; Determines if Zend OPCache is enabled
opcache.enable=1
; Determines if Zend OPCache is enabled for the CLI version of PHP
opcache.enable_cli=1
; The OPcache shared memory storage size (in MB)
opcache.memory_consumption=512
; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=128
; The maximum number of keys (scripts) in the OPcache hash table.
opcache.max_accelerated_files=10000
; The maximum percentage of "wasted" memory until a restart is scheduled.
opcache.max_wasted_percentage=5
; Enable timestamp validation, ensuring that cached files are up to date.
opcache.validate_timestamps=1
; Checks for script changes every 2 seconds.
opcache.revalidate_freq=2
; Path where OPcache can store file cache.
opcache.file_cache=/tmp
; Enables or disables opcode caching in shared memory.
opcache.file_cache_only=0
; Enables or disables checksum validation when script loaded from file cache.
opcache.file_cache_consistency_checks=1
; Other performance settings
opcache.fast_shutdown=1
opcache.enable_file_override=1
opcache.optimization_level=0x7FFFBFFFRaadpleeg de officiële OPcache-documentatie voor een volledige lijst van opties die u kunt configureren en aanpassen.
Testen of OPcache werkt
Herstart je webserver en php-fpm diensten.
- Nginx-webserver
sudo systemctl restart php-fpm nginx- Apache httpd-webserver
sudo systemctl restart php-fpm httpdHet weergeven van OPcache-runtimeconfiguraties kan worden gedaan door de onderstaande opdrachten uit te voeren:
php -i | grep opcache
Maak een voorbeeld PHP-script om te testen of OPcache werkt op ons systeem.
- Nginx-webserver
sudo vi /usr/share/nginx/html/opcache.php- Apache httpd-webserver
sudo vi /var/www/html/opcache.phpVoeg de inhoud toe aan het bestand.
<?php
if (function_exists('opcache_get_status')) {
$status = opcache_get_status();
if ($status) {
echo "OPcache is enabled on the server.";
} else {
echo "OPcache is disabled on the server..";
}
} else {
echo "OPcache is not installed.";
}
?>Sla het bestand op en open het vanaf http://serverIP_of_fqdn/opcache.php
Dit bevestigt dat PHP OPcache goed werkt in ons systeem. We hopen dat dit artikel nuttig was en wensen u snellere laadtijden van uw website!

