下载memcache服务器端memcached 和 php扩展 memcache
http://pecl.php.net/package/memcached 或者 http://memcached.org/downloads
http://pecl.php.net/package/memcache
memcached服务器端安装 依赖libevent 一般情况下 ,装系统的时候都会装进去
ls -al /usr/lib | grep libevent
1,若没有先安装libevent(http://libevent.org/)
tar zxvf libevent-2.0.21-stable.tar.gz
cd libevent-2.0.21-stable
./configure --prefix=/usr/src/libevent
make && make install
法二:
用系统自命的软件管理工具,archlinux用pacman,centos用yum ,Debian用apt-get等
2,安装memcached 服务器端
tar zxvf memcached-1.4.20.tar.gz
cd memcached-1.4.20
./configure --with-libevent=/usr/src/libevent --prefix=/usr/local/memcahced
make && make install
3,启动memcached服务
/usr/local/memcached/bin/memcached -d -m 20 -u root -p 12000 -P /tmp/memcached.pid
-d #作为守护进程运行
-m#分配20M的内存
-u #用户是root
-p #监听端口是12000
-P#进程PID存放的位置
netstat -tpln 查看memcached是否起来
4,安装php的memcache扩展
tar zxvf memcache-2.2.7.tar.gz
cd memcache-2.2.7
/usr/local/php/bin/phpize
./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
make && make install
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20121212/ 出现这句话表示安装成功了 此目录里会有一个memcache.so的文件
在php.ini 里 加上extension=memcache.so
重启服务器
phpinfo里查看
$memcache = new Memcache;
$memcache->connect("127.0.0.1",11211)or die ("Could not connect");
$memcache->set('key', 'memcache is work', 0, 60);
$val = $memcache->get('key');
print_r($val);