Make Your Website Faster with OPCache

This is me.

Author: Tim Strawbridge

Date Created: Feb 05, 2021

  • opcache,
  • operational code caching,
  • opcode
laptop keypad


What is OPCache?

OPCache is a performance enhancing extension in PHP. It is really OP code caching or operation code caching. OP code is an execution of the code that runs on the server. For instance, you might recall what operations are at the assembly level; ADD, INC, MOV are all operations. The OP code is the first byte of the instruction that tells the CPU which operation to perform.

Opcode tables:

http://ref.x86asm.net/

How does it work?

Let’s start with the basics of caching. The basic principle with cache is the same for all caches whether its browser cache, server cache, object cache, etc.

Since PHP is an interpreted language, it runs through an interpreter in order to get processed by the CPU. Interpretation takes longer to run than compiled code. This is where OPCache steps in. OPCache takes PHP source code, compiles it and stores that in a cache or memory. This type of memory is shared memory and can used by different processes. Shared memory is an efficient way to access and store common variables in a program. The first time the PHP code executes it will take a while to execute, however the next time the code is ran it is much faster.

How do I check to see if it is enabled on my server?

Since PHP version 5.5, OPCache has been bundled with PHP, however it may not be enabled. To check to see if you have OPCache enabled you will have to look in your loaded php.ini file.

Check where the ini file is:

php -i | grep php.ini

result:

Loaded Configuration File => /etc/php/7.3/cli/php.ini

Your results may differ based on what server OS is being used and the PHP package used.

Note: It may be better to run phpinfo() on a blank php file. The above configuration file references the CLI configuration file.

Now we can use a command to open the configuration file:

nano /etc/php/7.3/cli/php.ini

or

php -ini 

Setting it all up

Open the ini file and look for the following entry under the Zend OPcache settings.

Setting names are the same in Apache and Nginx.


opcache.enable=1

The opcache.enable setting should be set to 1.

Other settings:

opcache.memory_consumption
opcache.file_cache=/path/to/storage/.opcache;
opcache.max_accelerated_files=10000
opcache.revalidate_freq=200


If you don’t see the opcache setting then you might not have it installed on your machine. If you are running PHP version 7 + then you most likely have it installed. If not you download it through the PECL package manager.


To find out more about configuration options go to https://www.php.net/manual/en/opcache.configuration.php

Don’t worry if you see your opcache setting for the command line config is disabled. You shouldn’t enable OPCache on the CLI anyway.

Signup for our newsletter to access more than just blog posts. When you do, you'll get tailored content straight to your inbox!

Related Posts