By Tim Kotkamp
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/
Updating packages to the latest available version isn't required but recommended.
Lets start with installing Memcached.
yum install memcached
After installing Memcached, open the configuration file of Memcached with VI.
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.
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.
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 = files session.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:
and uncomment the ServerName line:
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!
Click below to sign up and get $200 of credit to try our products over 60 days!
Enter your email to get $200 in credit for your first 60 days with DigitalOcean.
yum install php-devel (for me that was the only thing missing) ;)
and: pecl install memcache (optional, but good to have, for wordpress users, as they will want to use w3 total cache and for some strange reason once pecl memcache is installed, w3 total cache starts showing it in options, personal experience)
Hello, I have no results to fix working memcached. The follow error come up:
memcached: invalid option – ‘1’ Illegal argument “?” (When I have set the settings to “-1 127.0.0.1”)
Nice tutorial, thnx. But I have some problems to install memcached on a VPS cloud server CentOS 6.2 x64 Using memcached on PrestaShop version 220.127.116.11 The follow fault:
[root@vps ~]# pecl install -f memcached downloading memcached-2.2.0.tgz … Starting to download memcached-2.2.0.tgz (70,449 bytes) …done: 70,449 bytes 15 source files, building running: phpize Configuring for: PHP Api Version: 20100412 Zend Module Api No: 20100525 Zend Extension Api No: 220100525
configure: error: no, sasl.h is not available. Run configure with --disable-memcached-sasl to disable this check ERROR: `/tmp/pear/temp/memcached/configure --with-libmemcached-dir=no’ failed [root@vps ~]# service httpd restart
Witch directory do I have to give it? And in the Section: OPTIONS=“-1 127.0.0.1”, I have delete the -1 and put only 127.0.0.1, are this right or do I replace your suggestion back?
Best Regards, Roger
Can we store session in Memcache in 1 central server and use the backend servers to access those sessions remotely ? It will help if we want to have a HA environment setup.
pecl channel-update pecl.php.net
Hello, I love your blog. I’m studying memcached so I can implement on my websites. I have a basic question, when changed to memcached is there any modification I need to do on my php when I call a session or it should work as it is (at the basic level)? I’m just worried about bugging the entire thing once I install memcached. I know I’ll have to change how the session is initialized to share between my servers, I’m just wondering on the basic level as it is. Thanks for your help!
I got error 500 server error for my websites after installation on my vps . ı got php time out in my logs when i set sessions to be handled by memcached as explained here . Ive got centos6.5 php5.3.3 interworx panel
Always get fatal error class memcached not found ls /usr/lib/php/modules curl.so intl.so mysql.so posix.so sysvshm.so xsl.so dom.so json.so pdo_mysql.so soap.so wddx.so zip.so fileinfo.so mcrypt.so pdo.so sqlite3.so xmlreader.so gd.so memcached.so pdo_sqlite.so sysvmsg.so xmlrpc.so imap.so mysqli.so phar.so sysvsem.so xmlwriter.so memcached.so installed here, but it was white, instead running green.
ps aux | grep memcache 498 11476 0.0 0.0 56272 824 ? Ssl 01:59 0:00 memcached -d -p 11211 -u memcached -m 64 -c 1024 -P /var/run/memcached/memcached.pid -l 127.0.0.1 root 13887 0.0 0.0 4352 736 pts/1 S+ 02:17 0:00 grep memcache
i have add into /etc/php.ini extension=memcached.so either full path
php -v PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib/php/modules/memcached.so’ - /usr/lib/php/modules/memcached.so: undefined symbol: php_json_encode in Unknown on line 0
Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib/php/modules/memcached.so’ - /usr/lib/php/modules/memcached.so: undefined symbol: php_json_encode in Unknown on line 0
if i disabled extension into php.ini either into memcached.ini, still no progress succeded.
All prequisites installation being correctly too, searching all over the web, have no answer for this class not found. yum -y groupinstall “Development Tools” yum -y install zlib-devel libmemcached-devel memcached-devel php-devel php-pear php-pecl-memcached
Dont know what im missing. Can anyone share me how to solve this issue :(.
Sorry i attached this long installation, hopefully useful
pecl install -f memcached-1.0.0 WARNING: channel “pecl.php.net” has updated its protocols, use “pecl channel-upd ate 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 Configuring for: PHP Api Version: 20090626 Zend Module Api No: 20090626 Zend Extension Api No: 220090626 building in /var/tmp/pear-build-rootsw1f2D/memcached-1.0.0 running: /var/tmp/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… i686-pc-linux-gnu checking host system type… i686-pc-linux-gnu checking target system type… i686-pc-linux-gnu checking for PHP prefix… /usr checking for PHP includes… -I/usr/include/php -I/usr/include/php/main -I/usr/i nclude/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/ph p/ext/date/lib checking for PHP extension directory… /usr/lib/php/modules checking for PHP installed headers prefix… /usr/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/include/php checking for memcached session support… enabled checking for memcached igbinary support… disabled checking for libmemcached location… /usr checking for a sed that does not truncate output… (cached) /bin/sed checking for fgrep… /bin/grep -F checking for ld used by cc… /usr/bin/ld checking if the linker (/usr/bin/ld) is GNU ld… yes checking for BSD- or MS-compatible name lister (nm)… /usr/bin/nm -B checking the name lister (/usr/bin/nm -B) interface… BSD nm checking whether ln -s works… yes checking the maximum length of command line arguments… 1966080 checking whether the shell understands some XSI constructs… yes checking whether the shell understands “+=”… yes checking for /usr/bin/ld option to reload object files… -r checking for objdump… objdump checking how to recognize dependent libraries… pass_all checking for ar… ar checking for strip… strip checking for ranlib… ranlib checking command to parse /usr/bin/nm -B output from cc object… ok 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 for dlfcn.h… yes checking for objdir… .libs checking if cc supports -fno-rtti -fno-exceptions… no checking for cc option to produce PIC… -fPIC -DPIC checking if cc PIC flag -fPIC -DPIC works… yes checking if cc static flag -static works… no checking if cc supports -c -o file.o… yes checking if cc supports -c -o file.o… (cached) yes checking whether the cc linker (/usr/bin/ld) 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 configure: creating ./config.status config.status: creating config.h config.status: executing libtool commands running: make /bin/sh /var/tmp/pear-build-rootsw1f2D/memcached-1.0.0/libtool --mode=compile cc -I/usr/include/php -I. -I/var/tmp/memcached -DPHP_ATOM_INC -I/var/tmp/pear-bui ld-rootsw1f2D/memcached-1.0.0/include -I/var/tmp/pear-build-rootsw1f2D/memcached -1.0.0/main -I/var/tmp/memcached -I/usr/include/php -I/usr/include/php/main -I/u sr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/includ e/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /var/tmp/memcached/php_memcache d.c -o php_memcached.lo libtool: compile: cc -I/usr/include/php -I. -I/var/tmp/memcached -DPHP_ATOM_INC -I/var/tmp/pear-build-rootsw1f2D/memcached-1.0.0/include -I/var/tmp/pear-build- rootsw1f2D/memcached-1.0.0/main -I/var/tmp/memcached -I/usr/include/php -I/usr/i nclude/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/p hp/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -c /var/tmp/memcac hed/php_memcached.c -fPIC -DPIC -o .libs/php_memcached.o /bin/sh /var/tmp/pear-build-rootsw1f2D/memcached-1.0.0/libtool --mode=link cc -D PHP_ATOM_INC -I/var/tmp/pear-build-rootsw1f2D/memcached-1.0.0/include -I/var/tmp /pear-build-rootsw1f2D/memcached-1.0.0/main -I/var/tmp/memcached -I/usr/include/ php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/u sr/include/php/ext -I/usr/include/php/ext/date/lib -DHAVE_CONFIG_H -g -O2 -o memcached.la -export-dynamic -avoid-version -prefer-pic -module -rpath /var/tmp /pear-build-rootsw1f2D/memcached-1.0.0/modules php_memcached.lo -lmemcached libtool: link: cc -shared .libs/php_memcached.o -lmemcached -Wl,-soname -W l,memcached.so -o .libs/memcached.so libtool: link: ( cd “.libs” && rm -f “memcached.la” && ln -s “…/memcached.la” " memcached.la" ) /bin/sh /var/tmp/pear-build-rootsw1f2D/memcached-1.0.0/libtool --mode=install cp ./memcached.la /var/tmp/pear-build-rootsw1f2D/memcached-1.0.0/modules libtool: install: cp ./.libs/memcached.so /var/tmp/pear-build-rootsw1f2D/memcach ed-1.0.0/modules/memcached.so libtool: install: cp ./.libs/memcached.lai /var/tmp/pear-build-rootsw1f2D/memcac hed-1.0.0/modules/memcached.la libtool: finish: PATH=“/usr/bin:/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/r oot/bin:/sbin” ldconfig -n /var/tmp/pear-build-rootsw1f2D/memcached-1.0.0/module s
Libraries have been installed in: /var/tmp/pear-build-rootsw1f2D/memcached-1.0.0/modules If you ever happen to want to link against installed libraries in a given directory, LIBDIR, you must either use libtool, and specify the full pathname of the library, or use the `-LLIBDIR’ flag during linking and do at least one of the following:
See any operating system documentation about shared libraries for more information, such as the ld(1) and ld.so(8) manual pages.
Build complete. Don’t forget to run ‘make test’.
running: make INSTALL_ROOT=“/var/tmp/pear-build-rootsw1f2D/install-memcached-1.0 .0” install Installing shared extensions: /var/tmp/pear-build-rootsw1f2D/install-memcach ed-1.0.0/usr/lib/php/modules/ running: find “/var/tmp/pear-build-rootsw1f2D/install-memcached-1.0.0” | xargs l s -dils 919062 4 drwxr-xr-x 3 root root 4096 Nov 30 02:07 /var/tmp/pear-build-rootsw 1f2D/install-memcached-1.0.0 919083 4 drwxr-xr-x 3 root root 4096 Nov 30 02:07 /var/tmp/pear-build-rootsw 1f2D/install-memcached-1.0.0/usr 919084 4 drwxr-xr-x 3 root root 4096 Nov 30 02:07 /var/tmp/pear-build-rootsw 1f2D/install-memcached-1.0.0/usr/lib 919085 4 drwxr-xr-x 3 root root 4096 Nov 30 02:07 /var/tmp/pear-build-rootsw 1f2D/install-memcached-1.0.0/usr/lib/php 919086 4 drwxr-xr-x 2 root root 4096 Nov 30 02:07 /var/tmp/pear-build-rootsw 1f2D/install-memcached-1.0.0/usr/lib/php/modules 919082 128 -rwxr-xr-x 1 root root 128994 Nov 30 02:07 /var/tmp/pear-build-rootsw 1f2D/install-memcached-1.0.0/usr/lib/php/modules/memcached.so
Build process completed successfully Installing ‘/usr/lib/php/modules/memcached.so’ install ok: channel://pecl.php.net/memcached-1.0.0 configuration option “php_ini” is not set to php.ini location You should add “extension=memcached.so” to php.ini
got this error after finishing memcached install
This comment has been deleted