php扩展memcache的作用是为了支持memcached数据库缓存服务器,下面是安装方法。
1、下载并解压memcache文件
1 | wget http://pecl.php.net/get/memcache-2.1.2.tgz |
备选
1 2 3 | wget -c http://pecl.php.net/get/memcache-3.0.6.tgz tar xzvf memcache-3.0.6.tgz cd memcache-3.0.6 |
2、执行phpize扩展安装程序,假设phpzie的路径为/usr/local/php/bin/phpize,具体的路径得根据自己的环境修改。
1 | /var/www/php5/bin/phpize |
3、开始安装扩展memcache
[plain] view plaincopy
1 2 | ./configure --enable-memcache --with-php-config=/var/www/php5/bin/php-config --with-zlib-dir make && make install |
安装完成后,提示
Installing shared extensions: /var/www/php5/lib/php/extensions/no-debug-non-zts-20060613/
4、最后修改php.ini文件,在zend之前加入如下代码。
1 2 3 | [memcache] extension_dir = "/var/www/php5/lib/php/extensions/no-debug-non-zts-20060613/" extension=memcache.so |
到这里就安装结束了,重新加载php.ini配置后,打开phpinfo()看下是不是有memcache。
附php的memcache测试代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | < ?php $memcache = new Memcache; $memcache->connect('127.0.0.1', 11211) 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); ?> |
5、安装libevent
1 | yum -y install libevent-devel |
6、下载memcached
1 2 3 | wget -c http://memcached.googlecode.com/files/memcached-1.4.10.tar.gz tar xzf memcached-1.4.10.tar.gz cd memcached-1.4.10 |
7、开始编译安装memcached
1 2 3 | ./configure --prefix=/usr/local/memcached make make install |
8、配置memcached
1)下载启动脚本(备用下载地址:http://download.csdn.net/detail/my_yang/3923506)
1 | wget http://www.centos.bz/wp-content/uploads/2011/11/memcached -O /etc/init.d/memcached |
1 | chmod +x /etc/init.d/memcached |
2)创建文件/usr/local/memcached/memcached.conf,并写入以下代码(vim /usr/local/memcached/memcached.conf)。
1 2 3 4 5 | PORT="11211" USER="memcached" MAXCONN="2048" CACHESIZE="64" OPTIONS="" |
PORT=”11211″:指定端口为11211
USER=”memcached”:以memcached用户运行
MAXCONN=”2048″:最大连接数
CACHESIZE=”64″:设置缓存内存为64M
3)建立用户memcached,加入启动项,并启动memcached
1 2 3 4 5 | groupadd memcached useradd -g memcached memcached chkconfig --add memcached chkconfig memcached on service memcached start |