In the first introduction to Redis, the installation and use of Redis are introduced, but only in this way, Redis cannot be operated in PHP. In order to be able to operate Redis in PHP, an expansion pack needs to be installed.
Download address:
https://github.com/phpredis/phpredis
Install after downloading:
<code><span>#unzip phpredis-develop.zip</span><span>#cd phpredis-develop</span><span>#/usr/local/php/bin/phpize</span><span>#./configure --with-php-c/local/php/bin/php-config</span><span>#make</span><span>#make install</span></code>
/usr/local/php directory is the PHP installation directory in my system. This is determined according to the PHP installation location of your system.
After the installation is complete, you need to modify PHP's php.ini configuration file, which is located in /usr/local/php/etc/php.ini. Add a line:
<code><span>extension</span>=<span>redis.so</span></code>
Then restart php-fpm. If it is an Apache environment, restart Apache service.
After the restart is complete, check to see if there is Redis support in PHP. Output phpinfo() to see:
<code><span><span><?php </span> phpinfo(); <span>?></span></span></span></code>
Seeing such a display means that the Redis extension was installed successfully!
It is recommended to run the verification program that comes with the phpredis extension. In the tests directory in the unzipped directory:
<code><span>#cd phpredis-develop/tests/</span><span>#php TestRedis.php</span></code>
Write a test code to verify it:
<code><span></span>php <span>$redis</span><span>=</span><span>new</span> Redis(); <span>$redis</span><span>-></span>connect(<span>'127.0.0.1'</span>,<span>6379</span>); <span>// 连接</span><span>//$r = $redis->ping(); // 检查连接状态,返回'+PONG'表示链接可用</span><span>$redis</span><span>-></span><span>set</span>(<span>'n4'</span>,<span>'DA L'</span>); <span>// 设置set(key,value)</span><span>$r</span><span>=</span><span>$redis</span><span>-></span>get(<span>'n4'</span>); echo <span>$r</span>; <span>$redis</span><span>-></span>del(<span>'n4'</span>); <span>// 移除del(key)</span></code>
If the verification passes, it means that the installed Redis extension is available, and that’s it. Start operating Redis in PHP!
The instructions for operating Redis in PHP are very similar to the instructions for operating the Redis terminal. For more detailed documents, please refer to:
Standard: https://github.com/phpredis/phpredis
Chinese (translated with the efforts of others): http://www.cnblogs.com/weafer/archive/2011/09/21/2184059.html
The above introduces the installation and application of PHP-Redis extension, including Apache and github content. I hope it will be helpful to friends who are interested in PHP tutorials.