2.7 KiB
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 it's a virtual package. 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: 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 4: 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.