Install and Configure OpCache on Ubuntu Server

By on December 7th, 2017

Install and Configure OpCache on Ubuntu Server

One of the great things about any website is its load time. One of the best ways to decrease the load time is enabling cache on the server. OpCache is an opcode cache, which will increase the speed of PHP websites by storing precompiled script bytecode in shared memory, it simply means any consequent requests for the same script then OpCache stores this script on it memory on the first execution, to be reused afterward, thus leading to performance boosts.

 

Steps to Install and Configure OpCache

1) Install the latest version of OpCache by following the below steps. In this documentation, I’m installing 7.0.5.

# apt-get install -y php-pear

# apt-get install -y build-essential php5-dev

# pecl install zendopcache-7.0.5

 

2) Configure OpCache.

Now it’s time to configure OpCache. In Ubuntu all conf files in /etc/php5/conf.d, which are symlinked from /etc/php5/mods-available. Here’s how to set that up:

 

3) Find the location of opcache. so file using the below command.

# find / -name ‘opcache.so’

The output will be something similar to the below result.

/usr/lib/php5/20100525/opcache.so

 

4) Now you can edit the OpCache configuration file called opcache.ini file and make the changes similar to the below example.

# vi /etc/php5/mods-available/opcache.ini

# Add the following to opcache.ini

zend_extension=/usr/lib/php5/20100525/opcache.so # File path from above

opcache.memory_consumption=128

opcache.interned_strings_buffer=8

opcache.max_accelerated_files=4000

opcache.revalidate_freq=60

opcache.fast_shutdown=1

opcache.enable_cli=1

opcache.save_comments=0

 

5) Now create a symlink for this ini file.

# ln -s ../mods-available/opcache.ini 20-opcache.ini

 

6) Finally, you need to restart php-fpm and apache for reflecting the changes that you made.

# service php5-fpm restart

# service apache2 restart

 

7) You can also verify the installation by running the below command.

# php -v

PHP 5.4.17RC1 (cli) (built: Jun 22 2013 19:27:26)

Copyright (c) 1997-2013 The PHP Group

Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

with Zend OPcache v7.0.2, Copyright (c) 1999-2013, by Zend Technologies

You can also verify the installation by using the below command.

# php -i | grep opcache

 

8) Now you can delete all unwanted dependencies by running the below command.

# apt-get remove –purge build-essential php5-dev

After installing the OpCache all sites with PHP scripts will load faster in high traffic. The script will automatically be caching and will load smoothly.

 

If you have any additional questions or concerns, please contact support@interserver.net.

 

 

Leave a Reply