1,memcached需要libevent,所以要先安裝它 下載地址:http://download.chinaunix.net/download.php?id=45065&ResourceID=5804 tar xf libevent-2.0.21-stable.tar.gz cd libeven ...
1,memcached需要libevent,所以要先安裝它
下載地址:http://download.chinaunix.net/download.php?id=45065&ResourceID=5804
tar xf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
make
sudo make install
2,安裝memcached
wget http://memcached.org/files/memcached-1.5.5.tar.gz
tar xf memcached-1.5.5.tar.gz
cd memcached-1.5.5/
./configure --with-libevent=/usr/local/libevent
make && sudo make install
3,安裝php的memcache擴展
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar xf memcache-2.2.7.tgz
cd memcache-2.2.7/
/usr/local/php54/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php54/bin/php-config --with-zlib-dir
【如果沒有zlib,先安裝zlib】
wget http://www.zlib.net/fossils/zlib-1.2.11.tar.gz
tar xf zlib-1.2.11.tar.gz
cd zlib-1.2.11/
./configure
make && sudo make install
接著再安裝memcache
make && sudo make install
4,在php.ini中添加擴展
extension=memcache.so
5,啟動memcached服務端
/usr/local/bin/memcached -d -m 10 -u root -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcached.pid
-d選項是啟動一個守護進程,
-m是分配給Memcache使用的記憶體數量,單位是MB,我這裡是10MB,
-u是運行Memcache的用戶,我這裡是root,
-l是監聽的伺服器IP地址,如果有多個地址的話,我這裡指定了伺服器的IP地址127.0.0.1
-p是設置Memcache監聽的埠,我這裡設置了12000,最好是1024以上的埠,
-c選項是最大運行的併發連接數,預設是1024,我這裡設置了256,按照你伺服器的負載量來設定,
-P是設置保存Memcache的pid文件,我這裡是保存在 /tmp/memcached.pid,
6,編寫測試文件
<?php $mem = new Memcache; $mem->connect( "127.0.0.1", 12000 ); $mem->set( "hi", "hello,ghostwu", 0, 120 ); echo $mem->get( "hi" ) . PHP_EOL; ?>