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:
- Install memcache on your machine (there are several docs showing how to do this)
- Modify your php.ini file and set the memcache related consts/flags to the values that make sense for your environment
- Create an init script in ini.d to start memcache as a daemon
- 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)
memcached and Apache+PHP are two totally unrelated things, 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 memcachedor
aptitude install memcached
- Before modifying your php.ini, you must install the
memcacheextension for PHP — so PHP has functions to connect to memcached.
- This can be done using the
php5-memcachepackage of your distribution
- Or with the
pecl install memcachecommand, which will download the sources from pecl.php.net and compile them — which means you’ll need everything that’s needed to compile software.
- This can be done using the
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. [email protected]: ~/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.
sudo apt-get install memcached If you do not have a compiler on your server, you can download build-essential in order to install memcache: sudo pecl install memcache Finally use PECL (PHP Extension Community Library) to install memcache sudo pecl install memcache Once you have completed the installation of memcache with PECL on the VPS, add memcached to memcache.ini