欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

安装php的memcache扩展

程序员文章站 2024-01-14 18:50:16
...

linux: 安装 环境 RHEL 4 Php 5.2.6 所需软件 libevent-1.4.6-stable.tar.gz (http://monkey.org/~provos/libevent/) memcache-2.2.3.tgz (http://pecl.php.net/package/memcache) memcached-1.2.6.tar.gz (http://www.danga.com/memcached/) 安装 配置 1.

linux:

安装环境
RHEL 4
Php 5.2.6


所需软件
libevent-1.4.6-stable.tar.gz (http://monkey.org/~provos/libevent/)
memcache-2.2.3.tgz (http://pecl.php.net/package/memcache)
memcached-1.2.6.tar.gz (http://www.danga.com/memcached/)


安装配置

1. 安装libevent
# tar zxf libevent-1.4.6-stable.tar.gz
# cd libevent-1.4.6-stable
# ./configure --prefix=/usr/local/servers/libevent
# make && make install

2. 安装memcached
# tar zxvf memcached-1.2.6.tar.gz
# cd memcached-1.2.6
# ./configure --prefix=/usr/local/servers/memcached --with-libevent=/usr/local/servers/libevent
# make && make install

3. 运行memcached
# /usr/local/servers/memcached -d -m 128 -l localhost -p 11211 -u root

-d 以守护程序(daemon)方式运行 memcached;
-m 设置 memcached 可以使用的内存大小,单位为 M;
-l 设置监听的 IP 地址,如果是本机的话,通常可以不设置此参数;
-p 设置监听的端口,默认为 11211,所以也可以不设置此参数;
-u 指定用户;

如果遇到运行memcached时遇到问题,错误提示如下:

/usr/local/servers/memcached/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

则运行 LD_DUBUG=libs 就可以知道memcached启动时库的加载路径。具体如下:

# LD_DEBUG=libs /usr/local/servers/memcached/bin/memcached -v
10929: find library=libevent-1.4.so.2 [0]; searching
10929: search cache=/etc/ld.so.cache
10929: search path=/lib/tls/i686/sse2:/lib/tls/i686:/lib/tls/sse2:/lib/tls:/lib/i686/sse2:/lib/i686:/lib/sse2:/lib:/usr/lib/tls/i686/sse2:/usr/lib/tls/i686:/usr/lib/tls/sse2:/usr/lib/tls:/usr/lib/i686/sse2:/usr/lib/i686:/usr/lib/sse2:/usr/lib (system search path)
10929: trying file=/lib/tls/i686/sse2/libevent-1.4.so.2
10929: trying file=/lib/tls/i686/libevent-1.4.so.2
10929: trying file=/lib/tls/sse2/libevent-1.4.so.2
10929: trying file=/lib/tls/libevent-1.4.so.2
10929: trying file=/lib/i686/sse2/libevent-1.4.so.2
10929: trying file=/lib/i686/libevent-1.4.so.2
10929: trying file=/lib/sse2/libevent-1.4.so.2
10929: trying file=/lib/libevent-1.4.so.2
10929: trying file=/usr/lib/tls/i686/sse2/libevent-1.4.so.2
10929: trying file=/usr/lib/tls/i686/libevent-1.4.so.2
10929: trying file=/usr/lib/tls/sse2/libevent-1.4.so.2
10929: trying file=/usr/lib/tls/libevent-1.4.so.2
10929: trying file=/usr/lib/i686/sse2/libevent-1.4.so.2
10929: trying file=/usr/lib/i686/libevent-1.4.so.2
10929: trying file=/usr/lib/sse2/libevent-1.4.so.2
10929: trying file=/usr/lib/libevent-1.4.so.2
10929:

然后给libevent-1.4.so.2建立一个链接后再运行memcached:
# ln -s /usr/local/servers/libevent/lib/libevent-1.4.so.2 /lib/libevent-1.4.so.2

4. 安装php的memcache扩展

可以使用php自带的pecl安装程序
# /usr/local/servers/php5/bin/pecl install memcache

也可以从源码安装
# tar zxf memcache-2.2.3.tgz
# cd memcache-2.2.3
# /usr/local/servers/php5/bin/phpize
# ./configure --enable-memcache=/usr/local/servers/memcached --with-php-config=/usr/local/servers/php5/bin/php-config --with-apxs2=/usr/sbin/apxs
# make && make inst

安装完后会有类似这样的提示:
Installing shared extensions: /usr/local/servers/php5/lib/php/extensions/no-debug-non-zts-20060922/

把这个记住,然后修改php.ini,把

extension_dir = "./"
修改为
extension_dir = "/usr/local/servers/php5/lib/php/extensions/"

并添加一行
extension="no-debug-non-zts-20060922/memcache.so"

5. 用phpinfo查看

windows:

下载memcache的windows稳定版,解压放某个盘下面,比如在d:\memcached

2. 在终端(也即cmd命令界面)下输入 d:\memcached\memcached.exe -d install 安装

3. 再输入: c:\memcached\memcached.exe -d start 启动。

NOTE: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。

4.下载php_memcache.dll,请自己查找对应的php版本的文件

5. 在php.ini 加入一行 extension=php_memcache.dll

6.重新启动Apache,然后查看一下phpinfo,如果有memcache,那么就说明安装成功!


测试模块

$memcache = new Memcache;
$memcache->connect('localhost', 12000) or die ("Could not connect");

$version = $memcache->getVersion();
echo "Server's version: ".$version."
\n";

$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;

$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)
\n";

$get_result = $memcache->get('key');
echo "Data from the cache:
\n";

var_dump($get_result);

?>

显示结果:

Server's version: 1.4.4-14-g9c660c0
Store data in the cache (data will expire in 10 seconds)
Data from the cache:

object(stdClass)[3] public 'str_attr' => string 'test' (length=4) public 'int_attr' => int 123

----------------

server端的就是memcached,client端可以看作是php_memcache.dll

php_memcache.dll提供了一组函数,用来缓存,提取,删除缓存等一系列的操作,而通过这些函数,最终把数据是放到了安装有memcached服务的机器上,可以是本机,也可以是其它网络上的机器。

memadmin是一个查看memcached服务器的一个小工具,很强大。(用php+jquery写的)

2cto 附件下载地址:http://up.2cto.com/2011/1124/20111124050347851.rar

百度网盘下载:http://pan.baidu.com/s/14HGPS