How to Enable and Configure PHP Opcache in ServBay
ServBay is a powerful local web development environment designed for developers, integrating a variety of technology stacks including PHP. To help developers optimize PHP application performance, ServBay pre-installs the Opcache module for every supported PHP version. This article provides a detailed guide on how to enable and configure Opcache in ServBay, and explains how it can speed up your PHP development and testing workflow.
Introduction to the Opcache Module
Opcache is an official PHP extension designed to improve the execution performance of PHP code. Its core principle is caching precompiled PHP script bytecode in shared memory. This allows subsequent requests for the same script to use the cached bytecode directly, rather than repeatedly parsing, compiling, and optimizing the code. As a result, it dramatically reduces CPU and memory usage and speeds up application response times.
Why is Opcache Important for Developers?
- Faster Development and Testing: Enabling Opcache in your local environment makes your applications run faster, reduces page load times, and boosts development efficiency.
- Production Environment Simulation: Most production servers enable Opcache for enhanced performance. Enabling it locally allows you to better emulate a production environment.
- Reduced Resource Consumption: Especially beneficial when running large frameworks or complex applications, Opcache significantly lowers resource consumption on your local machine.
Key Features of Opcache
- Bytecode Caching: The core function, storing compiled PHP code in cache.
- Shared Memory: Cache stored in shared memory, accessible by multiple PHP processes.
- Automatic Expiry: Configurable to periodically check for file changes, ensuring cache freshness.
- Flexible Configuration: Rich set of configuration options allowing adjustments according to specific needs.
- Built-in Support: Official PHP extension, tightly integrated with the PHP core.
ServBay’s Support for Opcache
ServBay comes with the Opcache module pre-installed for all integrated PHP versions (such as PHP 7.4, 8.0, 8.1, 8.2, 8.3, 8.4, etc.). No additional downloads or compilation are required—users can enable and configure it directly through ServBay’s graphical interface or by editing configuration files.
How to Enable the Opcache Module
There are two main ways to enable Opcache in ServBay: via the graphical user interface (GUI) or by manually editing configuration files.
Method One: Enable Using the ServBay GUI (Recommended)
This is the simplest and quickest method.
- Open the ServBay application.
- In the sidebar navigation, click Languages.
- Select the PHP version you want to enable Opcache for, for example, PHP 8.3.
- On the PHP version detail page on the right, click the Extensions tab.
- Find the OPcache extension in the list.
- Click the switch next to OPcache to turn it on.
- Click the Save button at the bottom of the page.
- ServBay will prompt you to restart the corresponding PHP package to apply changes. Click the Restart button.
After restarting, the Opcache module will be enabled.
Method Two: Manually Edit the Configuration File
If you require more granular configuration or prefer to manage settings via config files, you can manually edit the Opcache .ini
file.
- Locate the Configuration File: The Opcache config file is located in the
conf.d
directory for the corresponding PHP version. This directory stores configuration files for all extensions loaded by that PHP version.- For example, for PHP 8.3, the config path is typically:
/Applications/ServBay/etc/php/8.3/conf.d/opcache.ini
1 - Adjust the version number in the path to match your PHP version.
- For example, for PHP 8.3, the config path is typically:
- Edit the Configuration File: Open the
opcache.ini
file with your preferred text editor.- Typically, this file already includes a basic Opcache configuration framework but commented out. Locate the
[Zend Opcache]
section. - Uncomment (remove the leading
;
) the linezend_extension = opcache.so
to ensure the Opcache extension is loaded. - Modify or add other Opcache configuration parameters as needed. A typical configuration example is shown below (these parameters are explained in the next section):ini
[Zend Opcache] ; Ensure the extension is loaded zend_extension = opcache.so ; Basic Configuration (example values) opcache.enable = 1 ; Enable Opcache globally (1=enabled, 0=disabled) opcache.memory_consumption = 128 ; Amount of memory for opcode cache (in MB) opcache.interned_strings_buffer = 8; Amount of memory for interned strings (in MB) opcache.max_accelerated_files = 4000 ; Maximum number of files to cache opcache.revalidate_freq = 60 ; How often to check file timestamps for updates (in seconds). 0 = check on every request (slow), but good for development. opcache.fast_shutdown = 1 ; Enable fast shutdown opcache.enable_cli = 1 ; Enable Opcache for the PHP CLI (Command Line Interface)
1
2
3
4
5
6
7
8
9
10
11
12 - Save the
opcache.ini
file.
- Typically, this file already includes a basic Opcache configuration framework but commented out. Locate the
- Restart the PHP Package: In the ServBay app, find the corresponding PHP package (located under Packages in the sidebar dropdown, then select PHP), and click the restart button.
After manually editing the configuration and restarting the PHP package, the Opcache module and your changes will take effect.
Verifying That the Opcache Module is Loaded
The most reliable way to verify if Opcache is successfully enabled is to review PHP configuration information using phpinfo()
.
- Create a
phpinfo.php
File: In your ServBay site's web root, create a new PHP file, for examplephpinfo.php
. The default ServBay web root is/Applications/ServBay/www/
. You can create a subdirectory for testing, such as/Applications/ServBay/www/servbay.demo/
, and placephpinfo.php
inside it. File content:php<?php phpinfo(); ?>
1
2
3 - Visit
phpinfo.php
: Ensure your web server (Caddy, Nginx, etc.) is running and configured to serve your site root or test subdirectory. Access the file through your browser.- If you placed the file under
/Applications/ServBay/www/servbay.demo/
and have configured a domain such asservbay.demo
to point to that directory, visit:http://servbay.demo/phpinfo.php
. - If you put it directly in the default root
/Applications/ServBay/www/
and the ServBay default website points there, the access URL may behttp://localhost/phpinfo.php
or whatever local address ServBay provides by default.
- If you placed the file under
- Locate Opcache Information: On the opened
phpinfo()
page, scroll down or use your browser's search (Cmd+F
orCtrl+F
) and look for "Opcache".- If the Opcache module is loaded successfully, you'll see a dedicated configuration section named "Zend Opcache". This section displays Opcache's version, status, and current configuration.
- If you cannot find the "Zend Opcache" section, the module did not load successfully. Please review the enabling steps and double-check your configuration file.
Illustration: Locating the Zend Opcache configuration block on the phpinfo page
Detailed Explanation of Opcache Options
Opcache offers many configuration options that you can fine-tune according to your development or application needs. These parameters are typically set in the opcache.ini
file. Here are some of the most commonly used and important options along with explanations:
[Zend Opcache]
; Ensure the extension is loaded
zend_extension = opcache.so
; Enable Opcache. 0 = disabled, 1 = enabled. The GUI toggle controls this parameter.
opcache.enable = 1
; Shared memory size allocated to Opcache for storing compiled PHP bytecode (in MB).
; Adjust according to project size and available memory. Large projects may require more.
opcache.memory_consumption = 128
; Amount of shared memory for internal strings (like class and method names), in MB.
; Frameworks using many strings (like Symfony) may need this value increased.
opcache.interned_strings_buffer = 8
; Maximum number of files that Opcache can cache.
; If your project has more files than this value, some won’t be cached. Set this value above your total file count.
opcache.max_accelerated_files = 10000
; Frequency (seconds) at which Opcache checks for script updates.
; In production, this is usually set to a high value (e.g., 300 or 600), or even 0 (no checks; manual cache reset needed).
; **For local development, set this to a lower value (1 or 2) so code changes take effect quickly.** If set to 0, Opcache checks for updates on every request, which reduces caching benefits but is useful during development.
opcache.revalidate_freq = 2
; Enable fast shutdown sequences to speed up memory release at the end of requests. Usually recommended.
opcache.fast_shutdown = 1
; Enable Opcache for PHP CLI (Command Line Interface).
; Very helpful for scripts or Composer commands—usually recommended.
opcache.enable_cli = 1
; Allow freeing idle shared memory. In some cases can help reclaim memory, but may slightly reduce performance. Usually leave as default/disabled.
; opcache.enable_file_override = 0
; Ignore comments when caching file paths. Can save a small amount of memory, may affect tools relying on comments.
; opcache.save_comments = 1
; Retain doc comments (docblocks) in cache. Required for tools that rely on doc comments (PHPUnit, Doctrine, etc.).
; opcache.load_comments = 1
; Optimization level. Higher value means more optimization but longer compile time. Default is usually sufficient.
; opcache.optimization_level = 0x7FFFBBFF
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Note: After modifying opcache.ini
, you must restart the corresponding PHP package for changes to take effect.
Frequently Asked Questions (FAQ)
- Q: Is Opcache enabled by default?
A: In ServBay, Opcache is pre-installed but typically disabled by default. You need to enable it manually following the steps above. - Q: Will enabling Opcache affect my development workflow?
A: Enabling Opcache will make your application run faster. During development, setopcache.revalidate_freq
to a low value (like 1 or 2) or 0, so you see the effect of code changes immediately. For production, increase this value or set to 0 with a manual cache clearing strategy. - Q: Does Opcache cache PHP CLI scripts by default?
A: No, not by default. You need to setopcache.enable_cli
to1
to enable Opcache for PHP CLI. This is very useful for speeding up Composer commands, unit testing, or other CLI tools. - Q: How much memory should I allocate to Opcache?
A: The value foropcache.memory_consumption
depends on your project’s size and complexity. For most local development projects, 128MB or 256MB is usually sufficient. If your project is very large or you see warnings inphpinfo()
about full cache, increase this value. - Q: What should I do after modifying
opcache.ini
?
A: You must restart the relevant PHP package for configuration changes to take effect.
Conclusion
Opcache is a powerful and easy-to-use PHP performance optimization tool. ServBay provides a convenient way for developers to enable and manage the Opcache module, whether through an intuitive graphical interface or flexible configuration files. With just a few simple steps, you can enable Opcache in your ServBay environment and dramatically improve your PHP app’s loading speed and responsiveness—optimizing your local development and testing experience. By tuning Opcache settings to fit your project’s needs, you can further enhance performance. Start enabling Opcache in ServBay today and experience the leap in PHP performance!