I.安裝Memcahce
1. 安裝依賴包libevent
Memcache須要安裝libevent,因此安裝前可能須要執行
- yum install libevent-devel
2.安裝memcache
從http://memcached.org/下載最源碼新版1.4.13
- tar -xzvf memcached-1.4.13.tar.gz
- cd memcached-1.4.13
- ./configure --prefix=/usr/local/memcache
- make && make install
3.配置memcached自啓動
複製源碼目錄下scripts/memcached.sysv到/etc/init.d/memcached,須要修改/etc/init.d/memcached以下行:
- chown $USER /usr/local/memcache/bin/memcached
- daemon /usr/local/memcache/bin/memcached -d -p $PORT -u $USER -m $CACHESIZE -c $MAXCONN -P /var/run/memcached/memcached.pid $OPTIONS
(就是修改memcached所在路徑)
而後執行以下命令便可:
- chmod 755 memcached
- chkconfig --add memcached
啓動試試看
II.安裝PHP Memcahce擴展
引用
pecl.php.net有兩個memcache擴展:
memcache memcached extension
memcached PHP extension for interfacing with memcached via libmemcached library
memcached 的版本比較新,並且使用的是 libmemcached 庫。libmemcached 被認爲作過更好的優化,應該比 php only 版本的 memcache 有着更高的性能。因此這裏安裝的是memcached(假設php5.4已經安裝在在/usr/local/php).
1. 安裝依賴庫
https://launchpad.net/libmemcached/1.0/1.0.4/+download/libmemcached-1.0.4.tar.gz
- tar -xzvf libmemcached-1.0.4.tar.gz
- cd libmemcached-1.0.4
- ./configure
- make
- make install
2.安裝memcached
http://pecl.php.net/get/memcached-2.0.1.tgz下載,安裝指令:
- tar vxzf memcached-2.0.1.tgz
- cd memcache-2.0.1
- phpize
- ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config
- make
- make install
裝完後,最後顯示Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/,在該目錄下生成 memcached.so,將其複製到php.ini的extension_dir指向的目錄。修改php.ini增長行extension = memcached.so重啓apache,再到頁面上打開phpinfo信息頁面,看看memcached擴展是否安裝成功了。
寫個測試看看
- <?php
- $m = new Memcached();
- $m->addServer('localhost', 11211);
- $m->set('username', 'Allen');
- var_dump($m->get('username'));
- ?>
也有一個php寫的memcache監控工具能夠下載
http://livebookmark.net/journal/2008/08/28/memcachephp-flushes-servers/
這篇文章是如何設置php session到memcache
http://koda.iteye.com/blog/466667