This website uses cookies

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.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

Integrate PHP Opcache & Make Your Application Win Big With Performance

Updated on  22nd November

8 Min Read
PHP OPCAHE
Reading Time: 8 minutes

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.

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:

Add the following line to your php.ini:

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

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

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:

Some other settings might be helpful like if you are using code annotations, you can use:

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:

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:

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

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:

Now, restart your PHP version by executing the following command:

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:

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

Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at shahroze.nawaz@cloudways.com

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.