Using Redis for PHP installation
Before we start using Redis in PHP, we need to make sure that we have the Redis service and the PHP Redis driver installed and that PHP is working properly on your machine. Next, let's install the PHP redis driver:https://github.com/phpredis/phpredis/releases.
PHP Installation Redis Extensions
The following operations need to be done in the downloaded Phpredis directory:
$ wget HTTPS:Github.com/phpredis/phpredis/archive/2.2.4.tar.gz$ CD Phpredis-2.2.7 # Enter the Phpredis directory$ /usr/local /php/bin/ phpize # PHP installation path $ ./--with-php- config=/usr/local/< Span class= "PLN" >php/bin/php -config$ make && make install
If you are a PHP7 version, you need to download the specified branch:
-b php7 https://github.com/phpredis/phpredis.git
Modify the php.ini file
/usr/local/PHP/lib/php. INI
Add the following content:
="/usr/local/php/lib/php/extensions/no-debug-zts-20090626"extension=Redis. so
Restart PHP-FPM or Apache after the installation is complete. View the phpinfo information to see the Redis extension.
Connect to the Redis service
<?Php//connect to local Redis service $redis = new redis (); $redis Span class= "pun" >->connect ( ' 127.0.0.1 ' , 6379 echo "Connection to server sucessfully" ; //see if the service is running echo "Server is running:" $redis ->ping (); ?>
Execute the script with the output as:
Connection to server sucessfullyserveris running: PONG
Redis PHP String (string) instance
<?PhpConnect to a local Redis service$redis= New Redis();$redis-Connect(' 127.0.0.1 ', 6379 echo " Connection to Server sucessfully "; //set Redis string data $redis ->set ( "tutorial-name" , " Redis Tutorial "); //get stored data and output echo "Stored string in Redis:: " .->get ( "Tutorial-name" ?>
Execute the script with the output as:
Connection to server sucessfullyStoredstringin redis::redis Tutorial
Redis PHP list (list) instance
<?PhpConnect to a local Redis service$redis= New Redis();$redis-Connect(' 127.0.0.1 ', 6379);Echo"Connection to Server sucessfully"; Storing data in a list$redis-Lpush("Tutorial-list", "Redis");$redis-Lpush("Tutorial-list", "Mongodb");$redis->lpush ( "tutorial-list" , "Mysql" ); //get stored data and output $arList = $redis ->lrange ( "tutorial-list" , 0 , 5 echo "Stored string in Redis" ; Print_r $arList ?>
Execute the script with the output as:
Connection to server sucessfullyStoredstringin redisredisMongodbMysql
Redis PHP Keys Instance
<?PhpConnect to a local Redis service$redis= New Redis (); $redis ->connect (, 6379); "Connection to server sucessfully" ; //get data and output $arList = $redis ->keys ( "*" echo "Stored keys in Redis::" ; $arList ?>
Execute the script with the output as:
Connection to server sucessfullyStoredstringin redis::Tutorial-nametutorial -list
Problem handling:
Configure the Apache+php+redis+phpredis environment under CentOS.
Edit the PHP program that accesses the Redis cache test.php, which runs in the background as an application, can access Redis successfully, and is accessed as a Web page under Apache, and the code that accesses Redis and beyond is no longer executed. View Apache logs:/var/log/httpd/error_log, discovering that the code was running with an exception:
PHP Fatal error:uncaught exception ' redisexception ' with message ' Redis server went away ' in/var/www/html/test.php
On the Internet to check the exception is considered to be PHP sokcet time-out setting is too short, should be added before the code: Ini_set (' Default_socket_timeout ',-1);
The test still does not solve the problem, because the code behind the normal operation, so the judgment is not the problem of the code itself, but Apache does not allow access to network resources, try the following workaround:
Open the/etc/selinux/config and find one of the following:
Selinux=enforcing
Instead: selinux=disabled
Problem Solving!
If the problem still does not work, execute the following command:
/usr/sbin/setsebool httpd_can_network_connect=1
centos+apache+php Unable to access Redis solution