Home  >  Article  >  Backend Development  >  How to set up redis in php

How to set up redis in php

藏色散人
藏色散人Original
2020-11-05 10:33:542358browse

How to set up redis in php: first install the redis service and PHP redis driver; then open the php.ini file; then add the content as "extension=redis.so"; and finally restart php-fpm or apache.

How to set up redis in php

Recommended: "PHP Video Tutorial" "redis Tutorial"

PHP settings use Redis

Installation

Before starting to use Redis in PHP, we need to ensure that the redis service and PHP redis driver have been installed, and your PHP can be used normally on the machine. Next, let us install the PHP redis driver: The download address is: https://github.com/phpredis/phpredis/releases.

PHP installation redis extension

The following operations need to be completed in the downloaded phpredis directory:

$ wget https://github.com/phpredis/phpredis/archive/3.1.4.tar.gz
$ cd phpredis-3.1.4                      # 进入 phpredis 目录
$ /usr/local/php/bin/phpize              # php安装后的路径
$ ./configure --with-php-config=/usr/local/php/bin/php-config
$ make && make install

Modify the php.ini file

vi /usr/local/php/lib/php.ini

Add the following content:

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20090626"
extension=redis.so

Restart php-fpm or apache after the installation is complete. Check the phpinfo information and you can see the redis extension.

PHP uses Redis

to connect to the redis service

<?php
    //连接本地的 Redis 服务
   $redis = new Redis();
   $redis->connect(&#39;127.0.0.1&#39;, 6379);
   echo "Connection to server successfully";
         //查看服务是否运行
   echo "Server is running: " . $redis->ping();
?>

Execute the script, the output result is:

Connection to server sucessfully
Server is running: PONG

The above is the detailed content of How to set up redis in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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