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

memcachedPHPUbuntu

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.

Best Answer

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.

Related Topic