How to Install OPCache to Improve PHP Performance On CentOS / RHEL 7,8

OPcache is a type of OPcode caching. It is a powerful PHP extension built to enhance PHP performance. It works by storing pre-compiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

Have a look at the diagram to see how OPCache executes code:

Source: wp-rocket.me

Table of Contents

  1. Install OPCache
  2. Verify Installation
  3. Configiration

Install OPCache

I hope PHP is installed on your server. If the server doesn’t contain PHP, please follow this article to install PHP on your server.

To install PHP OPCache, run this command:

# centos 8
sudo dnf install php-opcache -y

# centos 7
sudo yum install php-opcache -y

Verify Installation

To verify installation run this command:

php -v

You’ll see the output like:

You can also verify from PHP info:

Configiration

After completing the installation, we can easily configure OPCache. Open OPCache config file:

nano /etc/php.d/10-opcache.ini

Now we can set our own config like:

opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1

You can see all available config options from here.

Lastly, restart the webserver for Opcache to start working.

# nginx
sudo systemctl restart nginx

# apache
sudo systemctl restart httpd

That’s all. Thanks for reading.


Software Engineer | Ethical Hacker & Cybersecurity...

Md Obydullah is a software engineer and full stack developer specialist at Laravel, Django, Vue.js, Node.js, Android, Linux Server, and Ethichal Hacking.