{"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\n Select the PHP Extensions <\/em>tab and search for \u201cmemcached\u201d. cPanel shows you extensions for installed PHP versions. Click the switch on those you would like to install.<\/p>\n\n\n\n Select the Review<\/em> tab, and then click the Provision<\/em> button at the bottom of the page. cPanel will install the extensions and their dependencies.<\/p>\n\n\n\n With the extensions installed, return to your SSH session and restart memcached:<\/p>\n\n\n\n The building blocks are in place, and memcached should be ready to start caching, but let\u2019s make sure that everything went as planned. First, we\u2019ll verify that memcached is working with PHP.<\/p>\n\n\n\n We\u2019re asking the system\u2019s EasyApache4 PHP 7.3 installation to display its internal configuration data and filtering the results with \u201cgrep\u201d to extract the relevant lines. For different versions of PHP, replace the \u201c73\u201d. For example, if your apps use PHP 7.4, the command should start with \u201cea-php74\u201d.<\/p>\n\n\n\n If all is well, PHP will print a lot of data, starting with lines that look like this:<\/p>\n\n\n\n To see similar information in your web browser, create a file called phpinfo.php<\/em> in your domain\u2019s public_html<\/em> directory with the following contents:<\/p>\n\n\n\n Save the file and visit it in a browser tab.<\/p>\n\n\n\n Replace example.com<\/em> with your domain. You should delete this file once you no longer need it. It contains information about your server that could reveal security vulnerabilities to bad actors.<\/p>\n\n\n\n If you\u2019re curious how much information is cached and other statistics, you can find out with the command:<\/p>\n\n\n\n However, we haven\u2019t hooked any PHP applications up to the cache yet, so you won\u2019t see much activity. Let\u2019s fix that.<\/p>\n\n\n\n We have Memcached running on the server, but apps won\u2019t use it until we tell them to. The process differs depending on the app, but Memcached extensions are available for most popular content management systems and ecommerce stores.<\/p>\n\n\n\n Follow the Memcached documentation for your PHP app or plugin. They may autodetect Memcached, and all you\u2019ll have to do is turn caching on. However, you may need to enter the IP address and port the cache is connected to, which is 127.0.0.1:11211. The port is the part after the colon: 11211.<\/p>\n\n\n\n Memcached can significantly reduce the load on busy servers, helping server administrators to control hosting costs while accelerating PHP apps for an improved user experience. cPanel and WHM\u2019s EasyApache4 PHP management tools make it easy to install and manage the extensions you need to use Memcached with PHP.<\/p>\n\n\n\n As always, if you have any feedback or comments, please let us know. We are here to help in the best ways we can. You\u2019ll find us on Discord<\/a>, the cPanel forums<\/a>, and Reddit<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":" 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 like Facebook\u00ae, Shopify\u00ae, and Slack\u00ae. In this article, we explore how Memcached works and […]<\/p>\n","protected":false},"author":77,"featured_media":65577,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[61],"tags":[],"class_list":["post-58165","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tips-and-tricks"],"acf":[],"yoast_head":"\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
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
systemctl restart memcached<\/code><\/pre>\n\n\n\n
ea-php73 -i | grep \"memcached\"<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
<?php \n phpinfo(); \n?><\/code><\/pre>\n\n\n\n
http://example.com\/phpinfo.php<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
memcached-tool 127.0.0.1:11211 stats<\/code><\/pre>\n\n\n\n
How to Configure PHP Apps to Work With Memcached<\/strong><\/h2>\n\n\n\n