Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Memcache' not found in command line PHP script

I have Memcache installed and working for PHP apps run through Apache (v2.2) but when I try to run a .php file in the command-line i get this error:

Fatal error: Class 'Memcache' not found in /usr/local/Matters/app/matters-common/connection.php on line 94

Line 94 is:

$memcache = new Memcache;

Other info:

CentOS 5.2
PHP 5.2.5 (cli) (built: Feb 20 2008 21:13:12)
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies
Apache v2.2.8

like image 541
Richard Stelling Avatar asked Jul 23 '09 12:07

Richard Stelling


People also ask

How do I enable Memcached PHP?

To enable the PHP Memcache extensions, you need to build PHP utilizing –enable-Memcache option when building, and configure it from the source. On Debian-based dispersions, you can use the php-Memcache package. To set global runtime configuration choices, specify the configuration option values within your php.

How to check if memcached is installed?

If you want to check whether memcache is running you can run : ps -ef | grep memcache This will work on Linux as long as you have the correct permissions (I ran it as root).

What does memcached do in PHP?

Memcached is an object caching system. It is primarily used to cache the results of database queries, helping dynamic websites like WordPress® and Drupal to serve pages faster. It can also significantly decrease resource use on a busy web server by reducing calls to the database.


2 Answers

Presumably you have separate php.ini files set up for apache and the command line (cli).

If so, you need to add the following to your cli php.ini file:

extension=memcache.so

On Ubuntu it's in /etc/php5/cli/php.ini

If it's working then memcache should appear in the list of modules if you run php -m on the command line.

Alternatively, you can create a file /etc/php5/cond.d/memcache.ini with the same contents.

like image 59
John Carter Avatar answered Sep 18 '22 16:09

John Carter


It is possible that you have a separate php.ini file for CLI mode. This file might not include memcache extension.

like image 20
leafnode Avatar answered Sep 17 '22 16:09

leafnode