首页 >后端开发 >php教程 >centos6.4中安装redis与phpredis

centos6.4中安装redis与phpredis

WBOY
WBOY原创
2016-07-25 08:57:391068浏览
  1. $ wget http://download.redis.io/releases/redis-2.6.16.tar.gz
  2. $ tar xzf redis-2.6.16.tar.gz
  3. $ cd redis-2.6.16
  4. $ make install
复制代码

2.配置

  1. $ mkdir /etc/redis
  2. $ cp redis.conf /etc/redis/redis.conf
  3. $ gedit /etc/sysctl.conf$ sysctl -p
复制代码

注:sysctl.conf文件尾部加上vm.overcommit_memory=1

3.启动测试

  1. $ /usr/local/bin/redis-server /etc/redis/redis.conf
  2. $ /usr/local/bin/redis-cli
  3. $ set test xjx
  4. $ get test
复制代码

4.开机启动

  1. $ gedit /etc/redis/redis.conf

  2. 设置daemonize yes
  3. $ gedit /var/run/redis.pid

  4. 直接保存,主要是创建该文件
  5. gedit /etc/init.d/redis

  6. 编辑脚本,也可以自己下载或者输入以下内容
  7. #!/bin/bash
  8. #
  9. # redis - this script starts and stops the redis-server daemon
  10. #
  11. # chkconfig: - 80 12
  12. # description: Redis is a persistent key-value database
  13. # processname: redis-server
  14. # config: /etc/redis/redis.conf
  15. # pidfile: /var/run/redis.pid
  16. source /etc/init.d/functions

  17. BIN="/usr/local/bin"

  18. CONFIG="/etc/redis/redis.conf"
  19. PIDFILE="/var/run/redis.pid"
  20. ### Read configuration

  21. [ -r "$SYSCONFIG" ] && source "$SYSCONFIG"
  22. RETVAL=0

  23. prog="redis-server"
  24. desc="Redis Server"
  25. start() {

  26. if [ -e $PIDFILE ];then

  27. echo "$desc already running...."
  28. exit 1
  29. fi
  30. echo -n $"Starting $desc: "

  31. daemon $BIN/$prog $CONFIG
  32. RETVAL=$?

  33. echo
  34. [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
  35. return $RETVAL
  36. }
  37. stop() {

  38. echo -n $"Stop $desc: "
  39. killproc $prog
  40. RETVAL=$?
  41. echo
  42. [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog $PIDFILE
  43. return $RETVAL
  44. }
  45. restart() {

  46. stop
  47. start
  48. }
  49. case "$1" in

  50. start)
  51. start
  52. ;;
  53. stop)
  54. stop
  55. ;;
  56. restart)
  57. restart
  58. ;;
  59. condrestart)
  60. [ -e /var/lock/subsys/$prog ] && restart
  61. RETVAL=$?
  62. ;;
  63. status)
  64. status $prog
  65. RETVAL=$?
  66. ;;
  67. *)
  68. echo $"Usage: $0 {start|stop|restart|condrestart|status}"
  69. RETVAL=1
  70. esac
  71. exit $RETVAL

复制代码

改变文件权限:

  1. $ chmod 755 /etc/init.d/redis
复制代码

添加开机启动:

  1. $ chkconfig --add redis
  2. $ chkconfig --level 345 redis on
  3. $ chkconfig --list redis
复制代码

之后重启服务器,$ service redis status 查看是否正确配置

接下来介绍phpredis的安装与配置方法。

二、phpredis

1.自行下载解压 https://github.com/nicolasff/phpredis/archive/master.zip

2.安装

  1. $ cd /root/phpredis-master
  2. $ /usr/local/php/bin/phpize
  3. $ ./configure --with-php-config=/usr/local/php/bin/php-config
  4. $ make && make install
复制代码

注:路径根据实际情况修改 3.php扩展 $ gedit /usr/local/php/etc/php.ini 加入extension=redis.so,然后重启php-fpm(非nginx)

4.测试

  1. $redis = new Redis();
  2. $redis->connect('127.0.0.1',6379);
  3. $redis->set('test','hello world!');
  4. echo $redis->get('test');
复制代码

三、phpredisadmin http://down.admin5.com/php/75024.html 下载解压至www目录即可,这里提供大家,最好不用官方最新版本,个中原由,你用了就明白啦。



声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn