Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 912
Fragsworth Avatar asked Apr 23 '13 01:04

Fragsworth


2 Answers

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
like image 34
ktyg Avatar answered Oct 23 '22 14:10

ktyg


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.

like image 187
llaumgui Avatar answered Oct 23 '22 15:10

llaumgui