Memcached is an in-memory caching function that allows fast access and low-latency caching for PHP applications. Used by major companies like Facebook and Slack Memcached is an object caching system that is primarily used to cache results from database queries. The caching of database results allows for fast, repeated access for PHP applications with lower processing costs. We will look to install Memcached and configure Memcached on cPanel.
Memcached isn’t suitable for all types of hosting environments “out of the box”. So, If you plan to use Memcached on a dedicated server there are further prerequisites that need to be addressed. Those are outside the scope of this guide. In this guide, we are installing Memcached on a High Availability cPanel virtual server.
How Does Memcached Work?
So usually, database information is contained on your hard drive. Even if your drive is NVMe based, the resulting query would still take a lot longer than querying the server’s Ram. That’s where Memcached comes into play. The first time a query is made, PHP will search the Memcached cache for the resulting answer. If the answer is not in the cache, PHP will query the database and store the resulting information in the cache for the next query. When the Memcache cache is full, Memcached will automatically remove the least used results from the cache. But it will also set a time limit on cache results to ensure any stale data is removed.
Install PHP Memcached cPanel
So first, let’s install Memcached in our cPanel NVMe VPS Server. Issue the below command to install and enabled Memcached
yum install memcached && systemctl enable memcached
Configure Memcached cPanel
Now we have Memcached installed we must make some changes to it’s default configuration. The help prevent DDoS attacks, disable UDP. It’s a common target. We also need to bind Memchaced to the server’s local address.
Change the last line in the Memcached file so it reads this;
OPTIONS="-l 127.0.0.1 -U 0"
But it’s also a good idea to increase the capacity of the cache. I like to put this at 128MB. To increase the cache size, just modify the value in the CACHESIZE=”128″. The file should look like this.
Save and close the file. Now we need to install the Memcached PHP modules. This is completed in Easy Apache 4. From WHM select Easy Apache 4 then Customize. From the PHP Extensions page select Memcache and Memcached
Confirming This Worked
To confirm this has worked and PHP Memcahced is now installed and working you can query Easy Apache. To confirm Memcached is working for PHP 8.1 the command would be;
ea-php81 -i | grep "memcached"
So the returned information will look a lot like the below output.
/opt/cpanel/ea-php81/root/etc/php.d/memcached.ini, memcached memcached support => enabled libmemcached version => 1.0.18 memcached.compression_factor => 1.3 => 1.3 memcached.compression_threshold => 2000 => 2000 memcached.compression_type => fastlz => fastlz memcached.default_binary_protocol => Off => Off memcached.default_connect_timeout => 0 => 0 memcached.default_consistent_hash => Off => Off memcached.serializer => php => php memcached.sess_binary_protocol => On => On memcached.sess_connect_timeout => 0 => 0 memcached.sess_consistent_hash => On => On memcached.sess_consistent_hash_type => ketama => ketama memcached.sess_lock_expire => 0 => 0 memcached.sess_lock_max_wait => not set => not set memcached.sess_lock_retries => 5 => 5 memcached.sess_lock_wait => not set => not set memcached.sess_lock_wait_max => 150 => 150 memcached.sess_lock_wait_min => 150 => 150 memcached.sess_locking => On => On memcached.sess_number_of_replicas => 0 => 0 memcached.sess_persistent => Off => Off memcached.sess_prefix => memc.sess.key. => memc.sess.key. memcached.sess_randomize_replica_read => Off => Off memcached.sess_remove_failed_servers => Off => Off memcached.sess_sasl_password => no value => no value memcached.sess_sasl_username => no value => no value memcached.sess_server_failure_limit => 0 => 0 memcached.store_retry_count => 0 => 0 Registered save handlers => files user memcached
Memcached has now been installed and configured on your cPanel VPS server.
You might also like
More from cPanel
SWAP is a memory type that Linux NVMe VPS Servers use to process requests. Memory will be held in SWAP …
The Error: last request failed: [AUTH] Authentication failed error can be caused by a range of things. It could be …