How to setup Memcached/APC on Ubuntu Server 10.04 for PHP?

I will be soon setting up Simple Machines forum on a fresh Ubuntu Server 10.04 VPS. The forum software is written in PHP. I will using Nginx as the web server, and I will be following a guide to install PHP-FPM on it: http://constantshift.com/install-php-fpm-5-3-2-on-ubuntu-10-04-lucid-lynx/

But the guide doesn't really explain anything about setting up Memcached or APC for caching. After looking around the net, searching this site and searching Stack Overflow, I'm still lacking answers for how to set these up successfully.

Can anyone help with information, guides, links? Much appreciated.


Solution 1:

First, decide if you need memcached. APC is both an "accelerator" (an opcode cache, which is fairly transparent), and a caching solution (provides an in-memory data store that code needs to write/read from). memcache only does the latter.

The only reason you'd typically need memcached is if you're going to be running multiple servers that need to read/write from the same cache. As long as you're only running a single web server host, APC will do the trick.

Installing APC is pretty easy.

[root@host]# pecl install apc     #or sometimes pecl install apc-beta
[root@host]# service php5-fpm start

You'll probably notice an immediate performance boost, just with APC's default settings.

You can then tweak as needed. See the docs for various settings.

The most common setup I've seen is a single segment, sized in a way that makes sense for your system:

In php.ini:

extension=apc.so
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 50M 

APC comes with a handy apc.php script that will give you lots of useful data (like how many hits/misses you're getting), along with pretty graphs.

Solution 2:

Memcached is a server. So install it with apt-get install memcached The PHP need an extension to work with memcached : apt-get install php5-memcache The accelerator APC is provided by another extension : apt-get install php-apc

Don't forget to restart your Web server to use the new modules.

I forget the links : [http://php.net/manual], then check memcache, apc

Solution 3:

In php.ini avoid using apc.shm_size = 50M but use apc.shm_size = 50. The M postfix makes PHP to ignore the changed size, although PHP is going to declare to you everywhere that 50M is what it uses. You'd be only mistaken.