记录php5.4与php-redis冲突的解决
最近由于update了ubuntu的源,导致原来新源与本地系统库一些lib冲突,导致系统桌面显示异常,后来只有重装,新安装了php5.4.9,还安装了redis,php-redis扩展,php-redis扩展2,10。但把原来的项目一放上去,就报nginx 502错误(nginx 502错误真是个很头疼的问题)。打开nginx日志查看
[error] 29229#0: *1403 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 127.0.0.1, server:
不知道所云。后来打开php-fpm错误日志,得到的log日志
[08-Oct-2013 21:22:23] WARNING: [pool www] child 12026 exited on signal 11 (SIGSEGV - core dumped) after 5.997917 seconds from start
[08-Oct-2013 21:22:23] NOTICE: [pool www] child 12034 started
可以确定的一点,是php-fpm在一个时间点后重启了一个进程。还是毫无头绪。在网上搜了一大遍,有个有用的跟踪方法。
1、设置php-fpm,只起一个work进程
2、重启php-fpm, 执行ps aux| grep php-fpm 得到work进程号pid
3、strace -p pid
下面是我得到的信息
getcwd("/home/www/wms2.xxxx.com", 4096) = 26
lstat("/home/www/wms2.xiaomi.com/./RWebUser.php", 0x7ffff4e30a10) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xiaomi.com/protected/vendor/wms/api/RWebUser.php", 0x7ffff4e30a10) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xiaomi.com/protected/vendor/wms/RWebUser.php", 0x7ffff4e30a10) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xiaomi.com/protected/extensions/log/RWebUser.php", 0x7ffff4e30a10) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xiaomi.com/protected/modules/rights/components/RWebUser.php", {st_mode=S_IFREG|0777, st_size=2945, ...}) = 0
lstat("/home/www/wms2.xiaomi.com/protected/modules/rights/components", {st_mode=S_IFDIR|0777, st_size=4096, ...}) = 0
lstat("/home/www/wms2.xiaomi.com/protected/modules/rights", {st_mode=S_IFDIR|0777, st_size=4096, ...}) = 0
lstat("/home/www/wms2.xiaomi.com/protected/modules", {st_mode=S_IFDIR|0777, st_size=4096, ...}) = 0
open("/home/www/wms2.xiaomi.com/protected/modules/rights/components/RWebUser.php", O_RDONLY) = 5
fstat(5, {st_mode=S_IFREG|0777, st_size=2945, ...}) = 0
fstat(5, {st_mode=S_IFREG|0777, st_size=2945, ...}) = 0
fstat(5, {st_mode=S_IFREG|0777, st_size=2945, ...}) = 0
fstat(5, {st_mode=S_IFREG|0777, st_size=2945, ...}) = 0
mmap(NULL, 2945, PROT_READ, MAP_SHARED, 5, 0) = 0x7fd9b4804000
mmap(NULL, 266240, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fd9b442f000
munmap(0x7fd9b4804000, 2945) = 0
close(5) = 0
getcwd("/home/www/wms2.xxx.com", 4096) = 26
lstat("/home/www/wms2.xxx.com/./XMCCacheHttpSession.php", 0x7ffff4e31060) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xxx.com/protected/vendor/wms/api/XMCCacheHttpSession.php", 0x7ffff4e31060) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xxx.com/protected/vendor/wms/XMCCacheHttpSession.php", 0x7ffff4e31060) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xxx.com/protected/extensions/log/XMCCacheHttpSession.php", 0x7ffff4e31060) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xxx.com/protected/modules/rights/components/XMCCacheHttpSession.php", 0x7ffff4e31060) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xxx.com/protected/modules/rights/XMCCacheHttpSession.php", 0x7ffff4e31060) = -1 ENOENT (No such file or directory)
lstat("/home/www/wms2.xxx.com/protected/components/XMCCacheHttpSession.php", {st_mode=S_IFREG|0777, st_size=2612, ...}) = 0
open("/home/www/wms2.xxx.com/protected/components/XMCCacheHttpSession.php", O_RDONLY) = 5
fstat(5, {st_mode=S_IFREG|0777, st_size=2612, ...}) = 0
fstat(5, {st_mode=S_IFREG|0777, st_size=2612, ...}) = 0
fstat(5, {st_mode=S_IFREG|0777, st_size=2612, ...}) = 0
fstat(5, {st_mode=S_IFREG|0777, st_size=2612, ...}) = 0
mmap(NULL, 2612, PROT_READ, MAP_SHARED, 5, 0) = 0x7fd9b4804000
munmap(0x7fd9b4804000, 2612) = 0
close(5) = 0
sendto(4, "get 6cacd9674ad21aa083b5032b1a29"..., 39, MSG_DONTWAIT|MSG_NOSIGNAL, NULL, 0) = 39
recvfrom(4, 0x2806d88, 8196, 64, 0, 0) = -1 EAGAIN (Resource temporarily unavailable)
poll([{fd=4, events=POLLIN}], 1, 5000) = 1 ([{fd=4, revents=POLLIN}])
recvfrom(4, "VALUE 6cacd9674ad21aa083b5032b1a"..., 8196, MSG_DONTWAIT, NULL, NULL) = 128
lstat("/home/www/wms2.xxx.com/yii/web/CHttpCookie.php", {st_mode=S_IFREG|0777, st_size=2772, ...}) = 0
lstat("/home/www/wms2.xxx.com/yii/web", {st_mode=S_IFDIR|0777, st_size=4096, ...}) = 0
open("/home/www/wms2.xxx.com/yii/web/CHttpCookie.php", O_RDONLY) = 5
fstat(5, {st_mode=S_IFREG|0777, st_size=2772, ...}) = 0
fstat(5, {st_mode=S_IFREG|0777, st_size=2772, ...}) = 0
fstat(5, {st_mode=S_IFREG|0777, st_size=2772, ...}) = 0
fstat(5, {st_mode=S_IFREG|0777, st_size=2772, ...}) = 0
mmap(NULL, 2772, PROT_READ, MAP_SHARED, 5, 0) = 0x7fd9b4804000
munmap(0x7fd9b4804000, 2772) = 0
close(5) = 0
stat("/home/www/wms2.xxx.com/protected/extensions/redis/XMRedis.php", {st_mode=S_IFREG|0777, st_size=2672, ...}) = 0
lstat("/home/www/wms2.xxx.com/protected/extensions/redis/XMRedis.php", {st_mode=S_IFREG|0777, st_size=2672, ...}) = 0
lstat("/home/www/wms2.xxx.com/protected/extensions/redis", {st_mode=S_IFDIR|0777, st_size=4096, ...}) = 0
lstat("/home/www/wms2.xxx.com/protected/extensions", {st_mode=S_IFDIR|0777, st_size=4096, ...}) = 0
open("/home/www/wms2.xxx.com/protected/extensions/redis/XMRedis.php", O_RDONLY) = 5
fstat(5, {st_mode=S_IFREG|0777, st_size=2672, ...}) = 0
fstat(5, {st_mode=S_IFREG|0777, st_size=2672, ...}) = 0
fstat(5, {st_mode=S_IFREG|0777, st_size=2672, ...}) = 0
fstat(5, {st_mode=S_IFREG|0777, st_size=2672, ...}) = 0
mmap(NULL, 2672, PROT_READ, MAP_SHARED, 5, 0) = 0x7fd9b4804000
munmap(0x7fd9b4804000, 2672) = 0
close(5) = 0
socket(PF_INET6, SOCK_DGRAM, IPPROTO_IP) = 5
close(5) = 0
socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 5
fcntl(5, F_GETFL) = 0x2 (flags O_RDWR)
fcntl(5, F_SETFL, O_RDWR|O_NONBLOCK) = 0
connect(5, {sa_family=AF_INET, sin_port=htons(22122), sin_addr=inet_addr("10.237.36.231")}, 16) = -1 EINPROGRESS (Operation now in progress)
poll([{fd=5, events=POLLIN|POLLOUT|POLLERR|POLLHUP}], 1, 3000) = 1 ([{fd=5, revents=POLLOUT}])
getsockopt(5, SOL_SOCKET, SO_ERROR, [0], [4]) = 0
fcntl(5, F_SETFL, O_RDWR) = 0
setsockopt(5, SOL_TCP, TCP_NODELAY, [1], 4) = 0
poll([{fd=5, events=POLLIN|POLLPRI|POLLERR|POLLHUP}], 1, 0) = 0 (Timeout)
sendto(5, "*2\r\n$4\r\nAUTH\r\n$9\r\nxm_wms_rs\r\n", 29, MSG_DONTWAIT, NULL, 0) = 29
poll([{fd=5, events=POLLIN|POLLPRI|POLLERR|POLLHUP}], 1, 0) = 0 (Timeout)
poll([{fd=5, events=POLLIN|POLLERR|POLLHUP}], 1, 3000) = 1 ([{fd=5, revents=POLLIN}])
recvfrom(5, "+OK\r\n", 8192, MSG_DONTWAIT, NULL, NULL) = 5
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
Process 6011 detached
可以看得出,在执行XMRedis后进程6011 就死掉了。基本可以定位是php-redis出了问题。
继续追查最终发现了有段代码问题
public function conn() {
try {
$this->_redis->pconnect($this->host, $this->port, $this->timeout);
// Set client option. must AFTER connected
//var_dump(Redis::OPT_SERIALIZER,Redis::SERIALIZER_PHP);exit();
$this->_redis->setOption(Redis::OPT_SERIALIZER,Redis::SERIALIZER_PHP);
//$this->_redis->setOption(0, 0);
$this->_redis->auth("xm_wms_rs");
$this->connected = true;
} catch (RedisException $e) {
throw new CHttpException(500, "Redis occurs an error:" . $e->getMessage());
}
}
用红色的代码那段,就会报502错误,而用绿色代码这段就没问题。
setOption 这个是个神马东西呢?查了下php-redis api
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_NONE); // don't
serialize data 不序列化数据
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); // use built-in serialize/unserialize 用php内置的序列化
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_IGBINARY); // use igBinary serialize/unserialize 用扩展IGBINARY序列化
$redis->setOption(Redis::OPT_PREFIX, 'myAppName:'); // use custom prefix on all keys redis key前缀
$redis->setOption(Redis::OPT_SERIALIZER, Redis::SERIALIZER_PHP); 用此就报502
为什么用php内置的序列化就会出错呢?到底发生了什么?
后来又安装了IGBINARY 扩展,还是不行。最后把php-redis安装了最新的版本2.24,再运行,就完全没问题了。
2.24版本php-redis源码地址https://github.com/nicolasff/phpredis