3.7 KiB
Hier is de handleiding aangevuld met de stappen voor het instellen van de opcache.file_cache directive, wat een extra prestatieverbetering biedt.
Here is a step-by-step tutorial in English on how to enable OPcache.
Step 1: Install the OPcache PHP Extension
First, you need to install the OPcache module for your specific PHP version. The package name is typically php-opcache, but you must specify the exact version.
-
Check your PHP version:
php -v -
Install the correct package: Replace
<version>with your PHP version (e.g.,8.3,8.4).sudo apt install php<version>-opcache
This command installs the extension for both the CLI (Command-Line Interface) and PHP-FPM/Apache.
Step 2: Configure php.ini
Now, you need to enable and configure OPcache in your PHP configuration file. The file's location depends on your web server.
-
Find your
php.inifile:- For Nginx/PHP-FPM:
/etc/php/<version>/fpm/php.ini - For Apache:
/etc/php/<version>/apache2/php.ini
- For Nginx/PHP-FPM:
-
Edit the file: Open the file using a text editor like
nano.sudo nano /etc/php/<version>/fpm/php.ini -
Add/Edit the
[opcache]section: Add or find the[opcache]section and ensure the following lines are present and uncommented (remove the;if it's there).[opcache] opcache.enable=1 opcache.memory_consumption=512 opcache.max_accelerated_files=20000 opcache.revalidate_freq=60opcache.enable=1: This enables OPcache for your web server.opcache.memory_consumption: Adjust this value based on your server's RAM and website size. For a 24 GB server and a large site,512is a good starting point.opcache.max_accelerated_files: Adjust this based on the number of PHP files in your project. A value of20000or higher is recommended for large websites.opcache.revalidate_freq: This tells OPcache how often (in seconds) to check for file updates.60is a good value for a production environment.
Step 3: Enable the File Cache 💾
This is an optional but highly recommended step for an additional performance boost. It creates a persistent cache on your hard drive, which is useful when the server restarts.
-
Add the following line to the
[opcache]section in yourphp.inifile:opcache.file_cache="/var/www/opcache"This directive sets the path where OPcache will store a copy of the precompiled bytecode.
-
Create the cache directory and make sure your web server has write permissions.
sudo mkdir /var/www/opcache sudo chown -R www-data:www-data /var/www/opcache(Note: The user
www-datais common on Debian/Ubuntu. If you're on a different system, replacewww-datawith your web server's user, e.g.,apacheornginx).
Step 4: Restart Your Web Server 🔄
After saving the php.ini file, you must restart your web server for the changes to take effect. The command depends on your setup.
-
For Nginx (with PHP-FPM):
sudo systemctl restart php<version>-fpm -
For Apache (with mod_php):
sudo systemctl restart apache2
Step 5: Verify that OPcache is Active
To confirm that OPcache is working, create a PHP info file in your web root.
-
Create a file named
info.php:<?php phpinfo(); ?> -
View the file in your browser: Navigate to
http://your_domain.com/info.php. -
Check the OPcache section: Search for "Zend OPcache". The "Opcode Caching" status should be "Up and Running". You will also see the configuration settings you just applied, including the path to the file cache.