Linux下Redis配置並啟用PHP支援

來源:互聯網
上載者:User


redis目前(20150525)已經更新到了3.0版本,今天我們來學習如何在伺服器部署Redis,並啟用php支援。

Redis logo

I. redis

下載安裝

redis的各個版本能夠在http://download.redis.io/releases/找到

wget http://download.redis.io/releases/redis-stable.tar.gz
tar zxf redis-stable.tar.gz
cd redis-stable
make && make install

# 複製設定檔
cp redis.conf /etc/redis.conf

# 將二進位檔案拷貝至 /usr/local/bin
cd src
cp redis-server redis-cli redis-benchmark /usr/local/bin/
至此Redis安裝完成。

配置Redis

安裝完成後需要我們進行一些配置工作。

redis-server -v
#Redis server v=3.0.1 sha=00000000:0 malloc=jemalloc-3.6.0 bits=64 build=3858e635de930c10

# 建立資料庫檔案夾
mkdir /var/lib/redis_db/ -p

vim /etc/redis.conf

# 修改以下變數

daemonize yes   # 守護方式運行
logfile "/var/log/redis.log"  # 記錄檔
dir /var/lib/redis_db/      # 資料持久化檔案夾
現在redis的基本運行已經配置完成,以後我們再討論主從、持久化策略等進階特性。

運行redis

redis-server /etc/redis.conf

# 檢查作業記錄,還記得我們剛才配置的記錄檔嗎?

tail -100 /var/log/redis.log

# 顯示

                _._
           _.-``__ ''-._
      _.-``    `.  `_.  ''-._           Redis 3.0.1 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 19337
  `-._    `-._  `-./  _.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |           http://redis.io
  `-._    `-._`-.__.-'_.-'    _.-'
 |`-._`-._    `-.__.-'    _.-'_.-'|
 |    `-._`-._        _.-'_.-'    |
  `-._    `-._`-.__.-'_.-'    _.-'
      `-._    `-.__.-'    _.-'
          `-._        _.-'
              `-.__.-'

19337:M 25 May 22:32:40.369 # Server started, Redis version 3.0.1
19337:M 25 May 22:32:40.369 # 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.
19337:M 25 May 22:32:40.369 * DB loaded from disk: 0.000 seconds
19337:M 25 May 22:32:40.369 * The server is now ready to accept connections on port 6379
II. 配置PHP

接著我們編譯phpredis,項目地址

下載

git clone https://github.com/phpredis/phpredis.git phpredis
cd phpredis
/usr/local/php/bin/phpize
./configure

# 老高的環境變數沒有匯入所以編譯命令為 ./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

如果你的php不是編譯的,那麼phpize需要執行 yum install php-devel ,即安裝devel版本的php
編譯完成後你會得到

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

我們將其拷貝出來

cp /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/redis.so /usr/local/php/lib/php/extensions/

開啟redis擴充

類似 PHP安裝memcached擴充,最後我們需要在php.ini中添加以下配置

vim /etc/php.ini

# add

[Redis]
extension=redis.so

重啟Apache或php-fpm後,phpinfo頁面

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.