centos7 下redis的安装与配置


1.安装

官方网站: https://redis.io/
 
下载:
1 $ cd /usr/local/src
2 $ wget http://download.redis.io/releases/redis-3.2.8.tar.gz
3 $ tar xzf redis-3.2.8.tar.gz
4 $ cd redis-3.2.8

 

编译:
先阅读README
1 To force compiling against libc malloc,use:
2 % make MALLOC=libc
3 To compile against jemalloc on Mac OS X systems,use:
4 % make MALLOC=jemalloc

所以

如果是linux系统 使用
1 $ make MALLOC=libc
2 编译
3 编译成功之后,运行 
4 $ make install

 

再看README 文件
1 In order to install Redis binaries into/usr/local/bin just use:
2 % make install
3 You can use`make PREFIX=/some/other/directory install`if you wish to use a
4 different destination.
5 Make install will just install binaries in your system, but will not configure
6 init scripts and configuration files in the appropriate place.

 

当安装完毕之后,只生成了4个可执行的二进制文件
 1 [root@localhost bin]# pwd
 2 /usr/local/bin
 3 [root@localhost bin]# ll
 4 total 6880
 5 -rwxr-xr-x.1 root root 337792Mar809:32 redis-benchmark
 6 -rwxr-xr-x.1 root root 25176Mar809:32 redis-check-aof
 7 -rwxr-xr-x.1 root root 3091736Mar809:32 redis-check-rdb
 8 -rwxr-xr-x.1 root root 490832Mar809:32 redis-cli 客户端
 9 lrwxrwxrwx.1 root root 12Mar809:32 redis-sentinel -> redis-server
10 -rwxr-xr-x.1 root root 3091736Mar809:32 redis-server 服务端

 

为了正常使用,还必须进行下一步操作:
1 if you are installing
2 it the proper way for a production system, we have a script doing this
3 forUbuntuandDebian systems:
4 % cd utils
5 %./install_server.sh
6 The script will ask you a few questions and will setup everything you need
7 to run Redis properly as a background daemon that will start again on
8 system reboots.

 

一路下一步,安装完成后,产生以下文件
1 Selected config:
2 Port           :6379
3 Config file    :/etc/redis/6379.conf
4 Log file       :/var/log/redis_6379.log
5 Data dir       :/var/lib/redis/6379
6 Executable     :/usr/local/bin/redis-server
7 CliExecutable:/usr/local/bin/redis-cli

2.客户端测试

开启服务
1 [root@localhost bin]# pwd
2 /usr/local/bin
3 [root@localhost bin]#./redis-server
4 或者是
5 ./redis-server /etc/redis/6379.conf (对应的配置文件)

 

进入客户端
1 [root@localhost bin]#./redis-cli
2 127.0.0.1:6379>set key1 aa
3 OK
4 127.0.0.1:6379>get key1
5 "aa"
6 127.0.0.1:6379>exit

3.php中使用redis

安装php-redis
1 yum -y install php-pecl-redis 
php中开启redis 模块
1 vim /etc/php.ini
2 加入:
3 extension=redis.so
重启apache
1 service httpd restart
查看phpinfo()
 

 

出现redis选项,说明安装成功了
 
编辑redistest.php
 1 <?php
 2 try{
 3 //连接本地的 Redis 服务
 4 $redis =newRedis();
 5 $redis->connect('127.0.0.1',6379);
 6 echo "Connection to server sucessfully\n";
 7 $redis->set('myname','laohuamao');
 8 echo $redis->get('myname')."\n";# 返回:ikodota
 9 //查看服务是否运行
10 echo "Server is running: ". $redis->ping()."\n";
11 }catch(exception $e){
12 echo $e->getMessage();
13 }

 

测试:
Permission denied
Warning:Redis::connect(): connect() failed:Permission denied in/var/www/html/redistest.php on line 5
Connection to server sucessfully Redis server went away

 

出现这种情况,是因为
By default, SELinux does not allow Apache to make socket connections.
解决方法:
1 /usr/sbin/setsebool httpd_can_network_connect=1

 

再运行
1 Connection to server sucessfully laohuamao Serveris running:+PONG

 

OK,php-redis 配置成功
 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM