If you currently have a WordPress-based website, you will know how important the database cache for your WP site.
Especially if your site has super busy traffic every day. Of course, the role of database cache will be very important to balance your server load, and can help your site to handle high traffic.
There are actually two database cache options that we can use on VestaCP server, including Memcached and Redis. However, in this guide I will show you how to install Memcached on a CentOS 7 server with Vesta Control Panel.
Install & Tune Memcached
This guide should work well on CentOS 6, but it requires a number of different settings. Well, installing Memcached together with its Memcache extension is very easy.
1. InstallMemcached andMemcache extension
First, install Memcached and the Memcache extension. Login to your CentOS 7 server as “root user” and then run this commands.
$ yum install memcached php-pecl-memcached php-pecl-memcache
2. AdjustMemcached settings
You can customize Memcached settings according to your WordPress site requirements. However, make sure to adjust the values in the Memcached configuration to match your server specifications, especially memory allocation.
OK, now open the Memcached configuration file.
$ nano /etc/sysconfig/memcached
; Default memcached config
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="64"
OPTIONS=""
The default values are as you can see above. Well, you can adjust it according to your needs. For example, we are allowed to change the default value of CACHESIZE="64"
to 128 or 256, and so on.
The CACHESIZE
value is the maximum amount of memory allocation that will be used by Memcached. Please note that the default value of CACHESIZE="64"
is a memory allocation of 64MB which will be used for object storage.
So if you change it to 256 or 512 then we will need a memory allocation of 256MB or 512MB. So make sure your server has enough memory (RAM) allocation.
Meanwhile, MAXCONN
is the maximum number of simultaneous connections allowed by Memcached. For example, we can increase the MAXCONN
value from 1024 to 2048 or 3072, according to our server’s needs.
Especially if you have a very busy server, you can increase the CACHESIZE
value up to 2GB and MAXCONN
4096. But make sure your server has enough free memory allocation.
Then if you want to change the default Memcached port, make sure to add your custom Memcached port to the whitelist on your Firewall rule. For example, we can change the default Memcached port from 11211 to 37509.
; Custom memcached config
PORT="37509"
USER="memcached"
MAXCONN="2048"
CACHESIZE="256"
OPTIONS=""
3. Modify50-memcached.iniand 40-memcache.ini files
Next, open the 50-memcached.ini and 40-memcache.ini files in the /etc/php.d/
and /etc/php-zts.d/
directories respectively. Then UNCOMMENT the lines session.save_handler
and session.save_path
as follows:
$ nano /etc/php-zts.d/50-memcached.ini
; Use memcache as a session handler
session.save_handler=memcached
; Defines a comma separated list of server urls to use for session storage
session.save_path="localhost:37509"
$ nano /etc/php-zts.d/40-memcache.ini
; Use memcache as a session handler
session.save_handler=memcache
; Defines a comma separated of server urls to use for session storage
session.save_path="tcp://localhost:37509?persistent=1&weight=1&timeout=1&retry_interval=15"
4. Adjust if you’re usingNginx + PHP-FPM
Well, if your VestaCP uses Nginx + PHP-FPM, make sure to adjust the php.ini values like this:
$ session.save_handler = files
$ session.save_path = /var/lib/php/session
$ soap.wsdl_cache_dir = /var/lib/php/wsdlcache
Then set directory permissions to 777 and 775:
$ chmod 777 /var/lib/php/session/
$ chmod 775 /var/lib/phpMyAdmin/temp/
5. Restart
All DONE…!!! Restart your Memcached service and web server.
$ service memcached restart
$ chkconfig memcached on
$ service php-fpm restart
$ service nginx restart
If you’re using an Apache web server (HTTPD), simply run the following command:
$ service httpd restart
6. Install cache plugin
Finally, if you use a WordPress-based site, you can install the W3 Total Cache plugin in order to integrate Memcached into your WP site.
Then make sure to enable Memcached as Database Cache and Object Cache Methods. For more details about WordPress + Memcached + W3 Total Cache, I will give you a more complete tutorial next time.
Conclusion
Installing Memcached on a CentOS 7 server with VestaCP is very easy. If you run a WordPress-based site on your VestaCP server, be sure to always install Memcached to reduce CPU load and to make your server environment more stable, and more scalable.
The most important thing is that Memcached can improve your site’s load time much faster, thanks to the Memcached database cache which can be seamlessly integrated into the WordPress CMS.
We are a bunch of people who are still continue to learn Linux servers. Only high passion keeps pushing us to learn everything.