[Redis專輯][1]ubuntu12.04下安裝php-redis的方法和步驟,redisphp-redis

來源:互聯網
上載者:User

[Redis專輯][1]ubuntu12.04下安裝php-redis的方法和步驟,redisphp-redis

首次發布路徑:phpredis的安裝

很久很久沒有寫博文了,好多博文都沒有整理完畢,今天才抽時間整理完這一篇博文,希望能對大家有一定的協助

首先對redis做個簡單的介紹:

Redis 是完全開源免費的,遵守BSD協議,先進的key – value持久化產品。它通常被稱為資料結構伺服器,因為值(value)可以是 字串(String), 雜湊(Map), 列表(list), 集合(sets)和 有序集合(sorted sets)等類型。

這是官網的中文翻譯,英文的就沒必要寫上來了,如果想看英文的,可以去redis.io上去看看,中文的可以去redis.cn看看

關於php-redis的安裝和使用已經有很多人寫過去了,我在這裡只是完成一個驗證的過程

安裝redis

wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable
make
sudo make install  (如果是root使用者可以不用sudo)

安裝完畢後,拷貝下設定檔redis.conf,這裡是為了更方便的使用

sudo mkdir /etc/redis/
sudo cp redis.conf /etc/redis/

這時啟動下redis看看,啟動方法:

redis-server /etc/redis/redis.conf

啟動後的介面效果:

