[转]编译安装libevent,memcache,以及php的memcached扩展

时间:2021-01-08 00:17:46

一 安装libevent

1.去官网http://libevent.org/ 下载最新源码,我用的是libevent-2.0.20-stable.tar.gz 

2.解压到/usr/src目录 ,执行命令:sudo tar -zxvf libevent-2.0.20-stable.tar.gz -C /usr/src

3.进入解压以后的目录,用sudo方式执行命令:sudo ./configure --prefix=/usr/local/libevent ; make ; make install

二 安装memcache

1.去官网 http://memcached.org/ 下最新源码,我用的是memcached-1.4.15.tar.gz

2.解压到/usr/src目录 ,执行命令:sudo tar -zxvf memcached-1.4.15.tar.gz -C /usr/src

3.进入解压以后的目录,用sudo方式执行命令:sudo ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent; make ; make install

三 安装php的memcache扩展

   有两个版本一个是memcache(http://pecl.php.net/package/memcache ),另一个是基于libmemcached的memcached版本(http://pecl.php.net/package/memcached );

   网上查的资料是说前一个是原生的,后一个比前一个功能更强大。比较推荐使用基于libmemcahced 库的memcached扩展。支持memcache提供的CAS操作,稳定性和效率也更好。

这里我使用基于libmemcached 库的memcached扩展,安装步骤如下:

首先,安装libmemcached库

1.去网址 https://launchpad.net/libmemcached (或者http://libmemcached.org/libMemcached.html )下载源码,

   我用的是libmemcached-1.0.14.tar.gz

2.解压缩到/usr/src目录,用sudo方式执行命令:sudo tar -zxvf libmemcached-1.0.14.tar.gz -C /usr/src

3.进入目录/usr/src/libmemcached-1.0.14/,sudo方式执行命令:./configure ;make;make install (不加--prefix=/usr/local/libmemcached 指定安装目录时,libmemcached 默认安装在/usr/local/,头文件安装在/usr/local/include/libmemcachde/,动态库默认安装在/usr/local/lib/下。)

4.make时遇到问题:sorry, unimplemented: Graphite loop optimizations can only be used if the libcloog-ppl0 package is installed。解决办法是修改Makefile,查找并去掉 floop-parallelize-all(应该有两处需要去掉),看了网上资料说是去掉后表示不使用Graphite loop 优化。

5.再次make,遇到问题:fatal error: sasl/sasl.h: No such file or directory. 在网上搜到的解决办法是:

   Depending on your operating system, you'll need to install the cyrus-sasl development package.

   That would be cyrus-sasl-devel on RedHat based distros and libsasl2-dev on Debian based distros IIRC.

   我在ubuntu下直接用sudo apt-get install libsasl2-dev 安装了。

6.编译还是遇到问题,显示无法链接到sasl的一些文件。问题显示如下:

   undefined reference to `sasl_client_step'
   undefined reference to `sasl_dispose'
   undefined reference to `sasl_client_start'
   undefined reference to `sasl_client_new'

   解决办法是重新执行configure命令,增加相应的参数。sudo执行命令:sudo ./configure --enable-sasl --without-memcached LDFLAGS=-Wl,--as-neede ;make; make install

7.终于安装libmemcached库成功了。。。

其次,安装memcached版本的php扩展

1. 去网址http://pecl.php.net/package/memcached 下载memcached版本,我用的是memcached-2.1.0.tgz

2. 解压到/usr/src目录 ,执行命令:sudo tar -zxvf memcached-2.1.0.tgz -C /usr/src

3. 进入解压以后的目录,是没有configure文件的,用sudo方式执行命令来生成:sudo /usr/local/php/bin/phpize

4.遇到问题:Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script. 网上搜到的解决办法为:

cd /usr/src
wget http://ftp.gnu.org/gnu/m4/m4-1.4.9.tar.gz
tar -zvxf m4-1.4.9.tar.gz
cd m4-1.4.9/
./configure && make && make install
cd ../
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.62.tar.gz
tar -zvxf autoconf-2.62.tar.gz
cd autoconf-2.62/./configure && make && make install

5.这是再次执行sudo /usr/local/php/bin/phpize,就可以成功生成configure文件了。(phpize是用来扩展php扩展模块的,通过phpize可以建立php的外挂模块,官方说明网址为:http://php.net/manual/en/install.pecl.phpize.php)

6.用sudo方式执行命令./configure --with-php-config=/usr/local/php/bin/php-config; make ; make install

7.编译完成了,还需要做的是在/usr/local/lib/php.ini文件中加入extension值:extension=memcache.so 

8.重启/usr/local/php/sbin/php-fpm程序使配置生效,但是报错:

   ERROR: failed to open configuration file '/usr/local/php/etc/php-fpm.conf': No such file or directory (2)

   ERROR: failed to load configuration file '/usr/local/php/etc/php-fpm.conf'

   ERROR: FPM initialization failed

   解决办法为: (1)cd /usr/local/php/etc/; (2)mv php-fpm.conf.default php-fpm.conf

9.重启仍然出现问题:ERROR: [pool www] cannot get uid for user 'www'。解决办法为:编辑文件vim /usr/local/php/etc/php-fpm.conf,

   把user = nobody group = nobody  中的nobody修改成有效的用户和用户组名称。

10.php扩展可使用的一些方法可参考网址:http://blog.csdn.net/fwkjdaghappy1/article/details/7592337

参考网址:

http://www.dewen.org/q/844

http://hi.baidu.com/thinkinginlamp/item/5a3b2531e25dc6f62784f4fb

http://code.google.com/p/memcached/wiki/PHPClientComparison

https://bugs.launchpad.net/keystone/+bug/843080

http://www.cppblog.com/kefeng/archive/2010/10/11/129422.html

https://bugs.launchpad.net/libmemcached/+bug/656010

http://blog.csdn.net/vaal_water/article/details/6439483

http://www.spiration.co.uk/post/1385/Cannot-find-autoconf.-Please-check-your-autoconf-installation

https://gist.github.com/3359828

http://www.2cto.com/kf/201111/110140.html

http://blog.johntechinfo.com/technology/264

http://www.oschina.net/question/231397_36921

http://www.cnblogs.com/huangjingzhou/articles/2093706.html

http://blog.csdn.net/fwkjdaghappy1/article/details/7592337