This article mainly introduces how to configure the php client (phpredis) and connect to Redis. It has certain reference value. Now I share it with you. Friends in need can refer to it
Using php to connect to Redis, Memcache, etc. requires expansion. Taking CentOS as an example, we will introduce the client environment construction of phpredis.
Purchase 1 elastic cloud server ECS on Huawei Cloud (I chose CentOS 6.3), a distributed cache instance (DCS for Redis), I chose a stand-alone instance.
Note that the ECS and cache instances are configured with the same VPC and security group to ensure network interoperability.
## yum install gcc-c make
Step 2: Install the php development package and command line tools
Use yum to install directly.
yum install php-devel php-common php-cli
php -version
##Step 3: Install phpredis
The above is the latest version. You can also go to the redis official website or the php official website to download other versions of the phpredis client
step02: Unzip after downloading
## cd redis-4.1.0RC3
step03: Execute the extended command before compilation
phpize
step04: Configuration
## ./configure --with-php-config= /usr/bin/php-config
Pay attention to the location of php-config. It is recommended to first search and confirm the directory of the php-config file. Different operating systems have different PHP installation method and file location are different.
Find / -name php-config
##step05: Compile and install
make && make install
step06: Pay attention to adding the redis module after installation. Reference configuration
vim /usr/local/php/etc/php.ini
Add the following configuration items, note that redis.so is the same You must first search for the confirmation file directory.
extension = "/usr/lib64/php/modules/redis.so"
Save and exit and confirm The extension takes effect:
php -m |grep redis
If the above command returns redis, congratulations You, the phpredis client environment is set up.
Edit a redis.php file:
connect($redis_host, $redis_port) == false) { die($redis->getLastError()); } if ($redis->auth($user_pwd) == false) { die($redis->getLastError()); } if ($redis->set("welcome", "Hello, DCS for Redis!") == false) { die($redis->getLastError()); } $value = $redis->get("welcome"); echo $value; $redis->quit(); ?>
Execute php redis.php, you will find that you can normally connect to the cache instance and perform data operations~
ps:
1. If an error is reported after execution, the configuration is incorrect;
2. If there is no response after execution, the network may be blocked. Check the IP address and port, and then confirm whether the VPC and security group of the ECS and cache instance are consistent. If they are inconsistent, you need to establish a VPC peering connection or modify the security Group, for details, please view the Virtual Private Cloud Service and Distributed Cache Service in the Help Center.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
Use PHPstudy to deploy the PHP system under Windows server
PHP method to obtain the client’s real IP address
The above is the detailed content of How to configure php client (phpredis) and connect to Redis. For more information, please follow other related articles on the PHP Chinese website!