手记

php开启与安装 memcache

安装memcache

# wgethttp://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz
# wget http://www.monkey.org/~provos/libevent-1.2.tar.gz

2.先安装libevent:
# tar zxvf libevent-1.2.tar.gz
# cd libevent-1.2
# ./configure –prefix=/usr
# make
# make install

3.测试libevent是否安装成功:
# ls -al /usr/lib | grep libevent
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent-1.2.so.1 ->libevent-1.2.so.1.0.3
-rwxr-xr-x 1 root root 263546 11?? 12 17:38 libevent-1.2.so.1.0.3
-rw-r–r– 1 root root 454156 11?? 12 17:38 libevent.a
-rwxr-xr-x 1 root root 811 11?? 12 17:38 libevent.la
lrwxrwxrwx 1 root root 21 11?? 12 17:38 libevent.so -> libevent-1.2.so.1.0.3
还不错,都安装上了。

4.安装memcached,同时需要安装中指定libevent的安装位置:
# cd /tmp
# tar zxvf memcached-1.2.0.tar.gz
# cd memcached-1.2.0
# ./configure –with-libevent=/usr
# make
# make install
如果中间出现报错,请仔细检查错误信息,按照错误信息来配置或者增加相应的库或者路径。
安装完成后会把memcached放到 /usr/local/bin/memcached ,

5.测试是否成功安装memcached:
# ls -al /usr/local/bin/mem*
-rwxr-xr-x 1 root root 137986 11?? 12 17:39 /usr/local/bin/memcached
-rwxr-xr-x 1 root root 140179 11?? 12 17:39 /usr/local/bin/memcached-debug


6.启动memcache

# /usr/local/bin/memcached -d -m 10 -uroot -l 192.168.0.200 -p 11211 -c 256 -P /tmp/memcached.pid

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

想开机自动启动的话,只需在/etc/rc.d/rc.local中加入一行,上面命令
有人用以下命令:
/usr/local/memcached/bin/memcached -d -m 20 -p 11211 -u apache
上面有些东西可以参考一下:即,ip不指定时,默认是本机,用户,最好选择是:apache 或 deamon
这样,也就是属于哪个用户的服务,由哪个用户启动。

启动服务时出现error while loadingshared libraries: libevent-2.0.so.5: cannot open shared object file: No suchfile or directory


>whereis libevent-2.0.so.5


libevent-2.0.so.5: /usr/local/lib/libevent-2.0.so.5


> ldd /usr/local/bin/memcached

        libevent-2.0.so.5 => not found

      libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b83fce0e000)

      libc.so.6 => /lib64/libc.so.6 (0x00002b83fd029000)

      librt.so.1 => /lib64/librt.so.1 (0x00002b83fd381000)

      /lib64/ld-linux-x86-64.so.2 (0x00002b83fc9b0000)


> LD_DEBUG=libs ./memcached -v

找到默认路径 /usr/lib/


>sudo ln -s /usr/lib/libevent-2.0.so.5/usr/lib64/libevent-2.0.so.5


>sudo ldd /usr/local/bin/memcached

         libevent-2.0.so.5 => /usr/lib64/libevent-2.0.so.5(0x00002b83fcbcd000)

      libpthread.so.0 => /lib64/libpthread.so.0 (0x00002b83fce0e000)

      libc.so.6 => /lib64/libc.so.6 (0x00002b83fd029000)

      librt.so.1 => /lib64/librt.so.1 (0x00002b83fd381000)

      /lib64/ld-linux-x86-64.so.2 (0x00002b83fc9b0000)


Php 加载memcache模块

1.wget http://pecl.php.net/package/memcache/memcache-2.2.7.tgz

tarzxvf memcache-2.2.7.tgz

cdmemcache-2.2.7.tgz

/usr/local/php/bin/phpize              ####php 安装路径

./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config--with -zlib-dir

make && make install


然后会得到提示

Installing sharedextensions:/usr/www/php/lib/php/extensions/no-debug-non-zts-20060613/

记下这个路径,查看phpinfo找到加载的php.ini的位置修改里面的

extension_dir="/usr/www/php/lib/php/extensions/no-debug-non-zts-20060613/"

添加

extension=memcache.so

重启php

/etc/init.d/php-fpm restart



使用PHP.info查看memcache 安装是否成功:

<?php

 phpinfo();

?>

上面是php.info 文件内容



memcached 启动脚本:

  1. #!/bin/sh  

  2. #  

  3. # memcached:    MemCached Daemon  

  4. #  

  5. # chkconfig:    - 9025

  6. # description:  MemCached Daemon  

  7. #  

  8. # Source function library.  

  9. . /etc/rc.d/init.d/functions  

  10. . /etc/sysconfig/network  


  11. start()    

  12. {  

  13.        echo -n $"Starting memcached: "

  14.        daemon /usr/local/bin/memcached -u daemon -d -m 4096 -l 10.10.10.220 -p 58728

  15.        ######根据自己需要进行修改

  16.        echo  

  17. }  


  18. stop()    

  19. {  

  20.        echo -n $"Shutting down memcached: "

  21.        killproc memcached    

  22.        echo  

  23. }  


  24. [ -f /usr/local/bin/memcached ] || exit 0


  25. # See how we were called.  

  26. case "$1" in  

  27.  start)  

  28.        start  

  29.        ;;  

  30.  stop)  

  31.        stop  

  32.        ;;  

  33.  restart|reload)  

  34.        stop  

  35.        start  

  36.        ;;  

  37.  condrestart)  

  38.        stop  

  39.        start  

  40.        ;;  

  41.  *)  

  42.        echo $"Usage: $0 {start|stop|restart|reload|condrestart}"

  43.        exit 1

  44. esac  

  45. exit 0





0人推荐
随时随地看视频
慕课网APP