{"id":2592,"date":"2021-12-02T16:00:43","date_gmt":"2021-12-02T14:00:43","guid":{"rendered":"https:\/\/www.scalahosting.com\/kb\/?p=2592"},"modified":"2022-01-27T16:34:39","modified_gmt":"2022-01-27T14:34:39","slug":"how-to-configure-memcached-on-your-vps","status":"publish","type":"post","link":"https:\/\/www.scalahosting.com\/kb\/how-to-configure-memcached-on-your-vps\/","title":{"rendered":"How to Configure Memcached on Your VPS"},"content":{"rendered":"<p><span style=\"font-weight: 400;\">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 &#8211; this functionality helps you speed up the loading of your pages and minimize the wait time for your visitors as much as possible.&nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">In this guide, we will learn more about <\/span><b>Memcached <\/b><span style=\"font-weight: 400;\">and how we can configure it on our VPS.&nbsp;<\/span><\/p>\n<h2><b>What is Memcached?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">Memcached is a simple yet effective solution for<\/span><b> memory caching<\/b><span style=\"font-weight: 400;\">. Hosting providers and server admins often use it to cache <\/span><b>database data<\/b><span style=\"font-weight: 400;\">, <\/span><b>page rendering chunks<\/b><span style=\"font-weight: 400;\">, <\/span><b>API calls<\/b><span style=\"font-weight: 400;\">, and many more. The result is a visible speed boost, especially in dynamic apps and websites.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You can have a very large hash table and <\/span><b>distribute it across multiple servers<\/b><span style=\"font-weight: 400;\"> through the Memcached APIs. If that table fills to the maximum &#8211; old cached data will be deleted, starting from the least recently used records.&nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Memcached was originally written in <\/span><b>Perl <\/b><span style=\"font-weight: 400;\">and publicly released in <\/span><b>2003<\/b><span style=\"font-weight: 400;\">. 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 <\/span><b>Facebook<\/b><span style=\"font-weight: 400;\">, <\/span><b>Twitter<\/b><span style=\"font-weight: 400;\">, <\/span><b>Youtube<\/b><span style=\"font-weight: 400;\">, <\/span><b>Wikipedia<\/b><span style=\"font-weight: 400;\">, <\/span><b>Reddit<\/b><span style=\"font-weight: 400;\">, and more.&nbsp;<\/span><\/p>\n<h2><b>Configuring Memcached in cPanel<\/b><\/h2>\n<p><a href=\"https:\/\/www.scalahosting.com\/managed-cloud-hosting.html\"><b>Managed VPS providers<\/b><\/a><span style=\"font-weight: 400;\"> always take care of the initial server configuration, and setting up Memcached is often part of the process. As for the <\/span><a href=\"https:\/\/www.scalahosting.com\/cloud-vps-hosting.html\"><b>self-managed virtual server<\/b><\/a><span style=\"font-weight: 400;\"> clients, they can configure the tool via a couple of simple commands.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Use your favorite <\/span><b>SSH client<\/b><span style=\"font-weight: 400;\"> to log into your machine. Install the daemon via:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">yum install memcached<\/span><\/p>\n<p><span style=\"font-weight: 400;\">To control how Memcached operates, we also need to <\/span><b>register it with the service manager<\/b><span style=\"font-weight: 400;\">. If you are in a CentOS environment, the command is:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">systemctl enable memcached<\/span><\/p>\n<p><span style=\"font-weight: 400;\">After the initial installation and setup, we need to consider its security as well.<\/span><\/p>\n<h2><b>How to Secure Your Memcached Configuration?<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">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.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">For starters, you can open your Memcached config file with a text editor of your choice. It should be in:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">\/etc\/sysconfig\/memcached<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Once inside, edit the last line so it reads:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">OPTIONS=&#8221;-l 127.0.0.1 -U 0&#8243;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">This disables the UDP protocol and ties Memcached to your local network only.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Additionally, you can check your <\/span><b>CACHESIZE<\/b><span style=\"font-weight: 400;\">. This is the option that tells your server how much memory should be allocated to caching processes. Many hosts keep the default at<\/span><b> 64 MB<\/b><span style=\"font-weight: 400;\">, and you need to increase that to at least <\/span><b>128 MB<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">CACHESIZE=&#8221;128&#8243;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You can now restart the daemon for the changes to take effect:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">systemctl restart Memcached<\/span><\/p>\n<p><b>IMPORTANT: <\/b><span style=\"font-weight: 400;\">ScalaHosting keeps the CACHESIZE limits very high by default &#8211; <\/span><b>512 MB<\/b><span style=\"font-weight: 400;\">. This guarantees plenty of RAM is always available for caching purposes on our cloud VPS plans.<\/span><\/p>\n<h2><b>Configuring Memcached in SPanel<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">If you are a ScalaHosting customer on one of the <\/span><b>SPanel VPS plans<\/b><span style=\"font-weight: 400;\"> &#8211; 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.&nbsp;<\/span><\/p>\n<p><span style=\"font-weight: 400;\">You simply need to <\/span><b>send a ticket request<\/b><span style=\"font-weight: 400;\"> from your <\/span><b>Client Area<\/b><span style=\"font-weight: 400;\">, and our trained technicians will take care of any configurations for you.<\/span><\/p>\n<h2><b>Installing Memcached PHP Extensions<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">As PHP doesn&#8217;t natively support Memcached, so you&#8217;ll need some extensions to tie them together. The first thing you need to do is <\/span><b>check your PHP version<\/b><span style=\"font-weight: 400;\">, which will help you find the corresponding Memcached version. If you are running multiple PHP instances &#8211; you&#8217;ll need to configure the caching solution for each one of them.<\/span><\/p>\n<p><b>IMPORTANT:<\/b><span style=\"font-weight: 400;\"> There are two solutions with pretty confusing names &#8211; Memcached and Memcache. The latter is essentially the PHP extension for the Memcached daemon.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Here is how to install Memcache extension via <\/span><b>WHM<\/b><span style=\"font-weight: 400;\">:<\/span><\/p>\n<ol>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Log into the <\/span><b>Web Host Manager<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Under the <\/span><b>Software <\/b><span style=\"font-weight: 400;\">section on the left menu, click on <\/span><b>EasyApache 4<\/b><span style=\"font-weight: 400;\">.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">This will bring an option <\/span><b>Currently Installed Packages<\/b><span style=\"font-weight: 400;\">. Click on the blue <\/span><b>Customize<\/b><span style=\"font-weight: 400;\"> button next to it.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">You will see a bunch of sub-menus &#8211; <\/span><b>PHP extensions<\/b><span style=\"font-weight: 400;\"> is the one you need.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Activate the <\/span><b>Install <\/b><span style=\"font-weight: 400;\">options to the Memcache versions of your choice.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><span style=\"font-weight: 400;\">Next, select the <\/span><b>Review <\/b><span style=\"font-weight: 400;\">tab and click the <\/span><b>Provisions <\/b><span style=\"font-weight: 400;\">button at the bottom of the page.<\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">After a short wait, the selected PHP extensions and their dependencies will be installed. You can now go to your SSH client and <\/span><b>restart Memcached<\/b><span style=\"font-weight: 400;\"> on your VPS:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">systemctl restart memcached<\/span><\/p>\n<p><span style=\"font-weight: 400;\">That should do the trick, and you can start enjoying faster apps with the help of Memcached.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Down the line, you can always <\/span><b>check useful statistics<\/b><span style=\"font-weight: 400;\"> about your caching with the following command:<\/span><\/p>\n<p><span style=\"font-weight: 400;\">memcached-tool 127.0.0.1:11211 stats<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Our only step now is to tie our chosen PHP apps to properly work with Memcached.&nbsp;<\/span><\/p>\n<h2><b>Configuring PHP Apps for Memcached<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">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:<\/span><\/p>\n<ul>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>WordPress <\/b><span style=\"font-weight: 400;\">&#8211; W3 Total Cache, Cachify, Cache Master, and more.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Joomla <\/b><span style=\"font-weight: 400;\">&#8211; comes with native Memcached support. Activate through the config file.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Magento <\/b><span style=\"font-weight: 400;\">&#8211; comes with native Memcached support.<\/span><\/li>\n<li style=\"font-weight: 400;\" aria-level=\"1\"><b>Drupal <\/b><span style=\"font-weight: 400;\">&#8211; Memcache API and Integration module.<\/span><\/li>\n<\/ul>\n<p><span style=\"font-weight: 400;\">If you have more questions about Memcached and how you can use caching to your advantage &#8211; <\/span><a href=\"https:\/\/www.scalahosting.com\/contact-us.html\"><b>contact the Scala Team<\/b><\/a><span style=\"font-weight: 400;\">, and we will gladly help with more info.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8211; this functionality helps you speed up the loading of your pages and minimize the wait time for your visitors as much as possible.&nbsp; In this guide, we [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_seopress_robots_primary_cat":"","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[27],"tags":[],"class_list":["post-2592","post","type-post","status-publish","format-standard","hentry","category-vps-hosting"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/2592","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/comments?post=2592"}],"version-history":[{"count":7,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/2592\/revisions"}],"predecessor-version":[{"id":2897,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/posts\/2592\/revisions\/2897"}],"wp:attachment":[{"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/media?parent=2592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/categories?post=2592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scalahosting.com\/kb\/wp-json\/wp\/v2\/tags?post=2592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}