Home>Article>Backend Development> How to connect and use Redis with PHP

How to connect and use Redis with PHP

藏色散人
藏色散人 forward
2019-01-11 16:56:40 16017browse



How to connect and use Redis with PHP

##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/redis

Reference : "

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

CentOS needs to install the EPEL source first. Please refer to: Installing the EPEL software source on CentOS.

Then modify php.ini and add the following line:

extension=redis.so

2.3 Linux source code installation

Install dependent tools:

apt-get install php5-dev # Ubuntu yum install php-devel # CentOS

Then download, compile, and install phpredis:

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

The above phpize and php-config are commands in the php dev version.

If the prompt does not include these two commands, you can point to the full path, usually in the /usr/bin/ directory.

Then, open php.ini, and finally add:

extension=redis.so

3 Code test

Then, restart PHP-FPM and create a new PHP file, Code:

connect('127.0.0.1', 6379); $count = $redis->exists('count') ? $redis->get('count') : 1; echo $count; $redis->set('count', ++$count);

Refresh the page. If you see increasing numbers, it means the connection to Redis is normal.



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!

Statement:
This article is reproduced at:awaimai.com. If there is any infringement, please contact admin@php.cn delete
Previous article:what is thinkphp Next article:what is thinkphp