標籤:
I.安裝Memcahce
1. 安裝依賴包libevent
Memcache需要安裝libevent,所以安裝前可能需要執行
Shell代碼
- yum install libevent-devel
2.安裝memcache
從http://memcached.org/下載最源碼新版1.4.13
Shell代碼
- 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如下行:
Shell代碼
- 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所在路徑)
然後執行如下命令即可:
Shell代碼
- chmod 755 memcached
- chkconfig --add memcached
啟動試試看
Shell代碼
- service memcached start
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
Shell代碼
- 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下載,安裝指令:
Shell代碼
- 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代碼
- <?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
轉自部落格->http://koda.iteye.com/blog/665761
CenOS下安裝Memcache和PHP Memcache擴充.