geeknimo@bogon:~/Documents/phpredis$ redis-server /etc/redis/redis.conf
[16859] 07 Aug 10:05:30.292 # You requested maxclients of 10000 requiring at least 10032 max file descriptors.
[16859] 07 Aug 10:05:30.293 # Redis can’t set maximum open files to 10032 because of OS error: Operation not permitted.
[16859] 07 Aug 10:05:30.293 # Current maximum open files is 1024. maxclients has been reduced to 4064 to compensate for low ulimit. If you need higher maxclients increase ‘ulimit -n’.
[16859] 07 Aug 10:05:30.293 # Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with ‘noeviction’ policy now.
_._
_.-“__ ”-._
_.-“ `. `_. ”-._ Redis 2.8.13 (00000000/0) 32 bit
.-“ .-“`. “`\/ _.,_ ”-._
( ‘ , .-` | `, ) Running in stand alone mode
|`-._`-…-` __…-.“-._|’` _.-’| Port: 6379
| `-._ `._ / _.-’ | PID: 16859
`-._ `-._ `-./ _.-’ _.-’
|`-._`-._ `-.__.-’ _.-’_.-’|
| `-._`-._ _.-’_.-’ | http://redis.io
`-._ `-._`-.__.-’_.-’ _.-’
|`-._`-._ `-.__.-’ _.-’_.-’|
| `-._`-._ _.-’_.-’ |
`-._ `-._`-.__.-’_.-’ _.-’
`-._ `-.__.-’ _.-’
`-._ _.-’
`-.__.-’

[16859] 07 Aug 10:05:30.308 # Server started, Redis version 2.8.13
[16859] 07 Aug 10:05:30.308 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add ‘vm.overcommit_memory = 1′ to /etc/sysctl.conf and then reboot or run the command ‘sysctl vm.overcommit_memory=1′ for this to take effect.
[16859] 07 Aug 10:05:30.308 * The server is now ready to accept connections on port 6379

如果想要後台運行,可以執行redis-server /etc/redis/redis.conf & ,或者修改設定檔/etc/redis/redis.conf中的daemonize設定為yes即可。

設定為daemonize後,啟動redis-server,並使用redis-cli對redis-server進行串連

redis-cli

串連成功後:

geeknimo@bogon:~/Documents/phpredis$ redis-cli
127.0.0.1:6379>

輸入info查看:

127.0.0.1:6379> info
# Server
redis_version:2.8.13
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:7331093c2c819968
redis_mode:standalone
os:Linux 3.2.0-29-generic-pae i686
arch_bits:32
multiplexing_api:epoll
gcc_version:4.6.3
process_id:16890
run_id:660fc790eb501ea29b01b8bb9551f4711ab5f151
tcp_port:6379
uptime_in_seconds:6
uptime_in_days:0
hz:10
lru_clock:14868319
config_file:/etc/redis/redis.conf

# Clients
connected_clients:1
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

# Memory
used_memory:424880
used_memory_human:414.92K
used_memory_rss:1609728
used_memory_peak:424880
used_memory_peak_human:414.92K
used_memory_lua:22528
mem_fragmentation_ratio:3.79
mem_allocator:jemalloc-3.6.0

# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1407377241
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

# Stats
total_connections_received:1
total_commands_processed:0
instantaneous_ops_per_sec:0
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:0
keyspace_misses:0
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:0

# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

# CPU
used_cpu_sys:0.00
used_cpu_user:0.00
used_cpu_sys_children:0.00
used_cpu_user_children:0.00

# Keyspace

這就表明redis服務啟動成功了,至於最佳化嘛,後面的文章再說

接下來我們去配置php-redis

首先要安裝git,php5,apache2,php5-dev

sudo apt-get install php5 php5-dev apache2

安裝需要點時間

在php-redis原始碼目錄下,執行

git clone https://github.com/nicolasff/phpredis.git

phpize

./configure

make && sudo make install

提示資訊:

Installing shared extensions:     /usr/lib/php5/20121212+lfs/

共用庫在上面提示的路徑下

配置php-redis

在/var/www/html下建立index.php,裡面寫上如下內容:

<?php
phpinfo();
?>

並在/etc/php5/mods-available下建立一個redis.ini,內容如下:

extension=redis.so

並在/etc/php5/apache2/conf.d和/etc/php5/cli/conf.d下建立一個與redis.ini的軟連結,其中cli這個部分很關鍵,否則php -m無法得到redis的任何模組資訊,即無法成功載入使用phpredis

ln -s /etc/php5/mods-available/redis.ini /etc/php5/cli/conf.d/10-redis.ini
ln -s /etc/php5/mods-available/redis.ini /etc/php5/apache2/conf.d/10-redis.ini

重啟apache2服務

sudo service apache2 restart

這裡開啟ubuntu對應的ip/index.php就可以看到php-redis的配置

Additional .ini files parsed /etc/php5/apache2/conf.d/05-opcache.ini, /etc/php5/apache2/conf.d/10-pdo.ini, /etc/php5/apache2/conf.d/10-redis.ini, /etc/php5/apache2/conf.d/20-json.ini, /etc/php5/apache2/conf.d/20-readline.ini
redis
Redis Support enabled
Redis Version 2.2.5
session
Session Support enabled
Registered save handlers files user redis
Registered serializer handlers php_serialize php php_binary wddx

有了這些資訊後開始進行測試代碼

建立testredis.php,內容如下:

<?php
$redis = new Redis();
$redis->connect(’127.0.0.1′,6379);
var_dump($redis->info());
?>

這時執行php testredis.php

可以在終端看到對應的內容

也可以在web端看到相應的內容了。

這樣就算基本上搭建好了php-redis。


php redis 怎解決回複功可以

redis會將資料存放區在記憶體中,斷電丟失。這個要注意一下,如有必要就做個持久化。持久化的方法一言難盡,可以參考網上的文章。

php的redis擴充叫php-redis。網上有php-redis的中文手冊,下面給你一個樣本:
<?php$redis = new redis(); $result = $redis->connect('127.0.0.1', 6379); // 6379是預設連接埠$result = $redis->set('9639002718',"comment"); // 設定索引值echo $result = $redis->get('9639002718'); // 擷取索引值$all = $redis->getMultiple(array('9639002718', '9639002718')); // 同時獲得多個索引值// 沒有提供獲得所有索引值的方法。下面這句我不確定是否能用,你可以試一試。$all = $redis->getMultiple(array('*'));
望採納,謝謝支援!

 
What's the next project

welcome!welcome!welcome!welcome!welcome!welcome!
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.