Files
Epicnabbo-Catalogus-Updated…/extra tuts/how to enable OPcache.md
T
2025-09-21 13:25:55 +02:00

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.

  1. Check your PHP version: php -v

  2. 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.

  1. Find your php.ini file:

    • For Nginx/PHP-FPM: /etc/php/<version>/fpm/php.ini
    • For Apache: /etc/php/<version>/apache2/php.ini
  2. Edit the file: Open the file using a text editor like nano. sudo nano /etc/php/<version>/fpm/php.ini

  3. 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=60
    
    • opcache.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, 512 is a good starting point.
    • opcache.max_accelerated_files: Adjust this based on the number of PHP files in your project. A value of 20000 or higher is recommended for large websites.
    • opcache.revalidate_freq: This tells OPcache how often (in seconds) to check for file updates. 60 is 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.

  1. Add the following line to the [opcache] section in your php.ini file:

    opcache.file_cache="/var/www/opcache"
    

    This directive sets the path where OPcache will store a copy of the precompiled bytecode.

  2. 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-data is common on Debian/Ubuntu. If you're on a different system, replace www-data with your web server's user, e.g., apache or nginx).


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.

  1. Create a file named info.php:

    <?php
    phpinfo();
    ?>
    
  2. View the file in your browser: Navigate to http://your_domain.com/info.php.

  3. 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.