Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

# Integrate PHP Opcache & Make Your Application Win Big With Performance

July 7, 2021

OPcache is an opcode cache that stores precompiled PHP script bytecode in shared memory for faster execution. OPcache will speed up services based on PHP that saves time for your script compiling.

Best practices for optimizing PHP performance is a way broad topic and I can cover a lot of things in it. Previously I’ve written few guides regarding PHP best practices and firebase integrations which can be beneficial to kickstart with performance issues and database delivery.

PHP has so many options available which can boost up the application performance and allow handling number of requests smoothly at one time. The one similar option is of PHP OPcache. You can use it numerous times in your PHP apps for making its performance way faster.

In this article, I’ll give you a brief description about what is OPcache, the benefits of it and steps of integration. More importantly you will see how Cloudways has simplified the PHP OPcache integration in its PHP Hosting server.

## What is OPcache? I Just Heard The Name :/

Well OPcache comes bundled in PHP 5.5 and is available in latest versions. For previous versions like PHP 5.4, 5.3 and 5.2, you have to install pecl extension first to enable OPcache manually. According to php.net:

“OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.”

Basically when you complete the code compilation in PHP, the human readable code is converted in to machine language and it takes time to compile all the scripts. So if you make request every time and the cycle continues as the way it is, your app will face lot of performance issues.

But having the PHP OPcache enabled, the process will run once and cache all the scripts and compile time. The scripts will be stored in memory and only the updates will compile and continue to store. This can give you a serious performance boost and can reduce the app load time significantly. Meanwhile, PHP7 OPcache uses 64MB of memory by default.

## Why The Heck I need OPcache? I’ve Wands

To answer the above question, let’s consider a case where you have a small website with the normal traffic like 500-5k visitors daily. Concurrent users are also small like 5-10 daily. In this case you won’t feel any major performance issues and the users are entertained perfectly.

But imagine a website or application with hundred, thousand of visitors per second and the concurrent user ratio is also much more than thousand. Here PHP7 OPcache can give you optimum performance relief as with few configuration steps, you will see serious improvements in speed and performance.

Here you need to note one more thing that OPcache alone cannot handle everything but your server settings and configuration will also play an important role in it. While your coding structure is also an important part in this case. On a Digitalocean 1GB RAM server with 1 CPU, it can handle 10 request/sec while OPcache is disabled. By enabling it the number will increase to 34+ request/sec. You can also optimize it further to boost app performance more.

### A case at stackoverflow

While researching on OPcache, I found an interesting case regarding the performance benchmark of OPcache on a website. A user Tschallacka has defined his config on the server and how the request has been served in small time. The case will let you know how OPcache enhances application’s speed and performance.

“I have made an extensive framework with a lot of fields and validation methods and enums to be able to talk to my database.

Without OPcache

When using this script without OPcache, I push 9000 requests in 2.8 seconds to the Apache server. It maxes out at 90-100% CPU for 70-80 seconds until it catches up with all the requests.

Total time taken: 76085 milliseconds(76 seconds)

With OPcache enabled

With OPcache enabled, it runs at 25-30% CPU time for about 25 seconds and never passes 25% CPU use.

Total time taken: 26490 milliseconds(26 seconds)

I have made an OPcache blacklist file to disable the caching of everything except the framework which is all static and doesn’t need changing of functionality. I choose explicitly for just the framework files so that I could develop without worrying about reloading/validating the cache files. Having everything cached saves a second on the total of the requests 25546 milliseconds

This significantly expands the amount of data/requests I can handle per second without the server even breaking a sweat.

### Get Ready for Core Web Vitals Update

Ebook to Speed Up Your Website Before You Start Losing Traffic.

## Do I need APC and OPcache both?

The answer is No, If you have PHP OPcache enabled on your servers than it is optional to have APC also installed. Well, APC is also a caching system present in previous versions of PHP but it will not work with PHP 5.5 and newest versions like PHP 7.x. You must migrate to OPcache from APC and if you forcibly tries to use it, then you might could run into errors.

There is no need for APC (or any of the similar sort of bytecode caching extensions like XCache) with PHP 5.5 and later. The PHP developers directly integrate OPcache into the core of the app. Not only does this provide greater overall product stability, but is officially supported by the PHP developers meaning that it has wide working community.

## How PHP7 OPcache Execute Code?

Below is the picture of execution of code by PHP7 OPcache which is pretty much self explanatory.

Whenever the script compiles, the process checks the PHP OPcode caching and finds the already compiled code on shared memory. If the code is not found, initiates the compilation automatically and saves that to the memory generating output after that.

## Installation and Configuration

OPcache is compiled by default on PHP5.5+. However it is disabled on the version by default. In order to start using OPcache in PHP5.5+, you will first have to enable it manually. To do this you would have to do the following:

