Install OPCache to Improve PHP Performance in CentOS 7

PHP is one of the most popular programming language for developing applications, you will find it on every web hosting server. The most popular Content Management Systems (CMSs) are written in PHP, such as WordPress, Drupal, and Joomla.

One of the many reasons why PHP is well known out there is because it has numerous extensions in its default distribution, an example is OPcahce.

What is Opcache and How Does it Work?

Originally known as Zend Optimizer+, Opcache (introduced in PHP 5.5.0) is a powerful PHP extension built to enhance PHP performance thus boosting overall application performance. It is available as an extension through PECL for PHP versions 5.2, 5.3 and 5.4. It works by storing pre-compiled script byte-code in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

In this article, we will explain how to install and configure OPcache in CentOS 7 for a specific PHP version.

Install Opcache PHP Extension in CentOS 7

1. First start by installing the EPEL repository and followed by the REMI repository on your system, as follows.

# yum update && yum install epel-release
# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm  

2. Next, you need to install yum-utils, a collection of utilities to extend yum’s default features; they help you manage yum repositories as well as packages without any manual configuration and more.

# yum install yum-utils

3. Once you have yum-utils installed, use yum-config-manager to enable Remi repository as the default repository for installing different PHP versions and modules.

# yum-config-manager --enable remi-php55		#For PHP 5.5
# yum-config-manager --enable remi-php56		#For PHP 5.6
# yum-config-manager --enable remi-php70 		#For PHP 7.0
# yum-config-manager --enable remi-php71		#For PHP 7.1
# yum-config-manager --enable remi-php72		#For PHP 7.2

4. Now install Opcache extension and verify your PHP version to confirm that it has Opcache extension installed using following commands.

# yum install php-opcache		
# php -v
Check OpCache PHP Extension

Check OpCache PHP Extension

Configure Opcache PHP Extension in CentOS 7

5. Next, configure OPcache by editing the /etc/php.d/10-opcache.ini (or /etc/php.d/10-opcache.ini) file using your favorite editor.

# vim /etc/php.d/10-opcache.ini

The following settings should get you started with using OPcache and are generally recommended as good performance. You can enable a configuration by uncommenting it.

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

6. Finally, restart your web server for Opcache to start working.

# systemctl restart nginx
OR
# systemctl restart httpd

That’s all! Opcache is a PHP extension built to improve PHP performance. In this article, we have explained how to install and configure OPcache in CentOS 7. If you have any questions, reach us via the comments form below.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Aaron Kili

Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

6 Responses

  1. Nate says:

    Hello Aaron. Thanks for the great advice. I followed your advice here and it REALLY speed things up with WordPress (since it is a PHP script essentially). I have PHP 7.2 and checking the php -v prior to following your advice opcache was not enabled. The speed boost was truly worth the effort! Much appreciated!

  2. Pavel says:

    Oh, what I’m saying – actually nginx IS a web server, but in this case it’s still better to restart php-fpm instead of nginx

  3. Pavel says:

    nginx isn’t web-server, it’s better to restart php-fpm in such case :)

  4. Kyle says:

    To be more secure and faster, if you install php7. Turns out opcaching is already compiled into it.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.