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

How to connect and use Redis with PHP

藏色散人
Release: 2023-04-04 18:14:01
forward
16130 people have browsed it



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"
Copy after login

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
Copy after login

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
Copy after login
Copy after login

2.3 Linux source code installation

Install dependent tools:

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

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
Copy after login

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
Copy after login
Copy after login

3 Code test

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

<?php
    $redis = new Redis();
    $redis->connect(&#39;127.0.0.1&#39;, 6379);
    $count = $redis->exists(&#39;count&#39;) ? $redis->get(&#39;count&#39;) : 1;
    echo $count;
    $redis->set(&#39;count&#39;, ++$count);
Copy after login

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!

Related labels:
source:awaimai.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template