Explains all the steps required to speed up PHP based websites and web applications using OPcache and Redis.

Speed up PHP Using OPcache and Redis
Speed up PHP Using OPcache and Redis
July 14, 2019

We can speed up the PHP based websites and web applications by enabling the caching of PHP scripts at the bytecode level. The PHP code will be cached at the bytecode level, hence avoiding the server to compile and convert the PHP script to machine understandable code on every request.

We can use OPcache to compile and store the compiled PHP code in the server's memory. The precompiled code will be used to serve the subsequent requests hence improving the speed drastically. OPcache is available since the release of PHP 5.5 and it's the default caching mechanism to cache the code at the bytecode level. PHP was previously using APC which got several drawbacks as compared to OPcache.

Redis can further enhance PHP performance by caching the data and objects in RAM. The other popular alternative to Redis is Memcached. The official definition of Redis is as mentioned below.

Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. Redis has built-in replication, Lua scripting, LRU eviction, transactions and different levels of on-disk persistence, and provides high availability via Redis Sentinel and automatic partitioning with Redis Cluster.

Notes: You may also be interested in Speed up PHP Using OPcache and Memcached.

Install OPcache

We need to install the PHP extension OPcache in order to enable it.

# Ubuntu
sudo apt-get install php7.2-opcache

# Centos
yum install php-opcache

# Confirm OPCache
php -v

# It shows below mentioned output on Ubuntu system
PHP 7.2.15-0ubuntu0.18.04.2 (cli) (built: Mar 22 2019 17:05:14) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.15-0ubuntu0.18.04.2, Copyright (c) 1999-2018, by Zend Technologies

You can also confirm the installation using the output of phpinfo() function as shown in Fig 1.

Confirm OPcache

Fig 1

You can further enhance OPcache by updating the configuration file i.e. php.ini.

opcache.interned_strings_buffer=4
opcache.max_accelerated_files=2000
opcache.memory_consumption=64
opcache.revalidate_freq=2
opcache.fast_shutdown=0
opcache.enable_cli=0
opcache.interned_strings_buffer=4
opcache.max_accelerated_files=2000
opcache.memory_consumption=64
opcache.revalidate_freq=2
opcache.fast_shutdown=0
opcache.enable_cli=0

Install Redis

Use below-mentioned commands to install Redis on Ubuntu 18.04 LTS. The same commands can be used on other Debian systems.

# Refresh packages index
sudo apt-get update

# Install Redis
sudo apt-get install redis-server

We can install Redis on Centos 7 using the below-mentioned command.

# Install EPEL
yum install epel-release

# Refresh packages index
yum update

# Install Redis
yum install redis

# Start Redis
systemctl start redis

# Start Redis on System boot
systemctl enable redis

Update the Redis configuration if required.

Test Redis

After installing Redis, we can test whether its store is working fine. Connect to the Redis server on the console using the command mentioned below.

# Connect to Redis
redis-cli

Now test the connectivity with Redis using below-mentioned command.

# Test connectivity
127.0.0.1:6379> ping

It must confirm the connectivity by showing PONG in response to ping. This will confirm that the Redis server is up and available.

Now we will test the Redis store using below mentioned commands.

# Set Key & Value
127.0.0.1:6379> set key1 "Hello Redis !!"
OK

# Retrieve the Key Value
127.0.0.1:6379> get key1
"Hello Redis !!"

# Disconnect
127.0.0.1:6379> exit

The above-mentioned commands confirm that Redis is working fine and we can use its store to manage the key-value pairs.

Install PHP Extension

We also need to install the PHP extension of Redis in order to use it directly from PHP programs. Use the commands as mentioned below.

# Install PHP Redis Extension on Ubuntu
apt-get install php-redis

# Install PHP Redis Extension on CentOS
pecl install igbinary igbinary-devel redis
# OR using remi's repository
yum install php-pecl-redis

# Test the extension
php -m | grep redis

We can install the PHP extension of Redis on Ubuntu and CentOS using the above-mentioned commands. Also, restart the apache server after installing the PHP extension.

# Restart apache server
systemctl restart apache2.service

We can also confirm the Redis extension for PHP by checking the output of phpinfo() as shown in Fig 2.

Redis Extension

Fig 2

Test Redis Extension

In this section, we will test the Redis by accessing it from the PHP program and create, read and remove the key-value pair. The below-mentioned code can be used to test the Redis extension.

<?php
// Create instance
$redis = new Redis();

// Configure server using host and port
$redis->connect( '127.0.0.1', 6379 );

// Get a key
$result = $redis->get( "key1" );

// Confirm the key
if( $result ) {

echo $result;
}
else {

echo "No results found for the key - key1. Adding the key to Redis.";

$redis->set( "key1", "Hello Redis !!" ) or die( "Unable to add key to Redis. Please check with the installation." );
}

This is how we can effectively use the combination of OPcache and Redis to speed up PHP by caching at both bytecode level and data level. The Redis server can also be used to cache the data which remains consistent and frequently retrieved from the database to avoid additional calls to the database.

Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS