简体   繁体   中英

PHP, Memcached works from command line but not from the web

I have PHP 5.3.3 installed on Centos 6.4 with the memcached.so extension, and httpd is running with version 2.2.15-26. Here is my index.php :

$mc = new \Memcached();
$mc->addServer('127.0.0.1', 11211);
$mc->set("test", "blah");
var_dump($mc->getResultCode());
var_dump($mc->getResultMessage());
var_dump($mc->get("test"));
die;

When I run it from the command line, it works . I get the following:

10:22:33 $ php index.php
int(0)
string(7) "SUCCESS"
string(4) "blah"

The memcache server also works from telnet. However, when I run index.php from the web, it fails. I get the following (from viewing webpage source):

int(47)
string(51) "SERVER HAS FAILED AND IS DISABLED UNTIL TIMED RETRY"
bool(false)

Short of re-installing my OS and trying different versions of crap, can anyone explain what might cause this issue?

Is it an SELinux problem ? Cli can access to Memcached but daemon no. Try this :

  • getenforce to know if you have SELinux enabled
  • setenforce 0 to disable it
  • reboot
  • Retry your test

If is good, You must configure Apache to access to Memcached.

I have similar issue on CentOS and what I found it I am running SELinux which prevent httpd to connect to memcached. You need to set below,

# setsebool -P httpd_can_network_memcache 1
# getsebool httpd_can_network_memcache
httpd_can_network_memcache --> on

Please ensure that your memcache service should bind all IPs. Default value is 127.0.0.1. change it to 0.0.0.0 for supporting all defined Ips. In additional, don't forget to control your iptables or firewall.

I had this problem in WAMP 2.4, running a simple Memcache test script worked from the command line, but not in the browser.

The answer turned out to be stunningly mundane: WAMP had two php.ini files, and I was editing the wrong one.

Eg Apache used this one: c:\\wamp\\bin\\apache\\Apache2.4.4\\bin\\php.ini WAMP also had this one: c:\\wamp\\bin\\php\\php5.4.12\\php.ini

Putting the extension=php_memcache.dll in the correct .ini file fixed things.

My clue something like this was the problem was that phpInfo()'s loaded config file reported different values in the two cases.

I had exactly the same issue as described by the OP. It turned out that the problem was caused by the server list the memcached extension maintains internally. My code was something like:

$serversList = $memcached->getServerList();

if (empty($serversList)) {
     $memcached->addServer($host, $port);
}

My initial call to the testing script was done with a wrong value for $port. The call was done from web (apache) not from cli. After i corrected the port and i ran the code again, it was skipping the 'if' and used the existing servers list which was flawed and so it failed again.

Seeing the failure from the web i tested with the cli and it was working perfectly. In the cli the servers list was different from the one in the web. In fact, the server list was empty at each start of the script, even if my script was setting it with each run. Yet it was persisting between calls on the web.

Anyway, after clearing the servers list on the web and setting the correct server, it worked as expected from the web too.

When I look at examples I see it being used without the namespace "\\" modifier. Try without it maybe?

http://www.php.net/manual/en/memcache.examples-overview.php

<?php

$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM