Now Reading
Speed up PHP Using OPcache and Redis
Dark Light

Speed up PHP Using OPcache and Redis

Avatar
flat screen computer monitor displaying white and black screen

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. # Ubuntusudo apt-get install php7.2-opcache# Centosyum install php-opcache# Confirm OPCachephp -v# It shows below mentioned output on Ubuntu systemPHP 7.2.15-0ubuntu0.18.04.2 (cli) (built: Mar 22 2019 17:05:14) ( NTS )Copyright (c) 1997-2018 The PHP GroupZend 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. Fig 1 You can further enhance OPcache by updating the configuration file i.e. php.ini. opcache.interned_strings_buffer=4opcache.max_accelerated_files=2000opcache.memory_consumption=64opcache.revalidate_freq=2opcache.fast_shutdown=0opcache.enable_cli=0opcache.interned_strings_buffer=4opcache.max_accelerated_files=2000opcache.memory_consumption=64opcache.revalidate_freq=2opcache.fast_shutdown=0opcache.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 indexsudo apt-get update# Install Redissudo apt-get install redis-server We can install Redis on Centos 7 using the below-mentioned command. # Install EPELyum install epel-release# Refresh packages indexyum update# Install Redisyum install redis# Start Redissystemctl start redis# Start Redis on System bootsystemctl 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 Redisredis-cli Now test the connectivity with Redis using below-mentioned command. # Test connectivity127.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 & Value127.0.0.1:6379> set key1 “Hello Redis !!”OK# Retrieve the Key Value127.0.0.1:6379> get key1″Hello Redis !!”# Disconnect127.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 Ubuntuapt-get install php-redis# Install PHP Redis Extension on CentOSpecl install igbinary igbinary-devel redis# OR using remi’s repositoryyum install php-pecl-redis# Test the extensionphp -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 serversystemctl restart apache2.service We can also confirm the Redis extension for PHP by checking the output of phpinfo() as shown in Fig 2. 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. How To Install PHP For Nginx On Ubuntu 20.04 LTS

Touch The Stars

0 / 5

Your page rank:

What's Your Reaction?
Excited
0
Funny
0
In Love
0
Like It
0
Not Sure
0
View Comments (0)

Leave a Reply

Your email address will not be published.


Copyrights © 2021 Lucid.Hive.Systems. All Rights Reserved.