Caching is one of the best friends of webmasters when they want to optimize their website performance. Be it on the server or on the site itself – this functionality helps you speed up the loading of your pages and minimize the wait time for your visitors as much as possible.
In this guide, we will learn more about Memcached and how we can configure it on our VPS.
What is Memcached?
Memcached is a simple yet effective solution for memory caching. Hosting providers and server admins often use it to cache database data, page rendering chunks, API calls, and many more. The result is a visible speed boost, especially in dynamic apps and websites.
You can have a very large hash table and distribute it across multiple servers through the Memcached APIs. If that table fills to the maximum – old cached data will be deleted, starting from the least recently used records.
Memcached was originally written in Perl and publicly released in 2003. Since then, it has gone through some rewrites, making it more adaptable to different systems. Today, Memcached is integrated into many of the biggest and most popular websites like Facebook, Twitter, Youtube, Wikipedia, Reddit, and more.
Configuring Memcached in cPanel
Managed VPS providers always take care of the initial server configuration, and setting up Memcached is often part of the process. As for the self-managed virtual server clients, they can configure the tool via a couple of simple commands.
Use your favorite SSH client to log into your machine. Install the daemon via:
yum install memcached
To control how Memcached operates, we also need to register it with the service manager. If you are in a CentOS environment, the command is:
systemctl enable memcached
After the initial installation and setup, we need to consider its security as well.
How to Secure Your Memcached Configuration?
By default, Memcached has no in-built security features and keeps connections open to anyone. As you have guessed, that may lead to many issues and web attacks (like a DDoS) down the line. Luckily, there are some easy tricks to harden your defenses.
For starters, you can open your Memcached config file with a text editor of your choice. It should be in:
/etc/sysconfig/memcached
Once inside, edit the last line so it reads:
OPTIONS=”-l 127.0.0.1 -U 0″
This disables the UDP protocol and ties Memcached to your local network only.
Additionally, you can check your CACHESIZE. This is the option that tells your server how much memory should be allocated to caching processes. Many hosts keep the default at 64 MB, and you need to increase that to at least 128 MB:
CACHESIZE=”128″
You can now restart the daemon for the changes to take effect:
systemctl restart Memcached
IMPORTANT: ScalaHosting keeps the CACHESIZE limits very high by default – 512 MB. This guarantees plenty of RAM is always available for caching purposes on our cloud VPS plans.
Configuring Memcached in SPanel
If you are a ScalaHosting customer on one of the SPanel VPS plans – you already have Memcached installed and ready for you. Additionally, our tech support team will gladly assist and save you some time if you are looking to change things like the default CACHESIZE or raising any other limits.
You simply need to send a ticket request from your Client Area, and our trained technicians will take care of any configurations for you.
Installing Memcached PHP Extensions
As PHP doesn’t natively support Memcached, so you’ll need some extensions to tie them together. The first thing you need to do is check your PHP version, which will help you find the corresponding Memcached version. If you are running multiple PHP instances – you’ll need to configure the caching solution for each one of them.
IMPORTANT: There are two solutions with pretty confusing names – Memcached and Memcache. The latter is essentially the PHP extension for the Memcached daemon.
Here is how to install Memcache extension via WHM:
- Log into the Web Host Manager.
- Under the Software section on the left menu, click on EasyApache 4.
- This will bring an option Currently Installed Packages. Click on the blue Customize button next to it.
- You will see a bunch of sub-menus – PHP extensions is the one you need.
- Activate the Install options to the Memcache versions of your choice.
- Next, select the Review tab and click the Provisions button at the bottom of the page.
After a short wait, the selected PHP extensions and their dependencies will be installed. You can now go to your SSH client and restart Memcached on your VPS:
systemctl restart memcached
That should do the trick, and you can start enjoying faster apps with the help of Memcached.
Down the line, you can always check useful statistics about your caching with the following command:
memcached-tool 127.0.0.1:11211 stats
Our only step now is to tie our chosen PHP apps to properly work with Memcached.
Configuring PHP Apps for Memcached
The process of integrating Memcached with a PHP app would highly depend on your choice of software. Here are some of the best plug-ins for some of the most popular CMS platforms:
- WordPress – W3 Total Cache, Cachify, Cache Master, and more.
- Joomla – comes with native Memcached support. Activate through the config file.
- Magento – comes with native Memcached support.
- Drupal – Memcache API and Integration module.
If you have more questions about Memcached and how you can use caching to your advantage – contact the Scala Team, and we will gladly help with more info.