Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.
Memcached will store sessions in memory instead of files. Because memory is way faster than reading a file your website will perform better and reduce load time.
Sessions will only be stored in memory, memory can't hold data when your VPS is turned off or gets restarted so sessions will be deleted upon shutdown.
Before starting this tutorial, make sure you have an up and running PHP 5 installation, you can find tutorials on how to do this in the PHP help section.
Make sure you have the EPEL repository installed, you need the EPEL repository for Memcached because Memcached isn't available in the base repository.
rpm -Uvh http://mirrors.kernel.org/fedora-epel/6/x86_64/epel- release-6-8.noarch.rpm
Updating packages to the latest available version isn't required but recommended.
yum update
Lets start with installing Memcached.
yum install memcached
After installing Memcached, open the configuration file of Memcached with VI.
vi /etc/sysconfig/memcached
You will see this:
PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS=""
Memcached isn't protected with a password or username so anyone can access it through port 11211. We don't want this so we are going to allow only your VPS to access it by inserting some options in the 'OPTIONS=' section:
PORT="11211" USER="memcached" MAXCONN="1024" CACHESIZE="64" OPTIONS="-l 127.0.0.1"
You might want to change the cachesize; by default it is 64MB. As soon as Memcached reaches this limit, it will delete old entries to free up memory for new entries. Unless you have a very huge website, 64MB should be fine.
Lets start Memcached.
/etc/init.d/memcached start
Memcached doesn't start by default upon boot, we want it to start upon boot.
chkconfig --levels 235 memcached on
We need to install a few things, lets start with the development tools. These are required to build from the source code:
yum groupinstall "Development Tools"
After that, we are going to install a few more things. The first two are required to build the extension, and the last two are required to run / install the extension in PHP.
yum install zlib-devel libmemcached-devel php-pear php-pecl-memcached
Now we are going to install the PHP Memcached extension using PECL (PHP Extension Community Library) which we have just installed.
pecl install -f memcached-1.0.0
The last thing to do is to configure PHP to use Memcached as session handler. To do so, you have to open /etc/php.ini with VI.
vi /etc/php.ini
Search for the '[Session]' area as displayed below (scrolling from bottom to top is easier).
[Session] ; Handler used to store/retrieve data. ; http://www.php.net/manual/en/session.configuration.php#ini. session.save_handler = filessession.save-handler
And change it to this:
[Session] ; Handler used to store/retrieve data. ; http://www.php.net/manual/en/session.configuration.php#ini. session.save_handler = memcached session.save_path = "127.0.0.1:11211"session.save-handler
As you can see, we've changed the session_handler to memcached and the path to our localhost on port 11211 on which Memcached operates. Now lets restart Apache to reload the PHP.ini file.
service httpd restart
All sessions are now stored in Memcached instead of files.
You may see the following error:
Starting httpd: httpd: apr_sockaddr_info_get() failed for memcached httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
You can resolve this by editing the apache configuration:
vi /etc/httpd/conf/httpd.conf
and uncomment the ServerName line:
ServerName localhost
Memcached is ideal for storing intensive queries which doesn't need to be in realtime on every page view but in specified time increments (ie. every 10 minutes). For more information on how to use Memcached inside your scripts, I recommend you to look at step three in this tutorial.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
hi there, this is the error that I get: [root@server User]# pecl install -f memcached-1.0.0 WARNING: channel “pecl.php.net” has updated its protocols, use “pecl channel-update pecl.php.net” to update downloading memcached-1.0.0.tgz … Starting to download memcached-1.0.0.tgz (22,281 bytes) …done: 22,281 bytes 4 source files, building running: phpize sh: phpize: command not found ERROR: `phpize’ failed [root@server User]# please let me know what other details I should paste to you so you could identify my problem
Try running <pre>sudo yum install php-devel</pre>.
Hi , When i am trying to run command - pecl install -f memcached-1.0.0
This is the error message that i am getting .
can some one tell me what i am doing wrong?
running: phpize Configuring for: PHP Api Version: 20100412 Zend Module Api No: 20100525 Zend Extension Api No: 220100525 building in /root/tmp/pear/pear-build-rootZ5HHVA/memcached-1.0.0 running: /root/tmp/pear/memcached/configure checking for grep that handles long lines and -e… /bin/grep checking for egrep… /bin/grep -E checking for a sed that does not truncate output… /bin/sed checking for cc… cc checking for C compiler default output file name… a.out checking whether the C compiler works… yes checking whether we are cross compiling… no checking for suffix of executables… checking for suffix of object files… o checking whether we are using the GNU C compiler… yes checking whether cc accepts -g… yes checking for cc option to accept ISO C89… none needed checking how to run the C preprocessor… cc -E checking for icc… no checking for suncc… no checking whether cc understands -c and -o together… yes checking for system library directory… lib checking if compiler supports -R… no checking if compiler supports -Wl,-rpath,… yes checking build system type… x86_64-unknown-linux-gnu checking host system type… x86_64-unknown-linux-gnu checking target system type… x86_64-unknown-linux-gnu checking for PHP prefix… /usr/local checking for PHP includes… -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib checking for PHP extension directory… /usr/local/lib/php/extensions/no-debug-non-zts-20100525 checking for PHP installed headers prefix… /usr/local/include/php checking if debug is enabled… no checking if zts is enabled… no checking for re2c… no configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers. checking for gawk… gawk checking whether to enable memcached support… yes, shared checking for libmemcached… yes, shared checking whether to enable memcached session handler support… yes checking whether to enable memcached igbinary serializer support… no checking for ZLIB… yes, shared checking for zlib location… /usr checking for session includes… /usr/local/include/php checking for memcached session support… enabled checking for memcached igbinary support… disabled checking for libmemcached location… /usr checking for ld used by cc… /usr/bin/ld checking if the linker (/usr/bin/ld) is GNU ld… yes checking for /usr/bin/ld option to reload object files… -r checking for BSD-compatible nm… /usr/bin/nm -B checking whether ln -s works… yes checking how to recognize dependent libraries… pass_all checking for ANSI C header files… yes checking for sys/types.h… yes checking for sys/stat.h… yes checking for stdlib.h… yes checking for string.h… yes checking for memory.h… yes checking for strings.h… yes checking for inttypes.h… yes checking for stdint.h… yes checking for unistd.h… yes checking dlfcn.h usability… yes checking dlfcn.h presence… yes checking for dlfcn.h… yes checking the maximum length of command line arguments… 1572864 checking command to parse /usr/bin/nm -B output from cc object… ok checking for objdir… .libs checking for ar… ar checking for ranlib… ranlib checking for strip… strip checking if cc supports -fno-rtti -fno-exceptions… no checking for cc option to produce PIC… -fPIC checking if cc PIC flag -fPIC works… yes checking if cc static flag -static works… yes checking if cc supports -c -o file.o… yes checking whether the cc linker (/usr/bin/ld -m elf_x86_64) supports shared libraries… yes checking whether -lc should be explicitly linked in… no checking dynamic linker characteristics… GNU/Linux ld.so checking how to hardcode library paths into programs… immediate checking whether stripping libraries is possible… yes checking if libtool supports shared libraries… yes checking whether to build shared libraries… yes checking whether to build static libraries… no
creating libtool appending configuration tag “CXX” to libtool configure: creating ./config.status config.status: creating config.h running: make /bin/sh /root/tmp/pear/pear-build-rootZ5HHVA/memcached-1.0.0/libtool --mode=compile cc -I/usr/local/include/php -I. -I/root/tmp/pear/memcached -DPHP_ATOM_INC -I/root/tmp/pear/pear-build-rootZ5HHVA/memcached-1.0.0/include -I/root/tmp/pear/pear-build-rootZ5HHVA/memcached-1.0.0/main -I/root/tmp/pear/memcached -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /root/tmp/pear/memcached/php_memcached.c -o php_memcached.lo mkdir .libs cc -I/usr/local/include/php -I. -I/root/tmp/pear/memcached -DPHP_ATOM_INC -I/root/tmp/pear/pear-build-rootZ5HHVA/memcached-1.0.0/include -I/root/tmp/pear/pear-build-rootZ5HHVA/memcached-1.0.0/main -I/root/tmp/pear/memcached -I/usr/local/include/php -I/usr/local/include/php/main -I/usr/local/include/php/TSRM -I/usr/local/include/php/Zend -I/usr/local/include/php/ext -I/usr/local/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /root/tmp/pear/memcached/php_memcached.c -fPIC -DPIC -o .libs/php_memcached.o /root/tmp/pear/memcached/php_memcached.c: In function ‘php_memc_new’: /root/tmp/pear/memcached/php_memcached.c:1901: error: ‘zend_class_entry’ has no member named ‘default_properties’ make: *** [php_memcached.lo] Error 1 ERROR: `make’ failed
@ritesh I’m having the exact same issue, can someone please shed some light on this?
move to debian/ubuntu!
Thanks!
Hello. Just try pecl install -f memcached without submitting version “-1.0.0” It will find automatically latest version of it.
Good luck.
i was trying to follow this tutorial but maybe made some mistake
now getting this error
login as: root root@128.199.129.23’s password: Last login: Tue Nov 4 04:54:31 2014 from 103.19.253.50 [root@mrcmanager ~]# vi /etc/sysconfig/memcached
E325: ATTENTION Found a swap file by the name “/etc/sysconfig/.memcached.swp” owned by: root dated: Tue Nov 4 04:41:04 2014 file name: /etc/sysconfig/memcached modified: YES user name: root host name: mrcmanager process ID: 20394 While opening file “/etc/sysconfig/memcached” dated: Wed May 26 06:09:47 2010
(1) Another program may be editing the same file. If this is the case, be careful not to end up with two different instances of the same file when making changes. Quit, or continue with caution.
(2) An edit session for this file crashed. If this is the case, use “:recover” or “vim -r /etc/sysconfig/memcached” to recover the changes (see “:help recovery”). If you did this already, delete the swap file "/etc/sysconfig/.memcached.swp " to avoid this message. “/etc/sysconfig/memcached” 5L, 71C Press ENTER or type command to continue
how to fix it?
This comment has been deleted
got this error after finishing memcached install