Hello Everyone! Today I am going to share with you all some basic information about OpCache, and how to enable it on your PHP server.
I am preassuming that all of you know about the basic cache and it uses.
Basically, OpCache is a PHP extension, which when used, dramatically improves the performance of websites that use PHP. The OpCache runs out-of-the-box (by default, after installation). OPcache allows you to gain a performance boost by reducing code compilation time. When PHP code is compiled for the first time, it is saved in the server’s memory. Each time the code is called, the pre-compiled version is used instead of waiting for the code to compile, which causes a delay each time the code is used and increases the performance of the Website.
How does it work?
Following flowchart will help you understand the workflow of OpCache on a php website-
How to enable OpCache?
OpCache is generally disabled 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 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.
You can add the following optional settings to optimize the performance-
- opcache.memory_consumption=128 (for caching all compiled files)
- opcache.max_accelerated_files=4000 (by default can cache 2000 files)
Is OpCache Enabled?
To check If the OpCache is properly enabled and is working on your sever, write phpinfo(); in php file and see the status like:
This way you can check the OpCache UP and running.
This is all for now.
In My next Blog, I will share more information about OpCache features, advantages, and disadvantages.
Till then happy reading. 🙂