zend_extension=/full/path/to/opcache.so (nix)

zend_extension=C:\path\to\php_opcache.dll (win)

Note that when the path contains spaces you should wrap it in quotes:

zend_extension="C:\Program Files\PHP5.5\ext\php_opcache.dll"

Also note that you will have to use the zend_extension directive instead of the “normal” extension directive because it affects the actual Zend engine (i.e. the thing that runs PHP).

For PHP 5.4 or earlier (>= 5.2), you can install Zend OPcache using PECL.
$pecl install zendopcache-beta The pecl command will try to update your configure php.ini automatically. Now that you have setup OPcache perfectly, it’s time to update the recommended setting for performance optimization. The settings are as follows: opcache.memory_consumption=128 (for caching all compiled files) opcache.interned_strings_buffer=8 opcache.max_accelerated_files=4000 (by default can cache 2000 files) opcache.revalidate_freq=60 opcache.fast_shutdown=1 opcache.enable_cli=1 Some other settings might be helpful like if you are using code annotations, you can use: opcache.save_comments=1 You can see the fill list of run-time configurations here. ## How Cloudways Manages OPcache Since Cloudways believes in providing ease to their customers, it allows you to change the memory settings from server settings and packages tab. Also you have both the options APC and OPcache available there. While remember that you do not have the access to php.ini file, so the default values set up by Cloudways will be applied. You can change the memory size of both of these options according to your app requirements. The default setup values are as follows: opcache.interned_strings_buffer=4 opcache.max_accelerated_files=2000 opcache.memory_consumption=64 opcache.revalidate_freq=2 opcache.fast_shutdown=0 opcache.enable_cli=0 Now if you want to change the above values, you can move to application settings page and in PHP-FPM settings edit the values like: You Might Also Like: How to Host PHP on Amazon AWS EC2 ## OPcache Graphical GUI The traditional way to check if PHP OPcode caching is running on the server is to write phpinfo(); in php file and see the status like: But if you want to see the optimized Graphical GUI with user interaction in which you can have the options to manage settings with buttons, then you can use the open source packages available on Github. So following packages can be used for the job: OPcacheGUI This package allows you to manage OPcache features like status, configs, reset, scripts overview etc with a nice UI. You can also see the memory, keys and hits status in graphical form. Thus simplifies the over all data process to users. OPcache-status This package provides the one-page status page for PHP OPcode caching. Few more things it can do for you are: • The ability to sort the list of cached scripts by the various columns • A better layout that can accommodate more of the script data without looking cluttered • A tuning suggestion tab (need to add a couple of things to the OPcache output first though) OPcache-gui This package provides a clean and responsive interface for showing OPcache configurations, status and other settings built in jQuery and React. You’ll be able to see what host and platform you’re running on, what version of OPcache you’re using, when it was last reset and all the functions which are available. It also shows all the directives and statistics associated with the OPcache (number of hits, memory used, free and wasted memory, etc.). ## Final Words I’m sure by reading above you are pretty much convinced about integrating PHP OPcache in web applications. Obviously it has given major boost to PHP performance, so should be up and running in your web applications. Code caching is a great idea where you don’t need to compile code again and again. As the code is only compiled when it is updated and then saves into the shared memory. Hence, it saves a lot of time and gives flawless performance boost. So still if you have any further queries regarding PHP OPcode caching or about this article, you can write down your comments below. Q: How do I enable PHP opcode caching?Ans: To enable PHP opcode caching, here is the below-given code you will need to change in your php.ini file config: opcache.memory_consumption=128 opcache.interned_strings_buffer=8 opcache.max_accelerated_files=4000 opcache.revalidate_freq=60 opcache.fast_shutdown=1 opcache.enable_cli=1 Q: How do I disable PHP opcode caching? Ans: To disable PHP’s opcode caching, open your SSH terminal and go to the root, then edit the below-given file: /etc/php7.2-sp/conf.d/opcache.ini Add the following line to the end of this file: opcache.enable=0 Now, restart your PHP version by executing the following command: sudo service php7.2-fpm-sp restart Q: How to clear PHP opcache? Ans: To clear PHP opcache content, Reload the PHP module/daemon used by the web server and the OPcache contents will be deleted. Run the following given commands to complete the process: # when using mod_php and Apache$ apachectl graceful

# when using PHP-FPM


Q: What to do if PHP Opcache is not working?

Ans: If the PHP Opcache doesn’t works properly, go to the php.ini file and check the Opcache section. Make sure that the opcache.enable=1 is set in this file, if not then set it accordingly.

Share your opinion in the comment section. COMMENT NOW

Customer Review at

#### “Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

#### Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. Besides his work life, he loves movies and travelling. You can email him at [email protected]

×

## Thankyou for Subscribing Us!

×

### Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.