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

4.5 KiB

I will add a tutorial for setting up OPcache on IIS.


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.

  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.


OPcache on IIS (Internet Information Services) 🌐

You can also use OPcache on a Windows Server with IIS. The configuration is similar to Linux, but with a few key differences.

  1. Find the php.ini file for IIS: The file is typically located in the PHP installation folder, such as C:\Program Files\PHP\v8.3\php.ini.

  2. Configure opcache.file_cache: In your php.ini, set the file cache path using a Windows-style path.

    opcache.file_cache="C:\inetpub\temp\opcache"
    

    You may also need to set opcache.mmap_base on Windows to prevent memory issues with many simultaneous requests.

  3. Create the cache directory: Create the folder and ensure the IIS user (e.g., IIS_IUSRS or the AppPool user) has write permissions.

  4. Restart IIS: Restart IIS to apply the changes. You can do this via the command line with iisreset or by using the IIS Manager.