您的位置:首页 > 数据库 > Redis

[转]阿里云 Linux 安装phpredis扩展 How to install Redis and Redis php client

2016-01-19 16:20 821 查看
按照前一篇的安装redis教程的步骤在阿里云装redis扩展的时候各种错误频出,于是乎去翻墙Google了一篇

转自 请翻墙点击打开链接
http://anton.logvinenko.name/en/blog/how-to-install-redis-and-redis-php-client.html

How to install Redis and Redis php client



Redis — cache-storage for data that is stored in RAM. In this case you will get very fast data access. It is similar to memcache but Redis has vast opportunities of data storage.

Redis allows you to store not only strings but also lists, sets, sorted sets and hashes. You can find more details about Redis onofficial Redis site.
I recommend to install Redis from a source code. In this case you will get the latest version.


Redis installation

So, how to install Redis from source code? Go to Redis Download page and find there the latest version. Download it and unpack:
mkdir /tmp/redis
cd /tmp/redis
wget http://download.redis.io/releases/redis-2.8.8.tar.gz tar xzf redis-*
cd redis-*

[/code]

Next step is to compile with make utility and install:
make
sudo make install clean

[/code]

If you have 64 bit system it is necessary to install libc6-dev-i386 before compile. In Ubuntu it is possible to make with flowing steps:
sudo apt-get install libc6-dev-i386
make 32bit
sudo make install clean

[/code]

Create config:
mkdir /etc/redis
cp redis.conf /etc/redis/redis.conf

[/code]

Edit it:
nano /etc/redis/redis.conf

[/code]

Example of minimal configuration:
#start as a daemon in background
daemonize yes
#set port, by default is 6379
port 6379
#set ip on which daemon will be listening for connections
bind 127.0.0.1
#where to dump database
dir /var/opt

[/code]

By default redis-server will install in /usr/local/bin/redis-server. Check it whith whereis command:
whereis redis-server
#redis-server: /usr/local/bin/redis-server

[/code]

If you want the server be accessible after system start, it is possible to write a line in /etc/rc.local file before "exit 0"
/usr/local/bin/redis-server /etc/redis/redis.conf

[/code]

Start server
redis-server /etc/redis/redis.conf

[/code]

And try to connect
redis-cli

[/code]


PhpRedis Installation

PhpRedis - PHP extension from Nicolas Favre-Felix, which allows to access to Redis from php. Why PhpRedis? There are other
php libraries that allow to communicate with Redis? The answer - speed. This module have been written on C and this mean that this is faster than libraries that have been written on php

We need php5-dev

In Ubuntu it is possible to install with this command:
apt-get install php5-dev

[/code]

In CentOS:
yum install php5-dev

[/code]

Next step is to download, compile and install phpredis
sudo -i
cd /tmp
wget https://github.com/nicolasff/phpredis/zipball/master -O phpredis.zip
unzip phpredis.zip
cd phpredis-*
phpize
./configure
make && make install

[/code]

Now it is necessary to add the module to php config for Ubuntu with PHP 5.3
touch /etc/php5/conf.d/redis.ini
echo extension=redis.so > /etc/php5/conf.d/redis.ini

[/code]

for Ubuntu with PHP 5.5
touch /etc/php5/conf.d/redis.ini
touch /etc/php5/mods-available/redis.ini
echo extension=redis.so > /etc/php5/mods-available/redis.ini
ln -s /etc/php5/mods-available/redis.ini /etc/php5/apache2/conf.d
ln -s /etc/php5/mods-available/redis.ini /etc/php5/cli/conf.d

[/code]

for CentOS
touch /etc/php.d/redis.ini
echo extension=redis.so > /etc/php.d/redis.ini

[/code]

You can check PhpRedis with command bellow, after that you should see "OK" inscription
php -r "if (new Redis() == true){ echo \"\r\n OK \r\n\"; }"

[/code]

Before using PhpRedis in your php scripts don't forget to restart your web server

if this is Apache
/etc/init.d/apache2 restart

[/code]

if nginx
/etc/init.d/nginx restart

[/code]

Methods for Redis class available on page PhpRedis


Useful resources:

Redis official documentation

try Redis online

The Little Redis Book is a free book about Redis, available in English, Russian and Italian

Databasesredis cache php highload
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息