PHP – 有关一个PHP里Memcache的诡异问题
事情,是这样的。
环境:某服务器
系统:CentOS 64位
现在想搭建一个PHP+Nginx+Memcached的环境。
随后分别用yum安装了这三个东西
1 2 3 |
[root@centos ~]# yum install php [root@centos ~]# yum install nginx [root@centos ~]# yum install memcached |
接下来修改了些配置文件,如:
1 2 |
Nginx: /etc/nginx/nginx.conf PHP: /etc/php.ini |
然后重启Nginx服务:
1 |
[root@centos ~]# service nginx restart |
然后访问一个基于Yii的系统,提示如下错误:
那么很明显,是让我加载PHP的memcache扩展。
那么接下来想到了要给PHP安装memcache扩展,于是找了下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
[root@centos ~]# yum search memcache Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile =============== N/S Matched: memcache =============== php-pecl-memcached.x86_64 : Extension to work with the Memcached caching daemon libmemcached.x86_64 : Client library and command line tools for memcached server php-horde-Horde-Memcache.noarch : Horde Memcache API php-pecl-memcache.x86_64 : Extension to work with the Memcached caching daemon phpMemcachedAdmin.noarch : Graphic stand-alone administration for memcached to monitor and debug purpose python-memcached.noarch : A Python memcached client library dmlite-plugins-memcache.x86_64 : Memcached plugin for dmlite libmemcache.x86_64 : A client library for memcached libmemcache-devel.x86_64 : Header files, libraries and development documentation for libmemcache libmemcached.i686 : Client library and command line tools for memcached server libmemcached-devel.i686 : Header files and development libraries for libmemcached libmemcached-devel.x86_64 : Header files and development libraries for libmemcached memcached-devel.i686 : Files needed for development using memcached protocol memcached-devel.x86_64 : Files needed for development using memcached protocol perl-Apache-Session-Memcached.noarch : Perl module to store persistent data using memcached perl-Cache-Memcached.noarch : Perl client for memcached perl-Memcached-libmemcached.x86_64 : Thin fast full interface to the libmemcached client API php-ZendFramework-Cache-Backend-Libmemcached.noarch : Zend Framework libmemcache cache backend php-ZendFramework-Cache-Backend-Memcached.noarch : Zend Framework memcache cache backend dmlite-plugins-profiler.x86_64 : Memcached plugin for dmlite memcached.x86_64 : High Performance, Distributed Memory Object Cache |
于是看到了“php-pecl-memcache.x86_64”,装。
1 |
[root@centos ~]# yum install php-pecl-memcache.x86_64 |
装好后修改php.ini,添加:
1 |
extension=memcache.so |
然后重启Nginx,重新打开系统,依旧报同样的错误。
此时想到查一下PHP以及系统内是否已经安装了针对PHP的memcache,首先看了下系统内:
1 2 3 4 5 |
[root@centos /]# yum list installed | grep memcache libmemcached.x86_64 0.31-1.1.el6 @os memcached.x86_64 1.4.4-3.el6 @os php-pecl-memcache.x86_64 3.0.5-4.el6 @os php-pecl-memcached.x86_64 1.0.0-1.el6 @epel |
然后看PHP:
1 2 3 4 |
[root@centos /]# php -m | grep memcache PHP Warning: Module 'memcache' already loaded in Unknown on line 0 memcache memcached |
首先看到了这里有个Warning:Module ‘memcache’ already loaded in Unknown on line 0
虽然是Warning但需要排除下问题,于是找到了在“/etc/php.d/memcache.ini”中同样存在了“extension=memcache.so”这句话,把“/etc/php.ini”中添加的“extension=memcache.so”注释之后再“php -m”则无此Warning,但问题依旧,故和这个Warning无关。
然后在找,新建了两个程序:memcache.php-用于测试memcached是否正常,和,phpinfo.php,如下:
1 2 3 4 5 6 7 8 9 |
<?php $memcache = new Memcache(); $memcache->connect('127.0.0.1', 11211); $memcache->set('key', "Memcache test successful!", 0, 60); $result = $memcache->get('key'); unset($memcache); echo '[test]'; echo $result; ?> |
首先直接运行,结果如下:
1 2 |
[root@centos /]# php memcache.php [test]Memcache test successful! |
看样子Memcached和PHP是可以愉快的在一起玩耍的。那么再刷新系统页面,报错依旧。
于是在网页中打开“memcache.php”,但是页面一片空白无任何输出。
随后调试看到程序在“$memcache = new Memcache();”一句终止,这么看来PHP和Memcached还是没玩耍在一起。
于是找出了phpinfo在浏览器里看:
找不到memcache。联想到刚才两种执行方式的区别,于是再在服务器里看phpinfo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[root@centos /]# php phpinfo.php | grep memcache /etc/php.d/memcache.ini, /etc/php.d/memcached.ini, memcache memcache support => enabled memcache.allow_failover => 1 => 1 memcache.chunk_size => 32768 => 32768 memcache.compress_threshold => 20000 => 20000 memcache.default_port => 11211 => 11211 memcache.hash_function => crc32 => crc32 memcache.hash_strategy => consistent => consistent memcache.lock_timeout => 15 => 15 memcache.max_failover_attempts => 20 => 20 memcache.protocol => ascii => ascii memcache.redundancy => 1 => 1 memcache.session_redundancy => 2 => 2 memcached memcached support => enabled libmemcached version => 0.31 Registered save handlers => files user memcache memcached |
这么看也有。
好吧,继续找线索。
在phpinfo中找到了如下的参数:
1 2 3 4 5 6 7 8 |
Configuration File (php.ini) Path: /etc Loaded Configuration File: /etc/php.ini Scan this dir for additional .ini files: /etc/php.d Additional .ini files parsed: /etc/php.d/curl.ini, /etc/php.d/fileinfo.ini, /etc/php.d/json.ini, /etc/php.d/phar.ini, /etc/php.d/zip.ini .... extension_dir: /usr/lib64/php/modules |
此时好像找到问题了。
应该是PHP会加载/etc/php.d/目录下的所有配置文件,但是漏过了同在/etc/php.d/目录下的memcache.ini和memcached.ini。(在/usr/lib64/php/modules/中能够找到扩展文件:memcache.so)
但是转念想也不对,因为我把“extension=memcache.so”加在php.ini中也会报错。
那么,问题依旧,待续。
2BContinued.
妈蛋,诡异个锤子,重启服务器解决一切问题。