In my previous blog , I have explained How to Setup Memcached cluster in AWS

In this guide , We will see how to install and configure memcached on Ubuntu.

memcached

What is Memcached?

Memcached is an open sources distributed memory caching system.

It is used to speed up and improve the performance of the applications by storing the information temporarily in memory.

By doing so , It will also helps to reduce the connection to the database.

Installing Memcached

Lets go ahead and install Memcached on Ubuntu operating systems.

Make sure you have sudo or root privileges to the system.

Update the ubuntu’s default APT package repository using the command,

sudo apt-get update

Using the below command , We can install memcached on the system.

sudo apt-get install memcached libmemcached-tools -y

Optionally , We can install Memcache module for PHP functionality.

sudo apt-get install php-memcached -y

After the packages are installed , We can check the version of memcached installed on the system,

memcached --version
memcached

To verify whether Memcache PHP module is installed on the system, Execute the command.

php -m |grep memcache

The above command should reply as memcached

While installing memcached packages , The systemd unit file memcached.service will be created by itself , using which we can easily manage the memcached service.

cat /lib/systemd/system/memcached.service
memcached

To start the memcached service,

systemctl start memcached

To check the status of the service,

systemctl status memcached
memcached

To enable memcached service to automatically start on system boot up,

systemctl enable memcached

By default , Memcached runs on the Port 11211 and listens only to Localhost.

You can check the same using the below command.

netstat -nltp |grep memcache
memcached

Configuring Memcached

At some point , If you want to change the settings of the Memcached service , We can do it by modifying /etc/memcached.conf file.

We can change the Port , Allow remote connections , Memory for Caching.

sudo vi /etc/memcached.conf

Keeping the same flags for memory (-m) , Port (-p) and listen address (-l) , We can modify their values.

memcached

After changing the configurations for memcached , Restart the memcached service for the changes to take effect.

systemctl restart memcached

Using Memcached with PHP & Apache

Let’s test this, By installing PHP and Apache web server to use the Memcached service.

To install Apache and PHP packages , Use the below command.

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

After the packages are installed , We can verify the Apache and PHP installations using the below command.

php -v
memcached
systemctl status apache2
memcached

Next step is to create a sample php file – phpinfo.php under /var/www/html folder.

vi /var/www/html/phpinfo.php

Add these content to the file,

memcached

Save and close the file And then restart the Apache service.

systemctl restart apache2

Lets verify the installation , Go to your browser and type the URL as shown below.

http://IPaddress/phpinfo.php

You will see the information related to PHP and Memcached modules on te following page.

memcached
memcached

We have successfully installed and Configured memcached on the Ubuntu operating system.

Also We have tested the same by installing PHP module and Apache Web server to use Memcached.

If you found it helpful.Please check out other blogs.