tags:

views:

247

answers:

1

I want to start using memcache with PHP (on Ubuntu 9.10). There are lots of info online which appear to show how to do this. Suprisingly though, none of the articles (I have seen so far), explicitly state whether you need to RUN the memcache process BEFORE attempting to use it or whether by simply calling new MemCache() via the PHP client library, a process will be spawned (if not already running).

From the various docs I have read on this so far, these are the steps that I think make sense:

  1. Install memcache on your machine (there are several docs showing how to do this)
  2. Modify your php.ini file and set the memcache related consts/flags to the values that make sense for your environment 3. Create an init script in ini.d to start memcache as a daemon
  3. Restart Apache daemon

Number 3 is the part that I need confirmation on, because none of the docs I have seen so far mentions the lifespan of the memcache process.

Can someone experienced in this confirm if this is the correct steps? Also if I have missed a step, let me know.

As an aside, since I am relatively new to Linux, I would be grateful if someone could post an example of an init script that would be needed to run the memcache daemon process (assuming that the steps I have outlined above are correct)

+5  A: 

memcached and Apache+PHP are two totally unrelated thing, at first. The only thing is PHP is connecting to the memcached daemon.

So, yes, memcached (note the 'd' at the end) must be started before you try using it : it you try connecting to a memcached server when it's not running, you just won't be able to connect -- it won't spawn a new daemon nor anything.

Using a simple comparison : it your Apache server is not started and you try to access to it using your browser, you'll get a "cannot connect" error ; it's exactly the same with PHP trying to connect to memcached : it will not be able to.


Now, for the installation process, here are a couple of notes, for the parts that don't look OK in what you said :

  • Install memcached on your machine
    • If you installed in using your distribution mecanism, it should have created the script in /etc/init.d/
    • And it should have started the daemon
    • As a reminder, to install something with Ubuntu, you should use apt-get install memcached or aptitude install memcached
  • Before modifying your php.ini, you must install the memcache extension for PHP -- so PHP has functions to connect to memcached.
    • This can be done using the php5-memcache package of your distribution
    • Or with the pecl install memcache command, which will download the sources from pecl.php.net and compile them -- which means you'll need everything that's needed to compile software.


Edit : easier to answer here than in a comment

I said installing memcached should be OK -- on Ubuntu, it seems there is a trap :

Using this :

ps -Alf | grep memcached

I don't see any running daemon :-(

And when I try to start the daemon, I get the following :

$ sudo /etc/init.d/memcached start
Starting memcached: memcached disabled in /etc/default/memcached.


So, on Ubuntu, you have to edit /etc/default/memcached, and change it ; instead of this :

# Set this to yes to enable memcached.
ENABLE_MEMCACHED=no

It should contain that :

# Set this to yes to enable memcached.
ENABLE_MEMCACHED=yes


Then, starting memcached should be possible :

$ sudo /etc/init.d/memcached start
Starting memcached: memcached.
test@tests: ~/temp
$ ps -Alf | grep mem
4 S nobody    3813     1  0  80   0 - 30025 ep_pol 00:17 pts/4    00:00:00 /usr/bin/memcached -m 64 -p 11211 -u nobody -l 127.0.0.1
0 R test      3819  2690  0  80   0 -  1834 -      00:17 pts/4    00:00:00 grep --color=auto mem


Note : now that memcached is enabled, it should be started automatically with your system, each time you reboot it.

Pascal MARTIN
Very good answer. I have already installed memcached, but I was not sure if the daemon was running or not. Using your instructions, I was able to double check and confirm the existence of the init script. BTW, running php -i | grep -i memcache shows that the memcache extension has been installed. I STILL cant confirm whether the daemon is running or not because when I type ps, I do not see the name of the memcache daemon process. (I know I can confirm this by writing a little php script to test memcache - but thats not the point). So how, may I check to see if the dameon is running?
Stick it to THE MAN
@Stick : I've tryied, and found out the problem ; the explanation is a bit long, so I've edited my answer to include some additionnal informations.
Pascal MARTIN
Absolutely fantastic answer!. Thank you very much Pascal
Stick it to THE MAN
You're welcome :-) Have fun ! *(By chance, I had just installed a Virtual Machine running the last version of Ubuntu -- and didn't enable memcached on it yet)*
Pascal MARTIN