##1 Install Redis
First, you need a Redis server. For local installation methods, please refer to "Redis Manual".
2 Install PHP extension
To connect to Redis in PHP, you also need to install the phpredis extension in PHP to connect to the Redis server.2.1 Windows system
Download the phpredis extension directly under Windows, address: https://pecl.php.net/package/redisReference : "How to install redis extension for PHP"
Note that you must download the expansion package according to your PHP version and number of bits (not the system number of bits), otherwise it will not be available. Then modify php.ini and add phpredis support:; 下载dll文件后放到在PHP安装目录ext下,再加上这一行 extension="php_redis.dll"
2.2 Linux command system
The Linux command installation method is as follows:sudo apt-get install php5-redis # Ubuntu yum install php-pecl-redis # CentOS
extension=redis.so
2.3 Linux source code installation
Install dependent tools:apt-get install php5-dev # Ubuntu yum install php-devel # CentOS
wget https://pecl.php.net/get/redis-3.0.0.tgz tar zxf redis-3.0.0.tgz cd redis-3.0.0 phpize ./configure --with-php-config=php-config make make install
extension=redis.so
3 Code test
Then, restart PHP-FPM and create a new PHP file, Code:<?php $redis = new Redis(); $redis->connect('127.0.0.1', 6379); $count = $redis->exists('count') ? $redis->get('count') : 1; echo $count; $redis->set('count', ++$count);
The above is the detailed content of How to connect and use Redis with PHP. For more information, please follow other related articles on the PHP Chinese website!