{"id":58165,"date":"2020-11-12T12:22:00","date_gmt":"2020-11-12T18:22:00","guid":{"rendered":"https:\/\/blog.cpanel.com\/?p=58165"},"modified":"2020-11-12T12:22:00","modified_gmt":"2020-11-12T18:22:00","slug":"memcached-php-applications-for-faster-web-apps","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/tips-and-tricks\/memcached-php-applications-for-faster-web-apps\/","title":{"rendered":"Memcached PHP Applications for Faster Web Apps"},"content":{"rendered":"\n
Caching is an indispensable feature of cost-effective application hosting and fast, low-latency user experiences. In-memory caching is one of the most widely used techniques, and Memcached\u2019s in-memory caching capabilities are used by thousands of developers, hosting providers, and web services giants<\/a> like Facebook\u00ae, Shopify\u00ae, and Slack\u00ae.<\/p>\n\n\n\n In this article, we explore how Memcached works and how you can integrate it with PHP apps hosted on your cPanel & WHM server.<\/p>\n\n\n\n Before we begin, a word of warning: the method outlined here should only be used on single-tenant dedicated servers, such as cPanel solo and virtual server hosting. It is not suitable for multi-tenant shared hosting environments without additional configuration to enable authentication or encryption.<\/p>\n\n\n\n Memcached<\/a> is an object caching system. It is primarily used to cache the results of database queries, helping dynamic websites like WordPress\u00ae and Drupal to serve pages faster. It can also significantly decrease resource use on a busy web server by reducing calls to the database.<\/p>\n\n\n\n Like all caches, Memcached stores data generated by an expensive operation so that it can be used again without repeating the operation. For example, to build a web page, a PHP application often has to query a relational database like MySQL. Relational databases usually store data on a hard drive or SSD, both of which are slow compared to the server\u2019s RAM. Memcached puts often-used data in RAM, allowing it to be accessed a lot faster.<\/p>\n\n\n\n Caching with Memcached works like this:<\/p>\n\n\n\n When its allocated storage is full, the cache discards the least recently used (LRU) data. Items in the cache also have an expiry date so that stale data is removed.<\/p>\n\n\n\n Now that we understand what Memcached does, let\u2019s see how to install and configure it on a cPanel web server that hosts PHP apps such as WordPress, Drupal, and Adobe Commerce (formerly Magento).<\/p>\n\n\n\n Before we begin, let\u2019s take a look at what we\u2019ll be doing to get Memcached up and running with PHP applications hosted on your server. The plan is to:<\/p>\n\n\n\n First, we\u2019ll install the Memcached daemon, the software that caches PHP objects in memory. Log in to your server with SSH<\/a> and run the following as the root user:<\/p>\n\n\n\n Next, we\u2019ll register Memcached with CentOS\u2019s Systemd service manager so that we can control when it starts and stops.<\/p>\n\n\n\n Memcached lacks built-in security features, which is why it\u2019s not a good idea to use it on shared hosting platforms. Its default configuration accepts connections from everyone on the internet, a vulnerability frequently exploited in distributed denial of service attacks<\/a>. We\u2019ll add some startup options to create a more secure configuration.<\/p>\n\n\n\n Open the config file in your preferred text editor.<\/p>\n\n\n\n Edit the last line of the file so that it reads:<\/p>\n\n\n\n The \u201c-l 127.0.0.1\u201d option binds Memcached to the local network interface. The -U option disables the UDP protocol, which is commonly used in DDoS attacks.<\/p>\n\n\n\n While we have the configuration file open, we can also change the amount of memory available for caching. The default is 64 megabytes, but you can change it by editing the CACHESIZE option in this file. For example, to double the available memory, change the line to read:<\/p>\n\n\n\n Save the file, and we\u2019re ready to start (or restart) the daemon with the secure configuration:<\/p>\n\n\n\n We need to install the EasyApache4 Memcached PHP extensions. PHP doesn\u2019t support Memcached natively, and the extensions allow them to work together. A couple of pieces of information will help you to understand what we\u2019re about to do:<\/p>\n\n\n\n We could install the extensions on the command-line with \u201cyum,\u201d but it\u2019s easier to install the right ones in WHM.<\/p>\n\n\n\n In the WHM<\/em> sidebar menu, select EasyApache 4<\/em> in the Software<\/em> section. Click the Customize<\/em> button in Currently Installed Packages<\/em>.<\/p>\n\n\n\nWhat is Memcached?<\/strong><\/h2>\n\n\n\n
Install and Configure Memcached in cPanel<\/strong><\/h2>\n\n\n\n
yum install memcached<\/code><\/pre>\n\n\n\n
systemctl enable memcached<\/code><\/pre>\n\n\n\n
Creating a Secure Memcached Configuration<\/strong><\/h2>\n\n\n\n
nano \/etc\/sysconfig\/memcached<\/code><\/pre>\n\n\n\n
OPTIONS=\"-l 127.0.0.1 -U 0\"<\/code><\/pre>\n\n\n\n
CACHESIZE=\"128\"<\/code><\/pre>\n\n\n\n
systemctl restart memcached<\/code><\/pre>\n\n\n\n
How to Install Memcached PHP Extensions in cPanel<\/strong><\/h2>\n\n\n\n