Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
PHP-FPM mode can't suppose unix socket connect ? #277
Predis is very good, I already use it in my project. But I have a problem.
This is a demo:
require "frame/predis/Autoloader.php"; Predis\Autoloader::register(); $redis = new Predis\Client([ 'scheme' => 'unix', 'path' => "/home/work/local/monitor/redis/redis.sock", ]); $redis->set('test', '123'); echo $redis->get('test')."###\n";
But, when I access http://xxx/test.php:
It connect redis use TCP socket, but I set my redis "port 0", so failure.
I use nginx/1.4.4, php/5.4, redis/3.02, predis/1.0.3 and my system is Red Hat 4.4.4.
There is another explanation for CentOS / RHEL 7 and related operating systems using SELinux.
SELinux blocks the connection between PHP-FPM and the unix sockets, even when permissions are 777. TCP connections are also blocked.
This SELinux block is evident in /var/log/audit/audit.log, where you'd see something like this for a UNIX socket connection
or, like the following for a TCP connection
IF you're running Redis as a UNIX socket (i.e. using Port 0 and you set unixsocket and unixsocketperm in /etc/redis.conf) then try the following on the Linux command line to generate a permissive SELinux policy that allows Apache or Nginx to read and write to the Redis unix socket.
This assumes your socket is named /var/run/redis/redis.sock. If you're using something other than redis.sock then change the
To verify the custom
Restart your web server and PHP-FPM for good measure
Kudos and more details on the Nginx blog under Example 2.
ELSE IF, you're running Redis as a TCP socket (i.e. how Redis works out of the box, like Port 6379 in /etc/redis.conf) then run
Versions probably don't matter, since the issue is if SELinux is installed and enabled, but I was running CentOS 7, Apache 2.4.23, Redis 3.25, Predis 1.1, PHP and PHP-FPM 7.1. Both Apache and Nginx would seem to experience these problems and have the same solutions.