Speed up PHP Using OPcache and Memcached

Speed up PHP Using OPcache and Memcached

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

April 07, 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.

Memcached (Memcache Daemon) can further enhance PHP performance by caching the data and objects in RAM. Memcached is a high-performance, distributed memory object caching system, and speed up the web applications by reducing database load by storing objects in memory. It is an in-memory key-value store, storing the objects in server memory using unique keys to reduce the number of times the data source must be read. The other popular alternative to Memcached is Redis.

There are two PHP extensions available i.e. Memcache and Memcached, being released by two different authors, which can be used to interface with the actual Memcached.

Description from PECL (Memcached, Memcache)

Memcached - PHP extension for interfacing with memcached via libmemcached library. This extension uses libmemcached library to provide API for communicating with memcached servers.

Memcache - Memcached extension. Memcached is a caching daemon designed especially for dynamic web applications to decrease database load by storing objects in memory. This extension allows you to work with memcached through handy OO and procedural interfaces.

Both the PECL packages i.e. Memcached and Memcache uses Memcached Server to store the actual data. The package Memcache is a bit older and do not work with PHP 7, whereas Memcached is good to go with PHP 7. This tutorial will only show the installation and usage of the Memcached extension since the last version of the Memcache extension was released in April 2013.

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

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 Memcached

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

# Refresh packages index
sudo apt-get update

# Install Memcached
sudo apt-get install memcached

# Install additional tools
sudo apt-get install libmemcached-tools

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

# Refresh packages index
yum update

# Install Memcached
yum install memcached

# Install client library
yum install libmemcached

Update the Memcached configuration if required. Make sure that you configure it properly to avoid Denial-of-Service attack. Also, add authenticated users and limit the network to access the server for better security.

Below mentioned are the preferred configuration changes.

-m => Set it to 1GB if sufficient memory is available

-l => Set it to either 127.0.0.1 or localhost. Always accessible from localhost only unless it's install on a different server

-U => Set it to 0 to disabled UDP

Now update the configuration file according to your needs and preferences.

# Ubuntu
sudo nano /etc/memcached.conf

# Restart the server after making changes
sudo systemctl restart memcached


# Centos
vim /etc/sysconfig/memcached

You can also enable the Memcached to start on system reboot as shown below.

# Ubuntu - No need of additional commands since Memcached will start on boot with default installation

# Centos
systemctl start memcached
systemctl enable memcached
systemctl status memcached

Now confirm whether Memcached Server is running as shown below.

# Check memcached server
ps aux | grep memcached

# It must show output like - /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1

You can also monitor your Memcached stats as shown below.

# Check the stats
memcached-tool 127.0.0.1 stats

Install Memcached PHP Extension

Install the Memcached extension as shown below.

# Ubuntu
sudo apt-get install php-memcached

# Centos
yum install php-memcached
# OR
yum install php php-pecl-memcached

You can use this PHP script to confirm whether the extension installed by us is working fine.

<?php
phpinfo();

Now run this script. You can see that the Memcached extension for PHP is installed successfully as shown in Fig 2.

Confirm Memcached extension

Fig 2

Use the below-mentioned script to confirm that the Memcached extension is working fine with the Memcached.

<?php
// Create instance
$memcached = new Memcached();

// Configure server using host and port
$memcached->addServer( "127.0.0.1", 11211 );

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

// Confirm the key
if( $result ) {

echo $result;
}
else {

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

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

These are the basic steps required to install the combination of OPcache and Memcached to speed up the PHP pages.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS