1. Home
  2. Cloud and Servers
  3. Virtual Servers (VPS)
  4. Installing Memcached on Ubuntu 18.04 LTS

Installing Memcached on Ubuntu 18.04 LTS

Memcached is a high performance system to store objects to the memory, developed mostly to increase the scaling of web application with focus on decreasing load for database and file system. Its performance is shown best during a high number of paralel object loads, thanks to its flexible access to the memory.

In most cases accessing the disc is the most common place where the application is slowed especially if the application is more demanding on SQL database.

After completing this guide you will have stored sessions to the memcache and memcache backend will be available for caching inside your application.


Guide to installing Memcached on virtual server

Tip: Before you use this guide, we would recommend to gain some basic overview about installing packages on the server and editing configuration via console.

1. Updating repositories and installing updates

sudo apt-get update -y apt-get upgrade -y

2. Installing packages for Memcached

For operating system Ubuntu 18.04 LTS packages in the repositories and can be installed with:

sudo apt-get install memcached libmemcached-tools -y

3. After the installation you can run the Memcached and add it to the automated run after the system boots.

sudo systemctl start memcached
sudo systemctl enable memcached

4. State of Memcached can be check with a command

systemctl status memcached

5. Change of settings

Memcached configuration is located in a file /etc/memcached.conf in which you can edit its parameters according to your needs. Amongst the most common there are:

Change of standart port 11211

-p 11211

IP address where the Memcached operates. Setting any other as local (127.0.0.1) shloud be done only if you need to get to the Memcached from a different server.

-l 192.168.0.10

Define maximum amount of memory, that Memcached has available.

-m 256

After you edit and save the configuration in you preferred editor, you need to restart Memcached sot the setting would take effect.

sudo systemctl restart memcached

Installing Apache and PHP and their configuration with Memcached

If you want to use Memcached in addition with a web server Apache and PHP you need to install a module for it.

Tip: Ubuntu 18.04 LTS uses PHP in version 7.2. If you have any other version you want to have a different version of PHP e.g. from a repository of a third party, it has to contain suc module.

1. Guide for installation of Apache2, PHP and module Memcached

sudo apt-get install apache2 php7.2 libapache2-mod-php7.2 php-memcached php7.2-cli -y

2. Restart the services

sudo systemctl restart apache2

3. Check the settings

The version can be tested with a help of phpinfo
Create file phpinfo.php

sudo nano /var/www/html/phpinfo.php

add

<?php
phpinfo
();

Save the file and open the web browser on a page http://ip-of_your-server/phpinfo.php – on the website locate the part with memcached


Storing sessions to the Memcached

In the standart PHP configuration is session stored to the file, or for websites with high visitor rate, it can be suitable to store them to a Memcached.

In the file /etc/php/7.2/apache2/php.ini change

session.save_handler = files

to

session.save_handler = memcached

restart the Web serer apache

sudo systemctl restart apache2

Change will take effect even on the result of phpinfo (as above).

Updated on June 24, 2021

Was this article helpful?

Related Articles