Setting up and configuring memcached for database caching purpose on Debian/Ubuntu LTS Server is quite easy. The memcached and memcache PHP extensions is very useful for database caching purposes, especially if you’re running WordPress CMS on your site. It of course can reduce server load CPU usage and memory (RAM) consumption.
Low-end cloud server or budget VPS hosting is strongly recomended to using memcached or redis for database caching. Well, on this today guide, I will teach you on how to install and configure memcached database cache on Debian Ubuntu LTS Server. This can be applied on Debian 8/9/10 and Ubuntu 14.04 LTS to 20.04 LTS.
REQUIREMENTS:
- VPS/Cloud Server running Debian Ubuntu LTS
- Server IPv4 Address
- Full root access
- GNome Terminal for Linux
- PuTTy SSH Client for Windows
It’s quite easy, simply read the following tutorial to deploy memcached database cache on your virtual machine.
1.) Step One, install and configure Memcached + Memcache Extensions
$ apt-get install memcached php-memcached php-memcache $ apt-get install libmemcached-tools $ apt-get install -y php php-dev php-pear $ apt-get install libevent-dev
2.) Next, for Apache web server, you should install the following PHP extension.
$ apt install libapache2-mod-php
3.) Now, let’s edit memcached.conf file
And then find variable “-m 64”, after that simply change that variable to 256 or higher values, for example 512 if your machine having more memory or RAM. By default is 64 MB, so it’s pretty a small amount. Let’s change it to 256.
$ nano /etc/memcached.conf ...... # Start with a cap of 64 megs of memory. It's reasonable, and the daemon default # Note that the daemon will grow to this size, but does not start out holding this much # memory -m 256 # Default connection port is 11211 -p 11211 # Run the daemon as root. The start-memcached will default to running as root if no # -u command is present in this config file -u memcache ....
4.) Edit memcache.ini file inside mods-available directories, depending on your PHP version.
$ nano /etc/php/8.0/mods-available/memcache.ini $ nano /etc/php/7.4/mods-available/memcache.ini $ nano /etc/php/7.3/mods-available/memcache.ini $ nano /etc/php/7.2/mods-available/memcache.ini $ nano /etc/php/7.1/mods-available/memcache.ini $ nano /etc/php/7.0/mods-available/memcache.ini $ nano /etc/php/5.6/mods-available/memcache.ini
Then, put the following snippet code at the bottom line of the files.
; Use memcache as a session handler session.save_handler = memcache session.save_path = "tcp://localhost:11211"
5.) Restart Memcached, Web Server and Other Following Services.
To enabled memcached from boot
$ systemctl enable memcached.service
Restart memcached daemon
$ service memcached restart
Restart Apache2 Web Server
$ service apache2 restart
To Restart Nginx Web Server
$ service nginx restart
If you’re using PHP-FPM on your virtual machine, simply run the following command to restart php-fpm services.
$ service php5.6-fpm restart $ service php7.0-fpm restart $ service php7.1-fpm restart $ service php7.2-fpm restart $ service php7.3-fpm restart $ service php7.4-fpm restart $ service php8.0-fpm restart
CONCLUSION
Deploying memcached for database cache on Debian Ubuntu LTS machine is quite simple and easy to applied. You just need relax time to perform this task.