memcachedのインストール

CentOSのバージョンを確認する

$ cat /etc/redhat-release
CentOS release 5.8 (Final)

CentOSが32bitか64bitかを確認する

$ uname -a
x86_64 GNU/Linux

/usr/local/srcへの移動

cd /usr/local/src

libeventのインストール

wget http://www.monkey.org/~provos/libevent-1.4.4-stable.tar.gz
tar zxvf libevent-1.4.4-stable.tar.gz
cd libevent-1.4.4-stable
./configure
make
make install

memcachedのインストール

wget http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz
tar zxvf memcached-1.2.5.tar.gz
cd memcached-1.2.5
./configure
make
make install

memcachedのインストール確認

$ memcached -h
memcached 1.2.5
-p <num>      TCP port number to listen on (default: 11211)
-U <num>      UDP port number to listen on (default: 0, off)
-s <file>     unix socket path to listen on (disables network support)
-a <mask>     access mask for unix socket, in octal (default 0700)
-l <ip_addr>  interface to listen on, default is INDRR_ANY
-d            run as a daemon
-r            maximize core file limit
-u <username> assume identity of <username> (only when run as root)
-m <num>      max memory to use for items in megabytes, default is 64 MB
-M            return error on memory exhausted (rather than removing items)
-c <num>      max simultaneous connections, default is 1024
-k            lock down all paged memory.  Note that there is a
              limit on how much memory you may lock.  Trying to
              allocate more than that would fail, so be sure you
              set the limit correctly for the user you started
              the daemon with (not for -u <username> user;
              under sh this is done with 'ulimit -S -l NUM_KB').
-v            verbose (print errors/warnings while in event loop)
-vv           very verbose (also print client commands/reponses)
-h            print this help and exit
-i            print memcached and libevent license
-b            run a managed instanced (mnemonic: buckets)
-P <file>     save PID in <file>, only used with -d option
-f <factor>   chunk size growth factor, default 1.25
-n <bytes>    minimum space allocated for key+value+flags, default 48

memcachedの起動

memcached -d -m 64 -l 127.0.0.1 -p 11225 -u root
64MBで127.0.0.1の11225ポートでrootユーザーとして起動する

memcachedの起動確認

方法1

$ telnet 127.0.0.1 11225
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
↓
ここでstatsと打ち、enterする
↓
$ telnet 127.0.0.1 11225
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Escape character is '^]'.
stats
STAT pid 6671
STAT uptime 3749
STAT time 1410168232
STAT version 1.2.5
STAT pointer_size 64
STAT rusage_user 0.000000
STAT rusage_system 0.000999
STAT curr_items 0
STAT total_items 0
STAT bytes 0
STAT curr_connections 2
STAT total_connections 7
STAT connection_structures 3
STAT cmd_get 0
STAT cmd_set 0
STAT get_hits 0
STAT get_misses 0
STAT evictions 0
STAT bytes_read 181
STAT bytes_written 833
STAT limit_maxbytes 67108864
STAT threads 1
END

quitで終了

方法2

$ ps aux | grep memcached                                                                                                                                                                                               
root      6671  0.0  0.0  17140  1164 ?        Ss   17:21   0:00 memcached -d -m 64 -l 127.0.0.1 -p 11225 -u root
root      7033  0.0  0.0  65384   848 pts/2    R+   18:26   0:00 grep memcached

memcachedPHPから使用できるようにする

memcacheモジュールをインストールする

$ cd /usr/local/src
$ sudo wget http://pecl.php.net/get/memcache-2.2.7.tgz
$ sudo tar xvzf memcache-2.2.7.tgz
$ cd memcache-2.2.7
$ sudo /usr/local/bin/phpize
$ sudo ./configure --with-php-config=/usr/local/bin/php-config
$ sudo make -j 4
$ sudo make install

extension_dirにmemcache.soを置く

php.iniの以下のディレクトリに配置する
extension_dir = "/usr/local/lib/php/extensions"
$ cp modules/memcache.so /usr/local/lib/php/extensions/memcache.so

php.iniの設定

vi /usr/local/lib/php.ini
※php.iniのパスは環境毎に異なりますので、phpinfo();などで確認してから編集してください。
extension=memcache.so