Boost your PHP7 Server with MemCached

Boost your PHP7 Server with MemCached


In this tutorial, i assume that you have a working LAMP installation already on your server and everything is upto date. (If you are unsure, then google LAMP or read my tutorial on how to install it). Once you have a working webserver on your OS, you can move forward with this tutorial.


To begin installation enter the following command into the terminal

apt-get install memcached -y

Note -y parameter automatically accepts that you are willing to install the package no-matter of the size and its dependencies that will also get installed. Once installed you will see something like below in the terminal.

Installed memcached with all dependencies succesfully.

Now that you have memcached installed , now is the time to configure it.


Run the following command to check if the memcached server is up and running on your machine.

ps -eaf | grep memcached

The output of the above command shall be

nobody    2191     1  0 11:29 ?        00:00:00 /usr/bin/memcached -m 128 -p 11211 -u nobody -l
root      3136  3190  0 11:29 pts/0    00:00:00 grep memcached

The above output illustrates that memcached is running on port 11211 and using 128mb of memory in total (in RAM), please note that the default configuration for memcached is located at /etc/memcached.conf file. Open this file and make the appropriate changes as required. Type in terminal  nano /etc/memcached.conf to open the configuration file.

Change the following line

-m 128


-m 512

This configuration assumes you have 4GB of RAM, for more information refer to the memcached’s wiki. 1/4th of RAM contributing towards the memcached won’t harm your servers overall performance, so in case you have 1GB of RAM, 128 wont hurt thus. The overall configuration page is quite self explanatory, forexample if you wish to change the port (which is rare in case) then all you need to change is -p 11211 to any valid port number above 10000 and less than 65535 .  Press CTRL+W or CMD+W to save the changes to the file and restart memcached server.


service memcached restart



To check what settings have been working on the memcached server, all you need to enter is the following command.

echo "stats settings" | nc localhost 11211

Note : Replace 11211 with the specific port number that you might have changed earlier  in the configuration file. But if you havent changed the -p 11211 parameter, than continue with this command and you will have the following output (trimmed here).

STAT maxbytes 536870912
STAT maxconns 2048
STAT tcpport 11211
STAT udpport 11211
STAT inter
STAT evictions on
STAT domain_socket NULL
STAT umask 700
STAT auth_enabled_sasl no
STAT item_size_max 1048576

Above output is trimmed by [...] which in-case will be plain text.

install & configure memcached for php

Now we install the memcached plugin for PHP and make appropriate configurations to have it working under our webserver, for this run the following pecl command.

pecl install memcache

Open up the php.ini and add to it and finally run the following command

service apache2 restart

This will restart your apache webserver and now you can run phpinfo() to check for the loaded extensions and you will see memcache listed in the extensions Page.


If you followed all the steps than all you need to do is to run

php -i | grep memcache

and the terminal will output with memcached related information, if there’s a blank line or nothing on the terminal, then comment below and i will let you know more about it on how to resolve it.

Good luck!

As rated by readers
[Total: 0 Average: 0]

Leave a Reply

Notify of