Memcache的安装(Linux+Nginx+PHP+Memcached) 不指定

jed , 2012-4-25 16:16 , 服务器技术 , 评论(0) , 阅读(4995) , Via 本站原创 | |
一、源码包准备
服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-v1.4.4 。
下载:http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz
另外,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent,

libevent的最新版本是libevent-1.4.13-stable。(如果你的系统已经安装了libevent,可以不用安装)
官网:http://www.monkey.org/~provos/libevent/
下载:http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz

准备Memcached的PHP扩展的源码安装包:
官网:http://pecl.php.net/get/memcache-2.2.5.tgz

Linux指令下载:

wget http://memcached.googlecode.com/files/memcached-1.4.4.tar.gz
wget http://www.monkey.org/~provos/libevent-1.4.13-stable.tar.gz
wget http://pecl.php.net/get/memcache-2.2.5.tgz

二、安装与配置
1、先安装libevent

tar zxvf libevent-1.4.13-stable.tar.gz
cd libevent-1.4.13-stable
./configure -prefix=/usr
make
make install
cd ../


2、测试libevent是否安装成功

ls -al /usr/lib | grep libevent
libevent-1.1a.so.1
libevent-1.1a.so.1.0.2
libevent-1.4.so.2
libevent-1.4.so.2.1.3
libevent.a
libevent_core-1.4.so.2
libevent_core-1.4.so.2.1.3
libevent_core.a
libevent_core.la
libevent_core.so
libevent_extra-1.4.so.2
libevent_extra-1.4.so.2.1.3
libevent_extra.a
libevent_extra.la
libevent_extra.so
libevent.la
libevent.so

版本不同,可能文件列表不同。

3、安装memcached,同时需要安装中指定libevent的安装位置

tar zxvf memcached-1.4.4.tar.gz
cd memcached-1.4.4
./configure -with-libevent=/usr
make && make install
cd ../

安装完成后会把memcached 自动放到 /usr/local/bin/memcached

4、测试是否成功安装memcached

ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 201869 12-14 21:44 /usr/local/bin/memcached

5、安装Memcache的PHP扩展

①安装PHP的memcache扩展

tar vxzf memcache-2.2.5.tgz
cd memcache-2.2.5
/usr/local/php/bin/phpize
./configure -enable-memcache -with-php-config=/usr/local/php/bin/php-config -with-zlib-dir
make
make install
cd ../

②上述安装完后会有类似这样的提示:

Installing shared extensions: /usr/local/webserver/php/lib/php/extensions/no-debug-non-zts-20060613/

③把php.ini中的extension_dir = “./”修改为

extension_dir = “/usr/local/php/lib/php/extensions/no-debug-non-zts-2007xxxx/”

④添加一行来载入memcache扩展:extension=memcache.so

三、memcached的基本设置
1.启动Memcache的服务器端:

memcached -d -m 10 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid

参数说明:

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是10MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址202.207.177.177,
-p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,我这里设置了256,按照你服务器的负载量来设定,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid,


出现如下错误:

[root@localhost memcached-1.2.2]# /usr/local/bin/memcached -d -m 10 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid
/usr/local/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory


查找原因,解决方案如下:

LD_DEBUG=libs /usr/local/bin/memcached -v

如下显示:

[root@uhz002102 soft]# LD_DEBUG=libs /usr/local/bin/memcached -v
      3109:     find library=libevent-1.4.so.2 [0]; searching
      3109:      search cache=/etc/ld.so.cache
      3109:      search path=/lib64/tls/x86_64:/lib64/tls:/lib64/x86_64:/lib64:/usr/lib64/tls/x86_64:/usr/lib64/tls:/usr/lib64/x86_64:/usr/lib64            (system search path)
      3109:       trying file=/lib64/tls/x86_64/libevent-1.4.so.2
      3109:       trying file=/lib64/tls/libevent-1.4.so.2
      3109:       trying file=/lib64/x86_64/libevent-1.4.so.2
      3109:       trying file=/lib64/libevent-1.4.so.2
      3109:       trying file=/usr/lib64/tls/x86_64/libevent-1.4.so.2
      3109:       trying file=/usr/lib64/tls/libevent-1.4.so.2
      3109:       trying file=/usr/lib64/x86_64/libevent-1.4.so.2
      3109:       trying file=/usr/lib64/libevent-1.4.so.2
      3109:
/usr/local/bin/memcached: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory

主要是看最后一行提示的错误,那么我们:

cp /usr/lib/libevent* /usr/lib64/

重新运行

/usr/local/bin/memcached -d -m 10 -u root -l 127.0.0.1 -p 11211 -c 256 -P /tmp/memcached.pid


成功!
2.如果要结束Memcache进程,执行:

kill `cat /tmp/memcached.pid`

也可以启动多个守护进程,不过端口不能重复。

3.检查Memcached是否启动

[root@uhz002102 soft]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  
tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      23369/php-cgi      
tcp        0      0 127.0.0.1:11211             0.0.0.0:*                   LISTEN      3192/memcached      
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3111/nginx          
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      2219/vsftpd        
tcp        0      0 :::3306                     :::*                        LISTEN      17881/mysqld        
tcp        0      0 :::22                       :::*                        LISTEN      2193/sshd

11211端口已经打开,说明Memcached已正常启动。

4.重启CentOS

reboot

四、Memcache环境测试
运行下面的php文件,如果有输出This is a test!,就表示环境搭建成功。开始你的Memcache的征途吧!


<?php  
$mem = new Memcache;
$mem->connect("202.207.177.177", 11211);
$mem->set('key', 'This is a test!', 0, 60);
$val = $mem->get('key');
echo $val;  
?>

五、参考资料
对Memcached有疑问的朋友可以参考下列文章:
Linux下的Memcache安装:http://www.ccvita.com/257.html
Nginx 0.8.x + PHP 5.2.10(FastCGI)搭建胜过Apache十倍的Web服务器: http://blog.s135.com/nginx_php_v5/

Tags: , ,
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]