2011-01-19

Installing memcache on Windows for PHP

So I had a task to install memcache on my developer's PC.
My working environment is:
Windows 7Pro x64
XAMPP (win32 version 1.7.3), it had installed Apache 2.2.14 with PHP 5.3.1

I've got very useful article http://pureform.wordpress.com/2008/01/10/installing-memcache-on-windows-for-php/, which helped me a lot. (By the way, you can read there something about memcache itself). I've copied described steps there, but append them with my own.

So following that article I've started to install
  1. Download memcache from code.jellycan.com/memcached/ [grab the 'win32 binary' version]
  2.  Install memcache as a service
  • Unzip and copy the binaries to your desired directory (eg. c:\memcached) [you should see one file, memcached.exe]
  •  If you’re running Vista or Windows 7, right click on memcached.exe and click Properties. Click the Compatibility tab. Near the bottom you’ll see Privilege Level, check “Run this program as an administrator”. Of course, you have to be an adminon your computer
  • Install the service using the command: c:\memcached\memcached.exe -d install from the command line. If  you have a message "The program can't start because MSVCR71.dll is missing from your computer. Try reinstalling the program to fix this problem", make a local search for desired library. You may copy founded file to memcache's folder
  • Start the server from the Microsoft Management Console or by running one of the following commands: c:\memcached\memcached.exe -d start, or net start "memcached Server" 
Now memcache is installed and you have to tie it to PHP
  1. Check your php extensions directory [should be something like: C:\php\ext, for XAMPP location is <xampp folder>\php\ext] for php_memcache.dll
    If you don’t have any luck finding it, try looking at one of these sites:
    - downloads.php.net/pierre/
    - pecl4win.php.net/ext.php/php_memcache.dll [currently down]
    - www.pureformsolutions.com/pureform.wordpress.com/2008/06/17/php_memcache.dll for PHP 5.2.*
    - kromann.info/download.php?strFolder=php5_1-Release_TS&strIndex=PHP5_1 for PHP 5.1.* 
  2. Now find your php.ini file [default location for XP Pro is C:\WINDOWS\php.ini, for XAMPP location is <xampp folder>\php\php.ini] and add this line to the extensions list:
    extension=php_memcache.dll 
  3. Restart apache
  4. Run this code to test the installation: [found on www.php.net/memcache]
    
    <?php
        $memcache = new Memcache;
        $memcache->connect("localhost",11211); # You might need to set "localhost" to "127.0.0.1"
    
        echo "Server's version: " . $memcache->getVersion() . "<br />\n";
    
        $tmp_object = new stdClass;
        $tmp_object->str_attr = "test";
        $tmp_object->int_attr = 123;
    
        $memcache->set("key",$tmp_object,false,10);
        echo "Store data in the cache (data will expire in 10 seconds)<br />\n";
    
        echo "Data from the cache:<br />\n";
        var_dump($memcache->get("key"));?>
    If you see anything but errors, you are now using memcache!
  5. If you have errors, look into apache log files [for XAMPP installation it's <xampp folder>\apache\logs\error.log  ]. I had error there
    
    <b>Warning</b>:  PHP Startup: memcache: Unable to initialize module
    Module compiled with build ID=API20090626,TS,VC9
    PHP    compiled with build ID=API20090626,TS,VC6
    These options need to match
     in <b>Unknown</b> on line <b>0</b><br /> 
    
    To fix this, you need anorher version of c:\xampp\php\ext\php_memcache.dll. You can find it there http://alekciy.ru/files/php_memcache.zip

1 comment:

  1. Thank you for sharing such useful information! memcached is a very powerful web application.
    regards,
    SEO melbourne

    ReplyDelete