Lamp下安装memcached
1、先安装 libevent,再安装 Memcached主程序
# tar xf libevent-2.0.21-stable.tar.gz
# cd libevent-2.0.21-stable
# ./configure --prefix=/usr/local/libevent
# make
# make install
2、Memcached 安装
# tar xf memcached-1.4.21.tar.gz
# cd memcached-1.4.21
# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
# make
# make install
3、服务器端安装结果测试:
# /usr/local/memcached/bin/memcached -d -m 512 -u root
# ss -tnulp | grep 11211
# pkill memcached
4、Memcached 和 PHP 结合使用
①php Memcached 扩展 安装 memcache-2.2.6.tgz
# tar xf memcache-2.2.7.tgz
# cd memcache-2.2.7
# /usr/local/php/bin/phpize
# ./configure --enable-memcache --prefix=/usr/local/memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
# make
# make install
# vim /usr/local/php/etc/php.ini #加入下面两行,最后一行要看实际情况
extension = memcache.so
extension = "/usr/local/php/lib/php/extensions/no-debug-zts-20100525/"
# /usr/local/apache2/bin/apachectl stop #重启apache服务
# /usr/local/apache2/bin/apachectl start
# /usr/local/memcached/bin/memcached -d -m 512 -u root #启动memcached
#memcached -d -m 10 -u root -l localhost -p 11211 -c 256 -P /tmp/memcached.pid
此命令仅作为讲解下面参数
各个参数的详解
1. -d选项是启动一个守护进程,
2. -m是分配给Memcached使用的内存数量,单位是MB,
3. -u是运行Memcache的用户,我这里是root,
4. -l是监听的服务器IP地址,
5. -p是设置Memcache监听的端口,最好是1024以上的端口,
6. -c选项是最大运行的并发连接数,默认是1024,按照你服务器的负载量来设定,
7. -P是设置保存Memcache的pid文件
②测试memcached是否能与php配合使用
# cd /usr/local/apache2/htdocs/
# vim mem.php
<?php
$mem=new Memcache;
$mem->connect("127.0.0.1",11211);
$mem->set('hello','world',0,60);
$val=$mem->get('hello');
echo $val;
?>
# curl http://192.168.131.134/mem.php
出现"world"则说明memcache php扩展成功了