程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> linux下為PHP擴展安裝memcache模塊

linux下為PHP擴展安裝memcache模塊

編輯:關於PHP編程

安裝環境
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查看


測試模塊

<?php

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

$version = $memcache->getVersion();
echo "Servers version: ".$version."<br/> ";

$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)<br/> ";

$get_result = $memcache->get(key);
echo "Data from the cache:<br/> ";

var_dump($get_result);

?>

顯示結果:

Servers version: 1.2.6
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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved