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 配置成功